[英]How to retrieve path to ADB in build.gradle
我试图通过gradle
task
启动应用程序。
task runDebug(dependsOn: ['installDebug', 'run']) {
}
task run(type: Exec) {
commandLine 'adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}
但是这段代码不起作用,我得到错误:
a problem occurred starting process 'command 'adb''
但是,当我明确指定adb的路径时,将启动应用程序。
task run(type: Exec) {
commandLine 'D:\\android\\android-studio\\sdk\\platform-tools\\adb', 'shell', 'am', 'start', '-n', 'com.example.myexample/.ui.SplashScreenActivity'
}
那么我怎么能得到一个包含路径的变量并将其传递给commandLine
?
您应该使用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()
问题解决了。
变量必须包含
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'
}
}
def androidPlugin = project.plugins.findPlugin("android")
def adb = androidPlugin.sdkHandler.sdkInfo?.adb
在Windows中,您只需使用以下.reg
文件注册adb.exe
的application 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"
并保持原来的命令行
我对此问题的默认解决方案是将adb添加到路径变量中,以便您可以使用每个路径中的adb命令。
您可以在控制台中设置它,如下所示:
set path=%path%;x:\path\to\adb
您也可以通过UI进行设置。 另请参阅java.com上的此解释。
我们可以从android扩展获得。
android.adbExe
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.