What I have:
I have application installed in device
I have a Mac system and I am using terminal
I am connected to device via terminal
What I am trying to do:
I am trying to launch the installed application via terminal ( I
don't want to reinstall the app and run )
I need to find the app via package name and run it
What I have tried:
admins-MacBook-Pro:platform-tools devrath$ ./adb shell monkey -p com.cnx.dictionary -c android.intent.category.LAUNCHER 1
Error I am getting:
** No activities found to run, monkey aborted.
To find the package, use adb shell pm list packages. There will more than likely be a lot of packages listed. If you know a part of the package name, you can use grep to limit your results. If you were looking for the Facebook package name, you could use adb shell pm list packages | grep facebook and it would only show results with facebook in it.
From there, you need you find the class to start.
adb shell
dumpsys package | grep -Eo "^[[:space:]]+[0-9a-f]+[[:space:]]+com.packagename/[^[:space:]]+" | grep -oE "[^[:space:]]+$" (make sure you change the "com.packagename" part to the name of your package.
Find the class you want to use from the output list. Most apps will have a class of .SplashActivity, .HomeActivity, or .Main but you will have to find the one for your app
When you have that, use am start -n com.packagename/.Class where your package name replaces com.packagename and the class you chose replaces .Class.
One thing to note. In step 1, we used the command adb shell so we are in the of the android device issuing the commands after. If you are issuing the am start command at the main terminal screen, you will have to add adb shell before am start.
I used the zebra site a while back to get this info initially so i left it here for your reference as well.
What i want
I want to remove some google Apps with a shell script, started from my windows client using adb.
What is the problem
When I use following command in command prompt using adb / shell it works.
pm uninstall --user 0 com.google.android.apps.maps
But when I put the same command into a shell script, push it to my phone and try to run it - it gives the app is not installed error!
I solved the problem with a little workaround.
I just start the pm uninstall command also from the batch and do not push it to the device.
Like this: (uninstall.bat)
adb shell pm uninstall --user 0 com.google.android.gm
adb shell pm uninstall --user 0 com.google.android.apps.maps
adb shell pm uninstall --user 0 com.google.android.youtube
pause
The script you created on your Windows system contains MS-DOS/Windows style newlines (i.e. \r\n). Android shell uses Linux style newlines (\n). So your script gets an extra \r character at the end of every line and your
pm uninstall --user 0 com.google.android.apps.maps becomes pm uninstall --user 0 com.google.android.apps.maps\r.
Your script tries to uninstall a package named com.google.android.apps.maps\r - which does not exist.
To fix the issue you need to remove all those \r from your script after pushing it to the phone using sed -i 's/\r$//' script.sh command for example.
From a shell on my PC, I can run adb shell cmd package list packages, and get a list of all installed packages. I would like to run this and similar commands locally on my Android phone (Nexus 6P) in a terminal emulator (currently using Termux).
If I open the same shell with /system/bin/sh, and then try to run /system/bin/cmd package list packages, nothing happens (no errors, just outputs nothing and reloads the prompt).
If I run /system/bin/cmd -l the list of options appears as expected. $PATH and $LD_LIBRARY_PATH are the same in both environments. One major difference is that echo $USER returns "shell" from adb shell, but returns my local username from /system/bin/sh launched from Termux.
Is there any way to replicate the behavior of commands run from adb shell in a terminal emulator locally on Android?
Edit:
My device is rooted, and I am OK with root only solutions.
The problem is Termux. By design, Termux runs only (or is mostly?) the Linux command line programs that you install from within Termux using apt or the newer "native" package management interface, e.g. apt install bsdtar. What you need to run the adb shell commands is a terminal emulator that can truly access the underlying Android file system, not just the Termux that is practically a chroot save for the fact that it's aware it's not running commands from the filesystem root /.
As a simple test, run the following command:
which ls
It should return something like /system/bin/ls. But if it returns something like /data/data/com.termux/files/usr/bin/applets/ls then you have to change your terminal emulator to something else. I suspect that Termux was designed to take into account the more restrictive shell execution policies that Google put into place after KitKat or the Android 4.X.
The Android distribution I'm using, LineageOS 14.1, comes with a built-in shell emulator that allows me to run commands found in /system/bin/ls.
I don't have a rooted Nougat device handy, but something like the following may be a close enough approximation to adb shell (assuming you are using SuperSU):
env -i USER=shell "$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su)" shell --context u:r:shell:s0 --shell /system/bin/sh --command COMMAND
I (very briefly) tested it from Termux on a rooted Marshmallow device.
To elaborate:
the -i flag is used to start with an empty environment
USER=shell isn't specifically required, but for some reason su refuses to run with a completely empty environment
$(PATH=/system/xbin:/system/bin:/su/bin:/sbin:/magisk/.core/bin which su) points to the full path of the su binary on your device and can be hardcoded if you prefer
shell instructs the su binary to login as the shell user (the same as adb shell)
--context u:r:shell:s0 sets the appropriate SELinux context
--shell /system/bin/sh instructs SuperSU to use the system shell rather than it's own sush shell
Another option would be to actually run adb from the device, connecting to itself over TCP. If you need some functionality that is only available via adb (e.g. in my case it was adb forward) then this may be your only option. Unfortunately this isn't particularly convenient.
I wasn't able to find success with any publicly available adb binaries, so I build it myself with a few minor changes. You can see the sources I used and the changes I made at https://github.com/shakalaca/fastboot-adb-android and https://github.com/brbsix/fastboot-adb-android, respectively.
Once you have adb installed, here's an abbreviated list of commands I used to connect to the device:
# Add iptables rules to block external connections to port 9999'
su root iptables -N adbd
su root iptables -A adbd -i lo -p tcp -m tcp --dport 9999 -j ACCEPT
su root iptables -A adbd -p tcp -m tcp --dport 9999 -j DROP
su root iptables -A INPUT -j adbd
# Necessary in order to display authorization prompt
su shell setprop ro.debuggable 1
su shell setprop service.adb.tcp.port 9999
su root start adbd
adb connect 127.0.0.1:9999
adb wait-for-local-device
To shut down:
adb kill-server
su root stop adbd
su shell setprop ro.debuggable 0
su shell setprop service.adb.tcp.port 0
su root iptables -D INPUT -j adbd
su root iptables -F adbd
su root iptables -X adbd
So I tried this recently...if you're rooted you can use a terminal emulator.
su
then the command you want without "adb shell" part of it.
i tried the command "adb shell dumpsys deviceidle force-idle" in order to force device into doze.
I did this on the device via terminal emulator as:
"dumpsys deviceidle force-idle" and it did take effect.
also the dumpsys batterystats command worked.
be careful with commands with extensive text output, as the screen will be flooded with the output and will be unresponsive for some time.
EDIT
I originally answered this without the termux tag in mind. This worked for me while trying to execute shell commands on a vanilla emulator and saw this question while researching, so I tried to answer it differently.
You almost had it there in your question. You only need to execute sh:
int result = -1;
try {
final Process shell = Runtime.getRuntime().exec("sh");
final DataOutputStream commands = new DataOutputStream(shell.getOutputStream());
commands.writeBytes("write a series");
commands.writeBytes("of commands here");
commands.writeBytes("exit\n");
commands.flush();
result = shell.waitFor();
}
} catch (Exception e) {
e.printStackTrace();
}
If result == 0 the commands were succesful, else otherwise
Only rooted android
Busybox must be installed (though you can try without it)
Just write the normal command without the prefix adb
I am trying to understand the use of the following command:
adb shell pm disable <PACKAGE_OR_COMPONENT>
For example: I have a test app with package name 'com.example.com.testapp' and it has an activity called 'TestActivity'.
I ran the following from my terminal :
./adb shell pm disable com.example.com.testapp/TestActivity
From the name of the command, I thought it would disable the activity from launching, but I can launch it fine via adb as follows:
./adb shell am start -n com.example.com.testapp/.TestActivity
So what does this command disable then ?
I found the problem. It has nothing to do whether device is rooted or not.
I command had a missing dot. It should be :
./adb shell pm disable com.example.com.testapp/.TestActivity
Now if I try to launch the component, it's blocked (the app stops working).
How do I send an intent using Android's ADB tools?
adb shell
am start -n com.package.name/com.package.name.ActivityName
Or you can use this directly:
adb shell am start -n com.package.name/com.package.name.ActivityName
You can also specify actions to be filter by your intent-filters:
am start -a com.example.ACTION_NAME -n com.package.name/com.package.name.ActivityName
It's possible to run an application specifying the package name only using the monkey tool by follow this pattern:
adb shell monkey -p your.app.package.name -c android.intent.category.LAUNCHER 1
The command is used to run the app using the monkey tool which generates random input for the application. The last part of the command is an integer which specifies the number of generated random input for the app. In this case the number is 1, which in fact is used to launch the app (icon click).
Or, you could use this:
adb shell am start -n com.package.name/.ActivityName
Linux and Mac users can also create a script to run an APK file with something like the following:
Create a file named "adb-run.sh" with these three lines:
pkg=$(aapt dump badging $1|awk -F" " '/package/ {print $2}'|awk -F"'" '/name=/ {print $2}')
act=$(aapt dump badging $1|awk -F" " '/launchable-activity/ {print $2}'|awk -F"'" '/name=/ {print $2}')
adb shell am start -n $pkg/$act
Then "chmod +x adb-run.sh" to make it executable.
Now you can simply:
adb-run.sh myapp.apk
The benefit here is that you don't need to know the package name or launchable activity name. Similarly, you can create "adb-uninstall.sh myapp.apk"
Note: This requires that you have Android Asset Packaging Tool (aapt) in your path. You can find it under the new build tools folder in the SDK.
Step 1: First get all the package names of the apps installed in your device, by using:
adb shell pm list packages
Step 2: You will get all the package names. Copy the one you want to start using ADB.
Step 3: Add your desired package name in the below command.
adb shell monkey -p 'your package name' -v 500
For example,
adb shell monkey -p com.estrongs.android.pop -v 500
to start the Es explorer.
The shortest command yet is the following:
adb shell monkey -p your.app.package.name 1
This will launch the default activity for the package that is in the launcher.
Thanks to Androiderson for the tip.
Also, I want to mention one more thing.
When you start an application from adb shell am, it automatically adds FLAG_ACTIVITY_NEW_TASK flag which makes behavior change. See the code.
For example, if you launch a Play Store activity from adb shell am, pressing the 'Back' button (hardware back button) wouldn't take you back to your app. Instead, it would take you to the previous Play Store activity if there was some (if there was not a Play store task, then it would take you back to your app). FLAG_ACTIVITY_NEW_TASK documentation says:
if a task is already running for the activity you are now starting, then a new activity will not be started; instead, the current task will simply be brought to the front of the screen with the state it was last in
This caused me to spend a few hours to find out what went wrong.
So, keep in mind that adb shell am add FLAG_ACTIVITY_NEW_TASK flag.
We can as well start an application by knowing the application type and feeding it with data:
adb shell am start -d "file:///sdcard/sample.3gp" -t "video/3gp" -a android.intent.action.VIEW
This command displays available *video players to play a sample.3gp file.
You can find your app package name by the below command:
adb shell pm list packages
The above command returns a package list of all apps. Example:
org.linphone.debug
.
.
com.android.email
Now I want to start app linphone by using the below command and this worked for me:
adb shell am start org.linphone.debug
Open file ~/.bash_profile, and add these Bash functions to the end of the file
function androidinstall(){
adb install -r ./bin/$1.apk
}
function androidrun(){
ant clean debug
adb shell am start -n $1/$1.$2
}
Then open the Android project folder:
androidinstall app-debug && androidrun com.example.app MainActivity
monkey --pct-syskeys 0 for development boards
This argument is needed for development boards without keys/display:
adb shell monkey --pct-syskeys 0 -p com.cirosantilli.android_cheat.textviewbold 1
Without it, the app won't open, and you will get an error message like:
SYS_KEYS has no physical keys but with factor 2.0%
It was tested on HiKey960, Android O AOSP.
Learned from: this GitHub issue
Also asked at: How to use the monkey command with an Android system that doesn't have physical keys?
Use:
adb shell am start -n '<appPackageName>/.<appActitivityName>
Example:
adb shell am start -n 'com.android.settings/.wifi.WifiStatusTest'
You can use the APK-INFO application to know the list of app activities with respect to each app package.
adb shell am start -n com.app.package.name/com.java.package.name.ActivityName
Example
adb shell am start -n com.google.android.googlequicksearchbox/com.google.android.search.core.google.GoogleSearch
If the Java package is the same, then it can be shortened:
adb shell am start -n com.example.package/.subpackage.ActivityName
Use:
adb shell am start -n '<appPackageName>/<appActitivityName>'
To get <appPackageName> run :
adb shell pm list packages
To get <appActitivityName> lunch app and run
adb shell dumpsys window | grep -E 'mCurrentFocus'
Try this, for opening an Android photo app and with the specific image file to open as a parameter.
adb shell am start -n com.google.android.apps.photos/.home.HomeActivity -d file:///mnt/user/0/primary/Pictures/Screenshots/Screenshot.png
It will work on latest version of Android. No pop up will come to select an application to open as you are giving the specific app to which you want to open your image with.
When you try to open a Flutter app, you can use this command:
adb shell am start -n com.package.name/io.flutter.embedding.android.FlutterActivity
Replace com.package.name with your package name. You find your package in your app/build.gradle at applicationId.