How to retrieve path to ADB in build.gradle

2020-05-25 19:05发布

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?

6条回答
可以哭但决不认输i
2楼-- · 2020-05-25 19:26

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'
    }
}
查看更多
倾城 Initia
3楼-- · 2020-05-25 19:27
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb
查看更多
戒情不戒烟
4楼-- · 2020-05-25 19:27

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.

查看更多
贪生不怕死
5楼-- · 2020-05-25 19:30

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

查看更多
聊天终结者
6楼-- · 2020-05-25 19:32

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()
查看更多
来,给爷笑一个
7楼-- · 2020-05-25 19:36

we can get if from android extension.

android.adbExe

查看更多
登录 后发表回答