我想为Gradle指定一系列可能的Android构建工具版本。
在build.gradle中指定buildToolsVersion时是否可以使用+?
像buildToolsVersion 23+
?
或者编译依赖项可能包含这样的范围:
compile group: 'log4j', name: 'log4j', version: '[1.2.12,1.2.17]'
答案 0 :(得分:5)
好问题。
是的!这是可能的。我的回答大量使用此代码段:https://github.com/aayvazyan-tgm/autodetect_android_sdk_and_buildTools/blob/master/sdktools.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 23
buildToolsVersion getBuildVersionFromRange("23.0.2", "25.1.3")
...
}
.......
//
// get the installed SDK and build tools version
// based on: http://www.egeek.me/2013/12/07/gradle-auto-detect-android-sdk-and-build-tools-versions/
// modified by Ari Ayvazyan
import org.codehaus.groovy.runtime.StackTraceUtils
public String androidSDKDir() {
def sdkDir = android.getSdkDirectory().toString()
return sdkDir
}
public String[] buildToolsAvailable() {
println "sdk dir: " + androidSDKDir()
def buildToolsDir = new File(androidSDKDir(), "build-tools")
String[] collection = buildToolsDir.list([accept:{ d, f-> !f.contains("android") }] as FilenameFilter)
for (int i = 0; i < collection.length; i++) {
String s = collection[i]
collection[i]=s.replace("build-tools-","")
}
collection.sort { a, b -> b <=> a }
}
public String getBuildVersionFromRange(String defaultBuildTools, String maxBuildTools) {
def buildToolsVersions = buildToolsAvailable()
List maxBuild = maxBuildTools.tokenize('.')
for (int i = 0; i < buildToolsVersions.length; i++) {
def buildToolsToCheck = buildToolsVersions[i]
List currentBuild = buildToolsToCheck.tokenize('.')
boolean isPassing = true;
for (int j = 0; j < maxBuild.size(); j++) {
if (maxBuild.get(j) < currentBuild.get(j)) {
isPassing = false;
break;
}
}
if (isPassing) {
return buildToolsToCheck;
}
}
return defaultBuildTools;
}
import org.apache.tools.ant.taskdefs.condition.Os
public boolean isWindows() {
if (Os.isFamily(Os.FAMILY_WINDOWS)) {
return true
}
return false
}
当然,getBuildVersionFromRange
的代码可以改进。例如,现在,第一个参数是后备值,但可以将其重写为范围的下边框,例如。
我希望,这有帮助