How to run a specific Android app using Terminal?

2019-01-10 12:17发布

This question already has an answer here:

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?

4条回答
够拽才男人
2楼-- · 2019-01-10 13:09

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

PACKAGE=com.examply.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
查看更多
来,给爷笑一个
3楼-- · 2019-01-10 13:10

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.

查看更多
Lonely孤独者°
4楼-- · 2019-01-10 13:20

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
查看更多
淡お忘
5楼-- · 2019-01-10 13:22

You can Start the android Service by this command.

adb shell am startservice -n packageName/.ServiceClass

查看更多
登录 后发表回答