How to run (not only install) an android application using .apk file?

前端 未结 7 1330
孤独总比滥情好
孤独总比滥情好 2020-11-29 18:04

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 appl

相关标签:
7条回答
  • 2020-11-29 18:43

    When you start the app from the GUI, adb logcat might 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
    [...]

    0 讨论(0)
  • 2020-11-29 18:45

    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
    
    0 讨论(0)
  • 2020-11-29 18:45

    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

    0 讨论(0)
  • 2020-11-29 18:53

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

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

    For this to work there must be a .identifier file with the app's bundle identifier in it, like com.company.ourfirstapp

    No need to hunt activity name.

    0 讨论(0)
  • 2020-11-29 18:54

    I created terminal aliases to install and run an 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 -I X 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 building, installing and launching the debug apk which is usually in the path: ./app/build/outputs/apk/debug/app-debug.apk, when this command is executed from the root of the project

    If you would like to install and run any other apk, simply replace the path for debug apk with path of your own apk

    Here is the gist for the same. I created this because I was having trouble working with Android Studio build reaching around 28 minutes, so I switched over to terminal builds which were around 3 minutes. You can read more about this here

    Explanation:

    The one alias that I think needs explanation is the launchDebugApk alias. Here is how it is broken down:

    The part 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 the apk.

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

    0 讨论(0)
  • 2020-11-29 19:01

    First to install your app:

    adb install -r path\ProjectName.apk
    

    The great thing about the -r is it works even if it wasn’t already installed.

    To launch MainActivity, so you can launch it like:

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

    0 讨论(0)
提交回复
热议问题