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

前端 未结 9 980
借酒劲吻你
借酒劲吻你 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 11:03

    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.
    0 讨论(0)
  • 2020-12-07 11:06

    One line sentence:

    Build project & Install generated apk & Open app on device

    $ ./gradlew installDebug && adb shell am start -n com.example/.activities.MainActivity
    
    0 讨论(0)
  • 2020-12-07 11:07

    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.

    0 讨论(0)
提交回复
热议问题