92

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?

Paul Verest
  • 60,022
  • 51
  • 208
  • 332
Matt Whetton
  • 6,616
  • 5
  • 36
  • 57
  • 1
    `gradle tasks` is helpful to see the out of the box tasks - which includes installing (but not starting as stated below) – Dori Mar 18 '14 at 11:26
  • Command line is not IDE specific, .i.e. this way would work from Android Studio or Eclipse-based IDE – Paul Verest Apr 08 '14 at 02:38

9 Answers9

101
$ gradle installDebug

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

rafaello
  • 2,385
  • 1
  • 18
  • 16
76

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'

Roman K
  • 3,309
  • 1
  • 33
  • 49
  • 1
    In addition, the last parameter should be `'com.your.packagename/.path.relative.to.your.packagename.MyActivity'` instead of `'com.example/.MyActivity'` if your Activity is not in the root of your package. – Light Jan 03 '15 at 10:06
68

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
rps_deepan
  • 716
  • 4
  • 5
  • 7
    alias arun="gradle installDebug; adb shell am start -n com.example.app/.activity.MainActivity" – Dominic Nov 01 '13 at 15:53
  • I cannot select installDebug in gradle. There is no suitable task. Has it been renamed? – Jonas Gröger Nov 24 '13 at 19:50
  • 1
    @JonasGröger it seems to have been renamed to installDefaultFlavorDebug – durka42 Dec 30 '13 at 21:58
  • @rps_deepan Could you rename the task because it has been renamed? – Jonas Gröger Dec 31 '13 at 15:59
  • 7
    `alias arun="./gradlew installDebug && adb shell am start -n com.example.package/.LauncherActivity"` #Runs step2 Only when Step 1 is success – Thamme Gowda Jan 08 '14 at 07:29
  • It's worth noting, the task may be different if you have build variants (as shown in the lower left of Android Studio). For me the command was `./gradlew installDevDebug`. This could change the package too: `am start -n com.example.example_dev/com.example.example.MainActivity`. This stumped me for a while. – bmaddy Nov 18 '15 at 23:41
7

One line sentence:

Build project & Install generated apk & Open app on device

$ ./gradlew installDebug && adb shell am start -n com.example/.activities.MainActivity
João M
  • 1,939
  • 21
  • 14
7

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.

ms2r
  • 170
  • 3
  • 17
4

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.
0xcaff
  • 13,085
  • 5
  • 47
  • 55
  • You could instead execute `adb shell am start your.package.name.debugsuffix\.Activity` – Vikram Bodicherla Sep 18 '16 at 13:27
  • But what if you don't know your activity's name? – 0xcaff Sep 19 '16 at 00:16
  • 1
    Then you shouldn't be writing the build file. – kevr Dec 01 '16 at 03:51
  • 3
    I think its a really bad idea for the build file know explicitly which Activity to run...seems like something separation of concerns goes against. One simple reason - if you rename/move your Activity file, this now requires a build file change. Additionally, intents seem like a better api to run against than a file - I could write a gradle extension that adds default intents and ways to run them, version it, and never need to know anybody's class names. Hard coding is never the right answer. – smaudet Mar 27 '17 at 03:43
  • 1
    This answer also lacks a way to find the root package - I'd like to get rid of the package name too - e.g. discover it from the manifest instead. – smaudet Mar 27 '17 at 03:50
  • @0xcaff using `android.intent.category.LAUNCHER 1` you don't need to know the activity. – m3nda Jul 05 '17 at 19:04
4

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]
alexander.polomodov
  • 5,396
  • 14
  • 39
  • 46
Goffity
  • 142
  • 6
2
task appStart(type: Exec, dependsOn: 'installDebug') {
    commandLine android.adbExe, 'shell', 'am', 'start', '-n', 'com.example/.MyActivity'
}
Arash Hatami
  • 5,297
  • 5
  • 39
  • 59
hoot
  • 1,215
  • 14
  • 15
2

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.

maclir
  • 3,218
  • 26
  • 39