Can you deploy to a device via Gradle from the command line

前端 未结 9 979
借酒劲吻你
借酒劲吻你 2020-12-07 10:14

What the question says really - can you issue any commands directly to gradlew via the command line to build, package and deploy to a device?

相关标签:
9条回答
  • 2020-12-07 10:51

    1. Build project, install generated apk to device

    # at the root dir of project
    $ gradle installDebug
    

    2. Open app on device

    $ adb shell am start -n yourpackagename/.activityname
    
    0 讨论(0)
  • 2020-12-07 10:51

    There are three commands to accomplish this:

    1. ./gradlew assembleDebug #To build the project

    2. adb install -r ./app/build/outputs/apk/app-debug.apk #To install it to the device

    3. adb shell am start -n $PACKAGE/$PACKAGE.$ACTIVITY #To launch the application in the device, where $PACKAGE is the development package and $ACTIVITY is the activity to be launched (the launcher activity).

    I've been writing a bash script to do this, with other few features.

    0 讨论(0)
  • 2020-12-07 10:52
    $ gradle installDebug
    

    This will push the debug build apk to device, but you have to manually start the application.

    0 讨论(0)
  • 2020-12-07 11:00

    Build -> uninstall old verion -> install new version -> run application.

    echo "Build application" && ./gradlew clean build && 
    echo "Uninstall application" && adb uninstall [application package] && 
    echo "Install application" && adb -d install app/build/outputs/apk/<build type>/[apk name].apk echo "Run application" && 
    adb shell am start -n [application package]/.[application name]
    

    Or if you want install and run application in debug type.

    ./gradlew installDebug && adb shell am start -n [application package]/.[application name]
    
    0 讨论(0)
  • 2020-12-07 11:02

    Since you are using Gradle, you could simple add your own task in build.gradle

    task appStart(type: Exec, dependsOn: 'installDebug') {
        // linux 
        commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
    
        // windows
        // commandLine 'cmd', '/c', 'adb', 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'      
    }
    

    then call it in your project root

    $ gradle appStart

    Update:

    If you are using applicationIdSuffix ".debug", add .debug to the appId only but leave the activity untouched:

    'com.example.debug/com.example.MyActivity'

    0 讨论(0)
  • 2020-12-07 11:02
    task appStart(type: Exec, dependsOn: 'installDebug') {
        commandLine android.adbExe, 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
    }
    
    0 讨论(0)
提交回复
热议问题