How to retrieve path to ADB in build.gradle
Asked Answered
P

6

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?

Psychotomimetic answered 22/1, 2014 at 16:26 Comment(0)
P
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'
    }
}
Psychotomimetic answered 23/1, 2014 at 7:40 Comment(4)
Kevin's and Tapemaster's answers below suggest much easier ways which also work well.Caller
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.appSob
$System.env.ANDROID_HOME gives me null on my computer, while your updated version works for me but not my build servers.Mismatch
This is what ended up working for me and is much simpler too: def adb = android.sdkDirectory.path + "/platform-tools/adb"Mismatch
R
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()
Ramos answered 6/11, 2014 at 3:9 Comment(6)
Yet one more way is via android.plugin.getSdkInfo().getAdb().toString() (which is what getAdbExe() does internally as I just learned).Englishism
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.Ramos
This used to work great until gradle android plugin 1.1.0, but now it doesn't (I get gradle errorCould not find property 'plugin' on com.android.build.gradle.AppExtension_Decorated@1f6dc928.). Tapemaster's answer still works however.Caller
@Denis Thanks for pointing that out. I've updated my answer for the newest version of Android Gradle.Ramos
getAdb is deprecated, I'm using android.getAdbExecutable().absolutePathStinkwood
def adb = android.sdkDirectory.path + "/platform-tools/adb" Highly recommend this over other answers here.Mismatch
P
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'
    }
}
Psychotomimetic answered 23/1, 2014 at 7:40 Comment(4)
Kevin's and Tapemaster's answers below suggest much easier ways which also work well.Caller
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.appSob
$System.env.ANDROID_HOME gives me null on my computer, while your updated version works for me but not my build servers.Mismatch
This is what ended up working for me and is much simpler too: def adb = android.sdkDirectory.path + "/platform-tools/adb"Mismatch
R
7
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb
Raised answered 13/11, 2014 at 15:23 Comment(3)
Worked for me with gradle android plugin 1.1.2Phonography
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.Carranza
Doesn't work for me: Error:(404, 0) No such property: sdkHandler for class: com.android.build.gradle.AppPluginMismatch
J
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

Jansson answered 22/1, 2014 at 16:32 Comment(2)
It works. You probably just did not restart your system after editing registry.Jansson
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.Psychotomimetic
D
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.

Dimetric answered 28/9, 2015 at 9:54 Comment(0)
I
0

we can get if from android extension.

android.adbExe

Inconsistency answered 15/2, 2016 at 11:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.