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

AndroidGradleAndroid StudioGradlew

Android Problem Overview


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?

Android Solutions


Solution 1 - Android

$ gradle installDebug

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

Solution 2 - Android

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 - Android

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 - Android

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 - Android

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.

Solution 6 - Android

A more flexible way to do it is by using monkey:

task runDebug (type: Exec, dependsOn: 'installDebug') {
    commandLine android.getAdbExe().toString(), "shell",
        "monkey",
        "-p", "your.package.name.debugsuffix",
        "-c", "android.intent.category.LAUNCHER", "1"
}

Some advantages to this method:

  • getAdbExe doesn't require adb to be on the path and uses the adb version from the sdk pointed to in local.properties.
  • The monkey tool allows you to send a launcher intent, so you aren't required to know the name of your activity.

Solution 7 - Android

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]

Solution 8 - Android

task appStart(type: Exec, dependsOn: 'installDebug') {
    commandLine android.adbExe, 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
}

Solution 9 - Android

I wrote this task to be able to install and also open the application on the device. Since I had multiple buildTypes and flavors with different application ids, it was not feasible to hard code the package name. So I wrote it like this instead:

android.applicationVariants.all { variant ->
    task "open${variant.name.capitalize()}" {
        dependsOn "install${variant.name.capitalize()}"

        doLast {
            exec {
                commandLine "adb shell monkey -p ${variant.applicationId} -c android.intent.category.LAUNCHER 1".split(" ")
            }
        }
    }
}

This would give you open{variant} for every install{variant} task you already have.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt WhettonView Question on Stackoverflow
Solution 1 - AndroidrafaelloView Answer on Stackoverflow
Solution 2 - AndroidRoman KView Answer on Stackoverflow
Solution 3 - Androidrps_deepanView Answer on Stackoverflow
Solution 4 - AndroidJoão MView Answer on Stackoverflow
Solution 5 - Androidms2rView Answer on Stackoverflow
Solution 6 - Android0xcaffView Answer on Stackoverflow
Solution 7 - AndroidGoffityView Answer on Stackoverflow
Solution 8 - AndroidhootView Answer on Stackoverflow
Solution 9 - AndroidmaclirView Answer on Stackoverflow