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

前端 未结 7 1371
孤独总比滥情好
孤独总比滥情好 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: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.

提交回复
热议问题