如何在build.gradle中检索ADB的路径 [英] How to retrieve path to ADB in build.gradle
本文介绍了如何在build.gradle中检索ADB的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过 gradle
任务
来启动应用程序。
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'
}
但是这段代码不起作用,我得到错误:<
启动过程中出现问题'command'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"
完成的任务看起来像
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
不使用 ANDROID_HOME
的另一种方法
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'
}
}
这篇关于如何在build.gradle中检索ADB的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文