How to start an application using Android ADB tools
Asked Answered
C

18

606

How do I send an intent using Android's ADB tools?

Coloration answered 31/12, 2010 at 3:33 Comment(2)
stop: #3117595Mom
Get ActivityName: stackoverflow.com/a/37959688Reimers
E
755
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 
Earle answered 31/12, 2010 at 3:40 Comment(12)
Thank you very much , i've made it shell function in ~/.bash_profile to be much faster function androidrun(){ ant clean debug adb shell am start -n $1/$1.MainActivity } and its usage androidrun com.example.testGlobose
adb shell am will give you a list of other options to pass to the am command. You can find out more at developer.android.com/tools/help/adb.html#amOpportunity
@shurane How can I find -a option?Swirly
@Swirly I don't have access to a device offhand, but you can try just running adb shell am, which will list some more detailed help compared to the documentation.Opportunity
is is possible to run the default activity, instead of being so specific to which activity i intend it should start?Monmouth
AFAIK, you can omit part of the command like in Joilnen answerEarle
I had problems finding the application and activity. I used this pipe line adb logcat | grep --line-buffered ActivityManager | grep --line-buffered to list all applications that were displayed.Ricardoricca
Can you add an example of how to launch it with a deep link?Lockup
https://mcmap.net/q/65538/-how-to-run-a-specific-android-app-using-terminal-duplicate shows how to use cmd and how to obtain the Activity name knowing the packageMontage
This answer works on Oculus Go (as there is no way on its UI to run normal android apps) but it displays a non-VR view of the app. However, if your install and run your app through visual studio, you see the VR view of your app!Twill
If you have a dollar sign in an activity name, use 3 backslashes to escape it, e.g. adb shell "am start -a android.settings.SYNC_SETTINGS -n com.android.settings/.Settings\\\$AccountDashboardActivity"Perique
com.package.name/com.package.name.ActivityName, can be shortened/reduced to, com.package.name/.ActivityNameFirry
M
420

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).

Missive answered 20/8, 2014 at 7:18 Comment(15)
Launcher is default. You can simplify to adb shell monkey -p your.app.package.name 1Implement
Is it possible to use the APK file name, instead of the package name? Suppose I have an APK file, and I want to be able to install&run it, is it possible?Monmouth
What if I get an error: no activities found to run. aborting?Sheepish
@Sheepish if no activities found, it's a Service app and has no Activity at all.Lankester
--pct-syskeys 0 is required for devboards: https://mcmap.net/q/64181/-how-to-start-an-application-using-android-adb-toolsMom
@WSS im sorry but i think you're wrong. An visible APP has main activity at least. If you don't have activity nothing will happen except the case that you init a service at launch, ie notification app. Take a read: quora.com/Can-an-Android-app-be-made-without-an-activity. Regards.Lankester
@erm3nda sorry, I was wrong and that app worked. Since the app was open source I imported its project, but it has some problems and it caused it to fail.Lura
@Lura no worries :)Lankester
@Sheepish if it is a service use adb shell am startservice com.some.package.name/.YourServiceSubClassName see here: https://mcmap.net/q/65539/-how-to-start-and-stop-android-service-from-a-adb-shellSnowman
This should be the accepted answer. In particular, Androiderson's comment.Mirianmirielle
@androiddeveloper You can use the android tools to get the android package name and then launch that. See here: #6289649Duong
I found this to be the most useful technique because it only requires the package name and not the name of the launcher activity.Purposely
ty, best answer I have got.Brail
This helps me to launch gmail app. Thanks! To launch gmail app using adb shell adb shell monkey -p com.google.android.gm -c android.intent.category.LAUNCHER 1Cariole
It works, but has problems: 1) will launch random activity every time, 2) cannot add deeplink string. If you get ** No activities found to run, monkey aborted. check that you didn't unistall an app.Trombley
B
138

Or, you could use this:

adb shell am start -n com.package.name/.ActivityName
Bewick answered 1/9, 2012 at 15:42 Comment(7)
is it possible to do it without specifying the activity name, so that the default main activity will start?Monmouth
@androiddeveloper See the monkey command below from depodefi: no need to specify activity name!Gusta
@DanielBeauyat Nice. it works even on a device. Here's the link to the post, BTW: https://mcmap.net/q/64181/-how-to-start-an-application-using-android-adb-toolsMonmouth
It should be noted that if you use an applicationIdSuffix such as .debug for your debug builds, you have to use the fully qualified activity name: adb shell am start -n com.package.name.debug/com.package.name.ActivityName. The suffix only applies to the application id, not the package name of the java classes.Rosenthal
What if I don't know the ActivityName?Sheepish
Run ~/android-sdk-linux/build-tools/20.0.0/aapt dump badging yourapp.apk , which will list the following entry: launchable-activity: name='com.company.android.package.YourLaunchableActivity'Bracket
monkey -p com.package.name 1 via adb shellPuerperal
D
65

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.

Denis answered 25/6, 2013 at 5:47 Comment(7)
for windows you can try with cygwin (untested)Denis
Your post made me look up AWK. I went ahead and re-created your script. pastebin.com/X7X1SsFaReligiosity
This is fabulous. Why this isn't a standard adb command we'll never know.Ultravirus
You won't need to have aapt in your path (as long as "android" is) if you set the PATH in the adb-run.sh script, like this: PATH=$(dirname $(which android))/../build-tools/20.0.0:$PATHPrecipitation
@Denis why does this not work on play.google.com/store/apps/details?id=com.amaze.filemanager ?Lura
This answer is a real Good asset. I substituted aapt with : $HOME/Library/Android/sdk/build-tools/<version>/aaptCarruth
Note that there might be more than one launchable activity in one apk (I think) -- besides it might be better to run aapt only once...Coreen
A
40

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.

Apopemptic answered 16/11, 2018 at 12:41 Comment(2)
This will launch your application and send 500 pseudo-random events to it. So use '1' to replace '500' is better.Tensimeter
What is "Es explorer"? For example, can you add a reference to it? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)Merill
D
34

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.

Dysthymia answered 20/7, 2021 at 21:55 Comment(1)
Bam! Works great, even on Android 4.4. Thanks!Pucker
M
26

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.

Midland answered 26/5, 2014 at 6:35 Comment(3)
Also, worth mentioning that that there is no way to clear said flag, which is annoyingEinstein
would it help to supply the flag (-f) FLAG_ACTIVITY_CLEAR_TASK?Tichonn
What do you mean by "take you your app" (seems incomprehensible. Two instances)? Please respond by editing (changing) your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today).Merill
M
18

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.

Molt answered 4/1, 2014 at 12:42 Comment(0)
A
17

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
Alexipharmic answered 20/2, 2021 at 12:56 Comment(1)
Hmm, didn't work on Android 4.4. 1. the package names are all prefixed with package: (e.g. package:com.cnn.mobile.android.phone). 2. When trying to launch (with or without the package: prefix), I get Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=com.cnn.mobile.android.phone } Error: Activity not started, unable to resolve Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.cnn.mobile.android.phone }.Pucker
G
15

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
Globose answered 16/2, 2013 at 12:51 Comment(2)
Can you explain what app-debug does?Luxate
@TrevorSenior app-debug is the title of the apk file , usually in debug mode the generated apk file is titled "APPLICATION NAME-debug.apk"Globose
M
11

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?

Mom answered 25/10, 2017 at 14:30 Comment(0)
J
9

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'

Jumpy answered 29/11, 2022 at 16:51 Comment(0)
Z
6

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.

Zapateado answered 8/5, 2018 at 4:34 Comment(2)
your example codes don't show usage of ` APK-INFO`Thirza
What is "APK-INFO"? For example, can you add a reference to it? (But without "Edit:", "Update:", or similar - the answer should appear as if it was written today.)Merill
E
3
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
Ephemerality answered 17/3, 2021 at 10:22 Comment(0)
F
2

The right way would be to use cmd package resolve-activity to find the startup activity before launching, so you can launch with

am start $(cmd package resolve-activity --brief com.package.name | tail -n 1)

Firebreak answered 20/3, 2023 at 16:4 Comment(0)
L
0

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.

Lapidify answered 24/8, 2017 at 11:15 Comment(1)
I have a player app, which is able to stream a video from url, how can i pass this url as a parameter and stream from that url? suppose a url from youtube?Motorcade
A
0

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.

Aesculapius answered 28/4, 2022 at 23:28 Comment(0)
T
0

To guarantee support for Android TV as well as non-Android TV apps:

adb shell "monkey -p com.package.name -c android.intent.category.LEANBACK_LAUNCHER 1 || monkey -p com.package.name -c android.intent.category.LAUNCHER 1"
Tropic answered 15/8, 2023 at 15:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.