53

From the post Is it possible to start activity through adb shell?, we can start an Android app via adb by

adb shell am start -n yourpackagename/.activityname

But is it possible to start an Android app via adb without knowing the Activity name? For example, by sending the android.intent.action.MAIN intent to a package? Maybe some command like this:

adb shell am start -a android.intent.action.MAIN -n packageName

Thanks!!

-Cosmo

Alex P.
  • 30,437
  • 17
  • 118
  • 169
hackjutsu
  • 8,336
  • 13
  • 47
  • 87
  • 2
    http://stackoverflow.com/questions/4567904/how-to-start-an-application-using-android-adb-tools/25398877#25398877 – Daniel Nugent Apr 28 '15 at 23:13
  • rule of thumb - when your accepted answer is nothing but a reference to another post - your question is a duplicate – Alex P. Aug 28 '18 at 22:07
  • 2
    @AlexP. But this question is not quite the same—— At least, Google "adb start app" result in lots of `adb shell am start -n packagename/.activityname` things; while Google "adb start app **without knowing Activity name**" won't filter out the duplicated SO question. So this question could be reasonably valuable. – Weekend Aug 29 '18 at 02:38
  • 3
    The duplicated question accidentally has an answer which solves this question. But it's not the duplicated question specifically asked for. This question is asking for a more particular situation. – Weekend Aug 29 '18 at 02:50

3 Answers3

105

Yes, it is possible to launch an app via adb shell making use of the monkey tool.

Using the command from this answer:

adb shell monkey -p app.package.name -c android.intent.category.LAUNCHER 1

This command simulates the app icon click, so the intent implicit intent LAUNCHER is delivered to the specific receiver declared in app manifest (MAIN)

Community
  • 1
  • 1
Leu
  • 1,354
  • 1
  • 10
  • 19
  • It worked. But how to detect Main Activity name? I want to send parameter to a activity (main activity in com.sec.android.gallery3d package), So I should know main activity name. – Dr.jacky Dec 15 '15 at 08:17
  • Is it also possible, given the APK file, to run this ? Suppose I have an APK file, and I want to be able to install&run it, is it possible? – android developer Jun 15 '16 at 07:41
  • 3
    I get an error: **No activities found to run, monkey aborted** – IgorGanapolsky Aug 04 '16 at 21:14
  • 2
    @IgorGanapolsky, are you sure the package name is correct? If you're the developer of the application, make sure you have at least one activity with the intent filter with action `android.intent.action.MAIN` and category `android.intent.category.LAUNCHER` – Leu Aug 09 '16 at 21:06
  • @androiddeveloper yes, it's possible. Android Studio itself use the adb to install and run the app when you click to run the file. Take a look at [the official adb documentation](https://developer.android.com/studio/command-line/adb.html#move) for further details – Leu Aug 09 '16 at 21:12
  • @Leuofiridia The question was for a given APK file. I think the IDE already knows the package name and default activity... – android developer Aug 09 '16 at 23:20
  • @androiddeveloper you can extract the AndroidManifest.xml file from the apk, and run a binary to text conversion in it to see the package field value. Take a look at [this online tool](http://tools.ankurbhargava.com/APK_Manifest_Converter). Also, if you have the google play link of determined APK, you can see the package name in the URL – Leu Aug 16 '16 at 15:24
  • @Leuofiridia Again, it's not a part of adb or anything similar. – android developer Aug 16 '16 at 22:11
  • @androiddeveloper after a quick research, I've found that aapt have the means to extract the package name from apk file using this command `/build-tools/ /aapt dump permissions | grep package:\ ` – Leu Aug 19 '16 at 18:44
  • @Leuofiridia So, suppose I have an APK file on "c:/myGreatApk.apk" , what would be the command/s to install&run it? – android developer Aug 19 '16 at 21:17
  • @androiddeveloper I'm not skilled in windows command line nor power shell, but you need to do a port of [this unix script](http://stackoverflow.com/a/17289998/1648969) into windows cmd/ps language. – Leu Aug 29 '16 at 15:58
14

Using latest versions (at least API 25) you can start the default Activity, without knowing its name, and not using monkey:

PKG=com.android.calculator2
adb shell am start $PKG/$(adb shell cmd package resolve-activity -c android.intent.category.LAUNCHER $PKG | sed -n '/name=/s/^.*name=//p')
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • I don't like nitpicking much but it is not *starting the default Activity, without knowing its name* - it's just *finding the default Activity first, then using it*. Also there is no need to run `cmd package` in a separate `adb shell` session - it could be run in the same shell as `am start`. As a bonus the command will be more OS agnostic (currently it requires some unix shell to process the substitution). Finally, `cmd package resolve-activity` command has a `--brief` parameter which would print the full name of the default activity (as the last line) even without specifying the category. – Alex P. Dec 24 '16 at 21:32
  • 9
    `adb shell 'am start $(cmd package resolve-activity --brief com.google.android.calculator | tail -n 1)'` - for Windows replace `'` with `"` – Alex P. Dec 24 '16 at 21:38
0

If you have the apk file, you can install and start the app using droidbot.

> droidbot -d emulator-5554 -a <xxx.apk> -install_app -policy none -count 0

DroidBot is based on adb, but it uses static analysis to extract the Activity name automatically.