How to start an application using Android ADB tools - android

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.

Related

Launching the installed android application in device via terminal

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.

How to bypass data URL in android "am" using IntelijIdea run/debug configuration

I`m wondering how can I bypass android application manager shell command options, aka -d from Intelij run/debug configuration? So I will be able to start Activity which requires URL as data in Intent.
Something similar to:
adb shell am start -d file:////storage/sdcard0/fileToOpen.txt -n some.package/some.Activity
So far I found that I can cheat during selecting an Activity and put:
some.package.Activity" -d "file:////storage/fileToOpen.txt
Key trick here is quotes just ignore error message, save configuration and run application. Trick is that InteliJ Idea will use string without any verification and will create following shell command:
DEVICE SHELL COMMAND: am start -n "some.package.Activity" -d "file:////storage/fileToOpen.txt"
I`m using idea - community 12.0.3

get launchable activity name of package from adb

Is there a way to get the launchable activity for a package from using adb? For an unroot phone (i.e. without having the pull the apk from /data/app directory and inspect with appt).
I tried dumpsys, but it does not include information on default launchable activity.
Thanks
You don't need root to pull the apk files from /data/app. Sure, you might not have permissions to list the contents of that directory, but you can find the file locations of APKs with:
adb shell pm list packages -f
Then you can use adb pull:
adb pull <APK path from previous command>
and then aapt to get the information you want:
aapt dump badging <pulledfile.apk>
$ adb shell pm dump PACKAGE_NAME | grep -A 1 MAIN
Since Android 7.0 you can use adb shell cmd package resolve-activity command to get the default activity of an installed app like this:
adb shell "cmd package resolve-activity --brief com.google.android.calculator | tail -n 1"
com.google.android.calculator/com.android.calculator2.Calculator
#!/bin/bash
#file getActivity.sh
package_name=$1
#launch app by package name
adb shell monkey -p ${package_name} -c android.intent.category.LAUNCHER 1;
sleep 1;
#get Activity name
adb shell logcat -d | grep 'START u0' | tail -n 1 | sed 's/.*cmp=\(.*\)} .*/\1/g'
sample:
getActivity.sh com.tencent.mm
com.tencent.mm/.ui.LauncherUI
I didn't find it listed so updating the list.
You need to have the apk installed and running in front on your phone for this solution:
Windows CMD line:
adb shell dumpsys window windows | findstr <any unique string from your pkg Name>
Linux Terminal:
adb shell dumpsys window windows | grep -i <any unique string from your Pkg Name>
OUTPUT for Calculator package would be:
Window #7 Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}:
mOwnerUid=10036 mShowToOwnerOnly=true package=com.android.calculator2 appop=NONE
mToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}
mRootToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}
mAppToken=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}
WindowStateAnimator{3e160d22 com.android.calculator2/com.android.calculator2.Calculator}:
mSurface=Surface(name=com.android.calculator2/com.android.calculator2.Calculator)
mCurrentFocus=Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}
mFocusedApp=AppWindowToken{29a4bed4 token=Token{2f850b1a ActivityRecord{eefe5c5 u0 com.android.calculator2/.Calculator t322}}}
Main part is, First Line:
Window #7 Window{39ced4b1 u0 com.android.calculator2/com.android.calculator2.Calculator}:
First part of the output is package name:
com.android.calculator2
Second Part of output (which is after /) can be two things, in our case its:
com.android.calculator2.Calculator
<PKg name>.<activity name> =
<com.android.calculator2>.<Calculator>
so .Calculator is our activity
If second part is entirely different from Package name and doesn't seem to contain pkg name which was before / in out output, then entire
second part can be used as main activity.
Here is another way to find out apps package name and launcher activity.
Step1: Start "adb logcat" in command prompt.
Step2: Open the app (either in emulator or real device)
You can also use ddms for logcat logs where just giving search of the app name you will all info but you have to select Info instead of verbose or other options. check this below image.
Launch your app and keep it in foreground.
Run the below command:
adb shell dumpsys window windows | find "mcurrentfocus"
mCurrentFocus doesn't work for me on Android 12 device.
Here is the right step to go:
Connect the device and open the app.
adb shell dumpsys window windows | grep -E mObscuringWindow
mObscuringWindow=Window{bc78a3 u0 com.yds.demo/com.test.activity.AppActivity}
com.test.activity.AppActivity is the activity.

How can I launch and execute Vending.apk on the Android Emulator from adb

I've been trying to install Vending.apk into my emulator w/o success. It says it already exists and fails to replace it when I use adb install -r. The icon does not show up on the screen so I can't tap it to launch the Google Play marketplace.
Therefore, I thought I could run it from my PC (MacOSX) using adb like this:
adb shell am start -a android.intent.action.MAIN -n com.android.vending/.Vending
I constructed the above from examples that work in this article:
How to run (not only install) an android application using .apk file?
And I unzipped the AndroidManifest.xml file using info from this method to see if I could discover the activity name, but no luck:
aapt dump xmltree <apk-file> AndroidManifest.xml
I guess I need to know the exact command to execute the vending apk because I can't seem to find the correct Activity class. adb shell am start keeps giving me error type 3, Activity class does not exist.
Thanks
You can try this:
adb shell am start -n com.android.vending/com.google.android.finsky.activities.MainActivity

How to start an android app with valgrind

I've been searching for the last week trying to find an answer to this question.
How do I start an Android app with valgrind? I know I can start an app with the 'am' command, but it starts the app and exits.
I'm writing an app that uses the NDK for native C code, and I need to check it for suspected memory errors.
Edit:
I've learned a little more. You can "wrap" an app with a shell script.
Here's the shell script I'm using:
#!/system/bin/sh
VGPARAMS='--error-limit=no'
export TMPDIR=/data/data/com.starlon.froyvisuals
exec /data/local/Inst/bin/valgrind $VGPARAMS $*
And here's setprop:
adb shell setprop wrap.com.starlon.froyvisuals "logwrapper valgrind"
And here's how I start the app:
adb shell am start -n com.starlon.froyvisuals/.FroyVisuals
I don't think this is right, because I'm not sure where the shell script fits in and I'm not seeing anything in logcat. Any hints?
Edit2:
Oh the shell script is indicated with "setprop" command above. So
adb shell setprop wrap.com.starlon.froyvisuals "logwrapper /data/local/val.sh"
I'm still not seeing anything in logcat.
You can try to clear the logcat first
prompt# adb logcat -c
prompt# adb logcat
You should be able to see the logs coming in once you triggered your application.
am start -a android.intent.action.MAIN -n com.example.hellojni/.HelloJni
I had problems with my shell script and i used this instead.
adb shell setprop wrap.com.example.hellojni "logwrapper /data/local/Inst/bin/valgrind"
You should be able to pass in the parameter right after valgrind
I encountered this problem too. In my situation, I edit the "val.sh" in windows & adb push it to the emulator, but the shell script could not be executed correctly. Then I use a echo "*" > val.sh style to make the "val.sh" and It works well.
So you should first make sure the "val.sh" could be interpreted correctly.

Categories

Resources