How to run (not only install) an Android app using an .apk file?

Is there any command in cmd.exe that would allow me to run the main action of a specific Android application using this application's .apk file. Please note that I know this command, which only installs the Android app:

 adb install myapp.apk 

This command will only install myapp on the emulator, and I have to manually start this application from the emulator (with one click on its icon).

What I want to do is use a command that not only installs the application, but also starts its main action (note that I only have its .apk file, so I don’t know what the package name or any activity name is )

+53
android android-install-apk
May 24 '11 at 10:46
source share
7 answers

You cannot install and run at a time - but you can use adb to run an already installed application. Use the adb shell to start intentional intent - you will need to use the correct intent for your application. A few examples:

 adb shell am start -a android.intent.action.MAIN -n com.android.settings/.Settings 

Settings will start and

 adb shell am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity 

will launch the browser. If you want to specify a browser on a specific page, do it

 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 activity in the APK, do it

 aapt d xmltree <path to apk> AndroidManifest.xml 

output content will contain the following section:

  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) 

This tells you the name of the main action (MainWindow), and now you can run

 adb shell am start -a android.intent.action.MAIN -n com.anonymous/.MainWindow 
+81
May 24 '11 at 10:53
source share

if you are looking for the equivalent of "adb run myapp.apk"

you can use the script specified in this,

(linux and mac only - possibly with cygwin on windows)

Linux / mac users can also create a script to run apk with something like the following:

create a file called "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 doable.

now you can simply:

adb-run.sh myapp.apk

The advantage here is that you do not need to know the name of the package or the name of the program to run. Similarly, you can create "adb-uninstall.sh myapp.apk"

Note. This requires that you have access to your path. You can find it in the new build tools folder in the SDK

+12
Jun 25 '13 at 6:13
source share

When you launch the application from the graphical user interface, adb logcat can show you the corresponding action / category / component:

$ adb logcat
[...]
I / ActivityManager (1607): START {act = android.intent.action.MAIN cat = [android.intent.category.LAUNCHER] flg = 0x10200000 cmp = com.android.browser / .BrowserActivity} from pid 1792
[...]

+6
Jan 31 '12 at 9:27
source share

This is a solution in a 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. Run the application

 pkg_name=$name adb shell monkey -p "${pkg_name}" -c android.intent.category.LAUNCHER 1 
+5
Dec 04 '17 at 9:34 on
source share

I put this in my makefile, right next line after adb install ...

 adb shell monkey -p `cat .identifier` -c android.intent.category.LAUNCHER 1 

To do this, the file must have a .identifier file with the application package identifier, for example com.company.ourfirstapp

No need to search for the name of the activity.

+2
Jul 24 '15 at 0:56
source share

I created terminal aliases to install and run apk using a single command.

 // I use ZSH, here is what I added to my .zshrc file (config file) // at ~/.zshrc // If you use bash shell, append it to ~/.bashrc # Have the adb accessible, by including it in the PATH export PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:path/to/android_sdk/platform-tools/" # Setup your Android SDK path in ANDROID_HOME variable export ANDROID_HOME=~/sdks/android_sdk # Setup aapt tool so it accessible using a single command alias aapt="$ANDROID_HOME/build-tools/27.0.3/aapt" # Install APK to device # Use as: apkinstall app-debug.apk alias apkinstall="adb devices | tail -n +2 | cut -sf 1 | xargs -IX adb -s X install -r $1" # As an alternative to apkinstall, you can also do just ./gradlew installDebug # Alias for building and installing the apk to connected device # Run at the root of your project # $ buildAndInstallApk alias buildAndInstallApk='./gradlew assembleDebug && apkinstall ./app/build/outputs/apk/debug/app-debug.apk' # Launch your debug apk on your connected device # Execute at the root of your android project # Usage: launchDebugApk alias launchDebugApk="adb shell monkey -p 'aapt dump badging ./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name' | cut -d \' -f 2' 1" # ------------- Single command to build+install+launch apk------------# # Execute at the root of your android project # Use as: buildInstallLaunchDebugApk alias buildInstallLaunchDebugApk="buildAndInstallApk && launchDebugApk" 

Note. Here I am ./app/build/outputs/apk/debug/app-debug.apk , install and run debug apk, which is usually located on the path: ./app/build/outputs/apk/debug/app-debug.apk , when this command is executed from the root of the project

If you want to install and run any other apk, just replace the path for debug apk with the path of your own apk

Here is the gist for this. I created this because I had problems working with an Android Studio installation, reaching approximately 28 minutes, so I switched to terminal builds, which were about 3 minutes. You can learn more about it here.

Explanation:

One alias that I think needs explanation is the alias launchDebugApk . Here's how it is broken:

aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name partial aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name aapt dump badging./app/build/outputs/apk/debug/app-debug.apk | grep -e 'package: name basically uses the aapt tool to extract the package name from apk.

Next, this is the command: adb shell monkey -p com.package.name 1 , which basically uses the monkey tool to open the default run action of the installed application on the connected device. The com.package.name part com.package.name replaced by our previous team, which takes care of getting the package name from apk.

0
Apr 15 '18 at 8:08
source share

First install the application:

 adb install -r path\ProjectName.apk 

The great thing about -r is that it works even if it is not already installed.

To start MainActivity, you can run it as follows:

adb shell am start -n com.other.ProjectName/.MainActivity

0
Aug 07 '19 at 11:06 on
source share



All Articles