How can i run my adb command through apk - android

My ADB command is,
adb shell am instrument -w -r -e debug false -e class com.example.deepak.myapplication.ExampleInstrumentedTest com.example.deepak.myapplication.test/android.support.test.runner.AndroidJUnitRunner

Related

/system/bin/sh: tr: not found - Android Azure Devops pipeline

I get the following error when I try to start the emulator from my pipeline in Azure:
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot >
/dev/null 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop
sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'
$ANDROID_HOME/platform-tools/adb devices
echo "Emulator started"
Generating script.
##[debug]which 'bash'
##[debug]found: '/bin/bash'
##[debug]Agent.Version=2.204.0
##[debug]agent.tempDirectory=/Users/runner/work/_temp
##[debug]check path : /Users/runner/work/_temp
========================== Starting Command Output ===========================
##[debug]which '/bin/bash'
##[debug]found: '/bin/bash'
##[debug]/bin/bash arg: /Users/runner/work/_temp/52b9227f-faa1-
4557-9396-
8b3c63435983.sh
##[debug]exec tool: /bin/bash
##[debug]arguments:
##[debug] /Users/runner/work/_temp/52b9227f-faa1-4557-9396-
8b3c63435983.sh
/bin/bash /Users/runner/work/_temp/52b9227f-faa1-4557-9396-
8b3c63435983.sh
* daemon not running; starting now at tcp:5037
* daemon started successfully
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
/system/bin/sh: tr: not found
Given below is my pipeline yml
# Android
# Build your Android project with Gradle.
# Add steps that test, sign, and distribute the APK, save build artifacts, and more:
# https://learn.microsoft.com/azure/devops/pipelines/languages/android
#trigger:
#- feature/azure-cicd
pool:
vmImage: 'macos-latest'
steps:
- bash: |
# Install AVD files
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-19;google_apis;x86'
echo "AVD system-image successfully downloaded and installed."
displayName: 'Download and install emulator image'
- bash: |
# Create emulator
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n xamarin_android_emulator -k 'system-images;android-19;google_apis;x86' --force
$ANDROID_HOME/emulator/emulator -list-avds
displayName: 'Create emulator'
- bash: |
# Start emulator in background
nohup $ANDROID_HOME/emulator/emulator -avd xamarin_android_emulator -no-snapshot > /dev/null 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'
$ANDROID_HOME/platform-tools/adb devices
echo "Emulator started"
displayName: 'Start emulator'
- bash: |
./gradlew testdevelopDebug connectedMockDebugAndroidTest --stacktrace --no-daemon
./gradlew --stop
displayName: 'Run Instrumented Tests'
continueOnError: true
This works perfect when I use android-28 instead of android-19
Is there any change that I need to make in my pipeline such that I can launch the emulator?
Any help is much appreciated
The adb call in yml was done to get the status and check that locally. In the yml I was trying to pass the while loop into adb.
This example depicts the right usage and solution:
https://stackoverflow.com/a/38896494/
while [ "`adb shell getprop sys.boot_completed | tr -d '\r' `" != "1" ] ; do sleep 1; done

No tests found when enable ANDROIDX_TEST_ORCHESTRATOR(1.4.0)

I've read related question
However, I'm using the 'androidx.test:orchestrator:1.4.0', which should not has the bug below 1.2.0.
My case is, I will get error: com.android.build.gradle.internal.testing.ConnectedDevice > No tests found.[GM1910 - 11] FAILED if I enable :
testOptions {
execution 'ANDROIDX_TEST_ORCHESTRATOR'
}
I've done the prefix operations:
$ curl -O https://dl.google.com/android/maven2/androidx/test/orchestrator/1.4.0/orchestrator-1.4.0.apk
$ curl -O https://dl.google.com/android/maven2/androidx/test/services/test-services/1.4.0/test-services-1.4.0.apk
$ adb install -r orchestrator-1.4.0.apk
$ adb install -r test-services-1.4.0.apk
$ adb shell 'CLASSPATH=$(pm path androidx.test.services) app_process / \
androidx.test.services.shellexecutor.ShellMain am instrument -w -e \
targetInstrumentation com.example.notroid/androidx.test.runner.AndroidJUnitRunner \
androidx.test.orchestrator/.AndroidTestOrchestrator'
Time: 0
OK (0 tests)

How to install an app in system/app while developing from android studio

Is there a way to make an app install directly in the system/app folder while developing on Android Studio (the device is rooted)?
Meaning, when I press on the 'Run app' button, I want the apk to be placed in system/app.
If this is not possible, what is the recommended most convenient way to work on building and testing a system app?
Deploy automatically system app from AS
You can create a script that will do the job, and run it automatically each time you hit run in AS.
1. Create the script
You can adapt this script that I've created from my needs. Place it in: project_directory/installSystem.sh
#!/bin/bash
# CHANGE THESE FOR YOUR APP
app_package="com.example"
dir_app_name="MySysApp"
MAIN_ACTIVITY="SysAppMainActivity"
ADB="adb" # how you execute adb
ADB_SH="$ADB shell" # this script assumes using `adb root`. for `adb su` see `Caveats`
path_sysapp="/system/priv-app" # assuming the app is priviledged
apk_host="./app/build/outputs/apk/app-debug.apk"
apk_name=$dir_app_name".apk"
apk_target_dir="$path_sysapp/$dir_app_name"
apk_target_sys="$apk_target_dir/$apk_name"
# Delete previous APK
rm -f $apk_host
# Compile the APK: you can adapt this for production build, flavors, etc.
./gradlew assembleDebug || exit -1 # exit on failure
# Install APK: using adb root
$ADB root 2> /dev/null
$ADB remount # mount system
$ADB push $apk_host $apk_target_sys
# Give permissions
$ADB_SH "chmod 755 $apk_target_dir"
$ADB_SH "chmod 644 $apk_target_sys"
#Unmount system
$ADB_SH "mount -o remount,ro /"
# Stop the app
$ADB shell "am force-stop $app_package"
# Re execute the app
$ADB shell "am start -n \"$app_package/$app_package.$MAIN_ACTIVITY\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER"
2. Bind it with AS Run
Go to Run -> Edit Configurations
Do the following changes on General tab (of your module)
Installation Options->Deplay: Nothing
Launch Options->Launch: Nothing
Before launch: press +, then Run External Tool, to select your script.
In the new dialog:
set any name.
On 'Tool Settings'->Program: navigate to the project's dir, and select your script
Caveats :
First installation
The device needs to be restarted (adb reboot) only once, on the very first installation of your app. Afterwards, you can simply press Run and everything will happen automatically.
This is because the host compiler (dex2oat) is not invoked automatically. Somehow the OS is not yet informed for this new system app. Calling dex2oat manually should solve this, but I had no luck. If anyone solves it please share.
adb root issues
Sometimes (usually the initial execution after the restart) the call to adb root does not find the device. You can simply re-play from AStudio, or sleep for a second after a successful adb root.
using su instead of adb root
adb push won't be working despite mounting system and giving permissions. To make it work replace the ADB_SH variable and the install section of the script with the following:
..
ADB_SH="$ADB shell su -c"
..
# Install APK: using adb su
$ADB_SH "mount -o rw,remount /system"
$ADB_SH "chmod 777 /system/lib/"
$ADB_SH "mkdir -p /sdcard/tmp" 2> /dev/null
$ADB_SH "mkdir -p $apk_target_dir" 2> /dev/null
$ADB push $apk_host /sdcard/tmp/$apk_name 2> /dev/null
$ADB_SH "mv /sdcard/tmp/$apk_name $apk_target_sys"
$ADB_SH "rmdir /sdcard/tmp" 2> /dev/null
Windows script for those interested:
Store this file the same way: in the root of your project directory (installSysPrivApp.bat)
::WIN BATCH SCRIPT
:: CHANGE THESE
set app_package=com.example.package
set dir_app_name=app
set MAIN_ACTIVITY=MainActivity
set ADB="adb"
::ADB_SH="%ADB% shell" # this script assumes using `adb root`. for `adb su`
see `Caveats`
set path_sysapp=/system/priv-app
set apk_host=.\Application\build\outputs\apk\Application-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su -c
:: Install APK: using adb su
%ADB_SH% mount -o rw,remount /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rmdir /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
::Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
To bypass reboot issue from the #paschalis answer reinstall application with a help of package manager before remounting system to read only:
# Reinstall app
$ADB_SH "pm install -r $apk_target_sys"
# Unmount system
$ADB_SH "mount -o remount,ro /"
Package manager will invoke dex2oat by itself.
(Android Q >> Windows)
::WIN BATCH SCRIPT
::setup emulator https://stackoverflow.com/a/64397712/13361987
:: CHANGE THESE
set app_package=com.project.package
set dir_app_name=NewApkName
set MAIN_ACTIVITY=Package.MainActivity
set ADB="adb"
set path_sysapp=/system/priv-app
set apk_host=.\app\build\outputs\apk\debug\app-debug.apk
set apk_name=%dir_app_name%.apk
set apk_target_dir=%path_sysapp%/%dir_app_name%
set apk_target_sys=%apk_target_dir%/%apk_name%
:: Delete previous APK
del %apk_host%
:: Compile the APK: you can adapt this for production build, flavors, etc.
call gradlew assembleDebug
set ADB_SH=%ADB% shell su 0
:: Install APK: using adb su
%ADB_SH% mount -o remount,rw /system
%ADB_SH% chmod 777 /system/lib/
%ADB_SH% mkdir -p /sdcard/tmp
%ADB_SH% mkdir -p %apk_target_dir%
%ADB% push %apk_host% /sdcard/tmp/%apk_name%
%ADB_SH% mv /sdcard/tmp/%apk_name% %apk_target_sys%
%ADB_SH% rm -r /sdcard/tmp
:: Give permissions
%ADB_SH% chmod 755 %apk_target_dir%
%ADB_SH% chmod 644 %apk_target_sys%
:: Unmount system
%ADB_SH% mount -o remount,ro /
:: Stop the app
%ADB% shell am force-stop %app_package%
:: Re execute the app
%ADB% shell am start -n \"%app_package%/%app_package%.%MAIN_ACTIVITY%\" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
:: from >> https://stackoverflow.com/questions/28302833/how-to-install-an-app-in-system-app-while-developing-from-android-studio
For mac
By using the script of #Paschalis I got 3 problems first I couldn't mount the system from adb so I did it with "terminal emulator for android" from jack palevich only (needed once) https://play.google.com/store/apps/details?id=jackpal.androidterm
mount -o rw,remount /system
the second problem was the JRE that was not the same as Android studio.
so I added
export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home/
to have the same version of java that android studio was using ("ctrl + ;" in android studio to get this path)
And the last problem because of adb root that can not run in production build so I flashed this zip with magisk
https://github.com/evdenis/adb_root
Android: adbd cannot run as root in production builds
but then my phone was not detected anymore so I removed adb root and this time all works well.
Also try to run the script manually line by line in a terminal to debug this script android studio does not give all the error.
I think adb push *.apk /system/app/*.apk should do just fine.
I don't know about Android Studio, but if you are on Linux you can get try to create an alias for
adb install
that points to that command, It should work!

Get result code of adb shell command

How should I get $? of adb shell <command>?
I would like to check the result of adb shell mkdir /xxx.
I executed mkdir command by adb shell and failed but the result of $? is 0.
$ adb shell mkdir /xxx
mkdir failed for /xxx, Read-only file system
$ adb shell echo $?
0
$ adb shell "mkdir /xxx; echo $?"
mkdir failed for /xxx, Read-only file system
0
I would like to get the result code of adb shell <command> but not in the interactive mode like below:
$ adb shell
shell#android:/ $ mkdir /xxx
mkdir failed for /xxx, Read-only file system
255|shell#android:/ $ echo $?
255
You should do:
$ adb shell 'mkdir /xxx; echo $?'
mkdir failed for /xxx, Read-only file system
255
Notice the single quotes, otherwise $? is evaluated before reaching adb.
If you are using an older emulator, then adb shell does not return the exit value. Then you have to use adb shell 'false; echo $?' like in https://stackoverflow.com/a/17480194/306864
In newer emulators, it works properly, then you can do adb shell false || echo failed. Here's how I tested:
$ adb -e android-22 shell false; echo $?
0
$ adb -e android-22 shell 'false; echo $?'
1
$ adb -e android-29 shell false; echo $?
1
$ adb -e android-29 shell 'false; echo $?'
1

Running a shell script on android device using adb

Manually, we can run:
adb shell
su
chmod 666 /dev/graphics/fb0
export CLASSPATH=/data/local/device.jar
export LD_LIBRARY_PATH=/data/local
exec app_process /system/bin com.device.client.Main /data/local/device.conf &
However, we need to be able to run that from a bash script on the computer compiling the program.
I have tried:
adb shell "su
&& chmod 666 /dev/graphics/fb0
&& export CLASSPATH=/data/local/device.jar
&& export LD_LIBRARY_PATH=/data/local
&& exec app_process /system/bin com.device.client.Main /data/local/device.conf &"
But since we are entering the su shell, this does not work.
Can you please suggest a solution?
Try this:
adb shell "su -c '
chmod 666 /dev/graphics/fb0
&& export CLASSPATH=/data/local/device.jar
&& export LD_LIBRARY_PATH=/data/local
&& exec app_process /system/bin com.device.client.Main
/data/local/device.conf &'"
It might be possible to simplify it, too:
adb shell "su -c '
chmod 666 /dev/graphics/fb0 &&
CLASSPATH=/data/local/device.jar
LD_LIBRARY_PATH=/data/local
app_process /system/bin com.device.client.Main
/data/local/device.conf &'"
This is because you can set environment variables for one job just by prepending them on the line, rather than the export this, export that form.

Categories

Resources