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

Solution 1:

$ gradle installDebug

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

Solution 2:

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'

Solution 3:

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

Solution 4:

One line sentence:

Build project & Install generated apk & Open app on device

$ ./gradlew installDebug && adb shell am start -n com.example/.activities.MainActivity

Solution 5:

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.