Skip to content

Instantly share code, notes, and snippets.

@android10
Last active August 29, 2023 14:31
Show Gist options
  • Save android10/0b291129b4aec15b262d603612499e37 to your computer and use it in GitHub Desktop.
Save android10/0b291129b4aec15b262d603612499e37 to your computer and use it in GitHub Desktop.
Compile and launch android app from the command line.
task deployDebug(type: Exec, dependsOn: 'app:installDebug') {
def rootDir = project.rootDir
def localProperties = new File(rootDir, "local.properties")
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream {
inputStream -> properties.load(inputStream)
}
def sdkDir = properties.getProperty('sdk.dir')
def adb = "$sdkDir/platform-tools/adb"
commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.package/com.package.YourMainActivity'
}
}
@android10
Copy link
Author

You can also create a Gradle run configuration on Android Studio/Intellij.

gradle_run_config

@adrianbartnik
Copy link

Instead of doing all this $adb parsing, you can write
commandLine android.getAdbExe(), 'shell', 'am', 'start', '-n', "${android.defaultConfig.applicationId}/.MainActivity"

@android10
Copy link
Author

@whoww the problem is that if you have a project with multiple modules.
My solution is for the main build.gradle file where you do not have any android plugin get the adb location. 😄

@jlobos
Copy link

jlobos commented Jun 28, 2018

task start(type: Exec) {
    dependsOn 'installDebug'
    commandLine 'adb', 'shell', 'am', 'start', '-n', "$android.defaultConfig.applicationId/.MainActivity"
}

@cescoferraro
Copy link

@jlobos thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment