代码示例:
在 build.gradle 文件中添加以下代码:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath "com.android.tools.build:gradle:3.6.3"
}
}
// Adds the 'android' command line tool to your PATH
def androidHome = System.getenv("ANDROID_HOME")
def cmdlineTools = "${androidHome}/cmdline-tools/latest/bin"
def sdkmanager = "${cmdlineTools}/sdkmanager"
def avdmanager = "${cmdlineTools}/avdmanager"
// Add the directory containing the sdkmanager and avdmanager scripts to your PATH
def addCmdlineToolsToSystemPath() {
if (System.getenv("ANDROID_HOME") == null) {
throw new GradleException("ANDROID_HOME environment variable is not set. Please set it to the path of your android SDK.")
}
def path = System.getenv("PATH")
path = "$cmdlineTools:$path"
println "Adding ${cmdlineTools}/* to PATH"
println "PATH is now ${path}"
System.env.put("PATH", path)
}
// Run the addCmdlineToolsToSystemPath function when Gradle starts
gradle.startParameter.continueOnFailure = true
addCmdlineToolsToSystemPath()
此代码片段在 Gradle 构建期间将 cmdline-tools 组件添加到您的系统 PATH 中,从而使您能够直接调用 sdkmanager 和 avdmanager 脚本。要使用此代码,请将其添加到您的项目的 build.gradle 文件中。