44

I installed Eclipse and Android SDK already. Everything is running fine.

I want to install an .apk file so I follow the instruction already. But the problem is, when I start the emulator, it doesn't run my app automatically.

Is there a command in the terminal that allow me to run the specific app that I ask for it?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Lam Tran
  • 441
  • 1
  • 4
  • 3

4 Answers4

74

Use the cmd activity start-activity (or the alternative am start) command, which is a command-line interface to the ActivityManager. Use am to start activities as shown in this help:

$ adb shell am
usage: am [start|instrument]
       am start [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
                [-c <CATEGORY> [-c <CATEGORY>] ...]
                [-e <EXTRA_KEY> <EXTRA_VALUE> [-e <EXTRA_KEY> <EXTRA_VALUE> ...]
                [-n <COMPONENT>] [-D] [<URI>]
       ...

For example, to start the Contacts application, and supposing you know only the package name but not the Activity, you can use

$ pkg=com.google.android.contacts
$ comp=$(adb shell cmd package resolve-activity --brief -c android.intent.category.LAUNCHER $pkg | tail -1)
$ adb shell cmd activity start-activity $comp

or the alternative

$ adb shell am start -n $comp

See also http://www.kandroid.org/online-pdk/guide/instrumentation_testing.html (may be a copy of obsolete url : http://source.android.com/porting/instrumentation_testing.html ) for other details.

To terminate the application you can use

$ adb shell am kill com.google.android.contacts

or the more drastic

$ adb shell am force-stop com.google.android.contacts
Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
7

I keep this build-and-run script handy, whenever I am working from command line:

#!/usr/bin/env bash

PACKAGE=com.example.demo
ACTIVITY=.MainActivity
APK_LOCATION=app/build/outputs/apk/app-debug.apk
echo "Package: $PACKAGE"

echo "Building the project with tasks: $TASKS"
./gradlew $TASKS

echo "Uninstalling $PACKAGE"
adb uninstall $PACKAGE

echo "Installing $APK_LOCATION"
adb install $APK_LOCATION

echo "Starting $ACTIVITY"
adb shell am start -n $PACKAGE/$ACTIVITY
Shubham Chaudhary
  • 47,722
  • 9
  • 78
  • 80
  • When I run this command I'm getting an error saying **Error: Activity class {com.example.demo/com.example.demo.MainActivity} does not exist.**. You know how to solve this? It also says Error type 3 – Ezio Jun 14 '17 at 10:20
  • @Ezio Please change line 2 in script to point to your main activity. In my case it is: MainActivity.java in root of the package => com.example.demo.MainActivity. This will be something different for you. Also you need to update the package name (com.example.demo) at line 1 to your app's package name. – Shubham Chaudhary Jun 15 '17 at 09:02
  • I know about that Shubham, I made all the changes that you said but still getting the error. But now I used **adb shell monkey -p com.mypackage.name -c android.intent.category.LAUNCHER 1** and it worked. I wrote an answer in case anyone else is facing similar issues – Ezio Jun 15 '17 at 10:17
6

You can Start the android Service by this command.

adb shell am startservice -n packageName/.ServiceClass

Siddharth
  • 9,349
  • 16
  • 86
  • 148
user3085499
  • 61
  • 1
  • 1
  • i appreciate you answer,but please describe your answer in some details not in only two lines for users help. – Hamad Dec 10 '13 at 05:32
5

I used all the above answers and it was giving me errors so I tried

adb shell monkey -p com.yourpackage.name -c android.intent.category.LAUNCHER 1

and it worked. One advantage is you dont have to specify your launcher activity if you use this command.

Ezio
  • 2,837
  • 2
  • 29
  • 45
  • I could only get it with this monkey command thanks. The others were giving me permission errors. – giorgio79 Aug 24 '17 at 12:20
  • Is the monkey command on all Android devices? I have blind code execution on an embedded Android device, don't know what commands it has and what commands it doesn't have. – Billy Oct 05 '18 at 12:03
  • the monkey command is an adb command, it does not depend on the device, rather it is present in your development machine. – Ezio Oct 05 '18 at 15:40