简体   繁体   English

如何在build.gradle中检索ADB的路径

[英]How to retrieve path to ADB in build.gradle

I trying to start application via gradle task . 我试图通过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. 但是,当我明确指定adb的路径时,将启动应用程序。


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 ? 那么我怎么能得到一个包含路径的变量并将其传递给commandLine

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插件已有的逻辑来查找SDK和adb位置,以确保您的脚本使用相同的脚本。

# 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()

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 UPD
Another way without using ANDROID_HOME 另一种不使用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'
    }
}
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb

In Windows you can just register an application path for adb.exe with the following .reg file: 在Windows中,您只需使用以下.reg文件注册adb.exeapplication path

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 并保持原来的命令行

My default solution for this issue is to add adb to your path variable so you can use the adb command from every path. 我对此问题的默认解决方案是将adb添加到路径变量中,以便您可以使用每个路径中的adb命令。
You can set it eg from the console like this: 您可以在控制台中设置它,如下所示:

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

Alternative you can set it via the UI. 您也可以通过UI进行设置。 See also this explanation on java.com . 另请参阅java.com上的此解释。

we can get if from android extension. 我们可以从android扩展获得。

android.adbExe android.adbExe

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM