17

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?

Dima
  • 1,490
  • 16
  • 25

6 Answers6

34

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()
Kevin Brotcke
  • 3,765
  • 26
  • 34
  • 1
    Yet one more way is via `android.plugin.getSdkInfo().getAdb().toString()` (which is what `getAdbExe()` does internally as I just learned). – sschuberth Mar 02 '15 at 13:49
  • I found three or four different ways when browsing the source and picked the one that looked the most succinct. They all looked like references to the same core logic so either of these examples should be fine. – Kevin Brotcke Mar 03 '15 at 14:35
  • This used to work great until gradle android plugin 1.1.0, but now it doesn't (I get gradle error`Could not find property 'plugin' on com.android.build.gradle.AppExtension_Decorated@1f6dc928.`). Tapemaster's answer still works however. – Denys Kniazhev-Support Ukraine Mar 07 '15 at 10:22
  • @Denis Thanks for pointing that out. I've updated my answer for the newest version of Android Gradle. – Kevin Brotcke Mar 09 '15 at 15:52
  • 4
    `getAdb` is deprecated, I'm using `android.getAdbExecutable().absolutePath` – Androiderson Oct 26 '17 at 17:43
  • def adb = android.sdkDirectory.path + "/platform-tools/adb" Highly recommend this over other answers here. – Zeek Aran May 08 '18 at 19:47
20

The problem was solved.
The variable must contain

def adb = "$System.env.ANDROID_HOME/platform-tools/adb"

And complete task looks like


task run(type: Exec) {
    def adb = "$System.env.ANDROID_HOME/platform-tools/adb"
    commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}

UPD
Another way without using ANDROID_HOME


task run(type: Exec) {
    def rootDir = project.rootDir
    def localProperties = new File(rootDir, "local.properties")
    if (localProperties.exists()) {
        Properties properties = new Properties()
        localProperties.withInputStream { 
            instr -> properties.load(instr)
        }
        def sdkDir = properties.getProperty('sdk.dir')
        def adb = "$sdkDir/platform-tools/adb"
        commandLine "$adb", 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
    }
}
Dima
  • 1,490
  • 16
  • 25
  • 1
    Kevin's and Tapemaster's answers below suggest much easier ways which also work well. – Denys Kniazhev-Support Ukraine Jan 06 '15 at 19:53
  • 1
    The key point to this answer is the "$adb". The syntax highlighting on SO doesn't show it, but Gradle recognizes that `adb` is a variable, allowing the 'commandLine' function to work properly. Combining that bit with Kevin Brotcke's answer to get the path to ADB is an excellent solution. ```def adb = android.getAdbExe().toString()``` ```commandLine "$adb", 'shell', 'am', 'start', 'com.example.app``` – Sean Beach Jan 05 '16 at 19:41
  • $System.env.ANDROID_HOME gives me null on my computer, while your updated version works for me but not my build servers. – Zeek Aran May 08 '18 at 15:50
  • This is what ended up working for me and is much simpler too: def adb = android.sdkDirectory.path + "/platform-tools/adb" – Zeek Aran May 08 '18 at 19:46
7
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb
Tapemaster
  • 487
  • 5
  • 9
  • Worked for me with gradle android plugin 1.1.2 – Antonio Jose Mar 09 '15 at 03:34
  • The above is much better than relying on platform/OS dependencies to resolve the location of the actual ADB executable being used. It worked liked a charm for me. – Andries Apr 08 '16 at 16:08
  • Doesn't work for me: Error:(404, 0) No such property: sdkHandler for class: com.android.build.gradle.AppPlugin – Zeek Aran May 08 '18 at 15:48
1

In Windows you can just register an application path for adb.exe with the following .reg file:

REGEDIT4

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\adb.exe]
@="D:\\android\\android-studio\\sdk\\platform-tools\\adb.exe"
"Path"="D:\\android\\android-studio\\sdk\\platform-tools"

and just keep your original commandline

Alex P.
  • 30,437
  • 17
  • 118
  • 169
  • It works. You probably just did not restart your system after editing registry. – Alex P. Jan 23 '14 at 14:17
  • I tried with and without restarting. Maybe i did smth wrong, but it doesn't matter. I needed a more universal way for easier use with version control systems. – Dima Jan 23 '14 at 15:25
0

My default solution for this issue is to add adb to your path variable so you can use the adb command from every path.
You can set it e.g. from the console like this:

set path=%path%;x:\path\to\adb

Alternative you can set it via the UI. See also this explanation on java.com.

rekire
  • 47,260
  • 30
  • 167
  • 264
0

we can get if from android extension.

android.adbExe

hoot
  • 1,215
  • 14
  • 15