How to retrieve path to ADB in build.gradle

Dima picture Dima · Jan 22, 2014 · Viewed 8.3k times · Source

I trying to start application via gradle task.


task runDebug(dependsOn: ['installDebug', 'run']) {
}

task run(type: Exec) {
commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

But this code don't work and i get error:
a problem occurred starting process 'command 'adb''

However, when i specify the path to adb explicitly, application is started.


task run(type: Exec) {
    commandLine 'D:\\android\\android-studio\\sdk\\platform-tools\\adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

So how can i get a variable which contains the path and transfer it to commandLine?

Answer

Kevin Brotcke picture Kevin Brotcke · Nov 6, 2014

You should use the logic that the Android Gradle plugin already has for finding the SDK and adb locations to ensure your script is using the same ones.

# Android Gradle >= 1.1.0
File sdk = android.getSdkDirectory()
File adb = android.getAdbExe()

# Android Gradle < 1.1.0
File sdk = android.plugin.getSdkFolder()
File adb = android.plugin.extension.getAdbExe()