How to run (not only install) an android application using .apk file?
Is there any command on cmd.exe
that would allow me to start the main activity of a particular android application using the .apk
file of that application. Please note that I know this command which only installs an android application:
adb install myapp.apk
This command will only install myapp
onto the emulator and I have to manually run this application from the emulator (by performing single click on its icon).
What I want to do is use a command which not only installs the application but also starts its main activity (please note that I have only its .apk
file so I don't know what the package name or any activity name is).
You can't install and run in one go - but you can certainly use adb to start your already installed application. Use adb shell am start to fire an intent - you will need to use the correct intent for your application though. A couple of examples:
adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings
will launch Settings, and
adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity
will launch the Browser. If you want to point the Browser at a particular page, do this
adb shell am start -a android.intent.action.VIEW -n com.android.browser/.BrowserActivity http://www.google.co.uk
If you don't know the name of the activities in the APK, then do this
aapt d xmltree <path to apk> AndroidManifest.xml
the output content will includes a section like this:
E: activity (line=32)
A: android:theme(0x01010000)=@0x7f080000
A: android:label(0x01010001)=@0x7f070000
A: android:name(0x01010003)="com.anonymous.MainWindow"
A: android:launchMode(0x0101001d)=(type 0x10)0x3
A: android:screenOrientation(0x0101001e)=(type 0x10)0x1
A: android:configChanges(0x0101001f)=(type 0x11)0x80
E: intent-filter (line=33)
E: action (line=34)
A: android:name(0x01010003)="android.intent.action.MAIN"
XE: (line=34)
That tells you the name of the main activity (MainWindow), and you can now run
adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow
if you're looking for the equivalent of "adb run myapp.apk"
you can use the script shown in this answer
(linux and mac only - maybe with cygwin on windows)
linux/mac users can also create a script to run an apk with something like the following:
create a file named "adb-run.sh" with these 3 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 aapt in your path. You can find it under the new build tools folder in the SDK
This is a solution in shell script:
apk="$apk_path"
1. Install apk
adb install "$apk"
sleep 1
2. Get package name
pkg_info=`aapt dump badging "$apk" | head -1 | awk -F " " '{print $2}'`
eval $pkg_info > /dev/null
3. Start app
pkg_name=$name
adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1