如何在 build.gradle 中检索 ADB 的路径 [英] How to retrieve path to ADB in build.gradle

查看:70
本文介绍了如何在 build.gradle 中检索 ADB 的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过 gradle task 启动应用程序.

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'
}

但是此代码不起作用,我收到错误消息:
启动进程'命令'adb''时出现问题

但是,当我明确指定 adb 的路径时,应用程序将启动.

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'
}

那么如何获取包含路径的变量并将其传输到 commandLine?

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

推荐答案

问题解决了.
变量必须包含

The problem was solved.
The variable must contain

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

完成任务的样子


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
另一种不使用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'
    }
}

这篇关于如何在 build.gradle 中检索 ADB 的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆