开发Hybrid应用,一般都会和Cordova(Phonegap)打交道,Cordova环境没配置好会影响开发,其中可能会遇到下面的问题:
cordova build android提示"No installed build tools found. Please install the Android build tools version ",
解决方法:把android 的sdk目录\build-tools里的文件夹改成小数就好了,如把android-22改成22(在sdk几个文件夹下,android-22是常见的目录名称,所以不会留意到这个原因导致异常)。
至于解决过程有兴趣继续往下看:
这段异常提示来自哪里呢?
它在CordovaLib/cordova.gradle文件里面(源文件64行,这片段的27行):
String[] getAvailableBuildTools() {
def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDir.list()
.findAll { it ==~ /[0-9.]+/ }
.sort { a, b -> compareVersions(b, a) }
}
String doFindLatestInstalledBuildTools(String minBuildToolsVersion) {
def availableBuildToolsVersions
try {
availableBuildToolsVersions = getAvailableBuildTools()
} catch (e) {
println "An exception occurred while trying to find the Android build tools."
throw e
}
if (availableBuildToolsVersions.length > 0) {
def highestBuildToolsVersion = availableBuildToolsVersions[0]
if (compareVersions(highestBuildToolsVersion, minBuildToolsVersion) < 0) {
throw new RuntimeException(
"No usable Android build tools found. Highest installed version is " +
highestBuildToolsVersion + "; minimum version required is " +
minBuildToolsVersion + ".")
}
highestBuildToolsVersion
} else {
throw new RuntimeException(
"No installed build tools found. Please install the Android build tools version " +
minBuildToolsVersion + " or higher.")
}
}
这是Groovy语言,看代码,要提示这个异常,要达到下面条件:
availableBuildToolsVersions.length<=0
而这个变量来自函数getAvailableBuildTools(),函数意思是获取可用目录,看这个函数的实现,它是在androd的SDK目录/build-tools下查找所有文件夹,然后通过一个findAll正则表达式匹配:
findAll { it ==~ /[0-9.]+/ }
其中==~:用法跟=~类似,只是特指精确匹配,及左边的和右边的模式进行匹配。而右边的正则表达式意思是(0到9和.号的组合)的1到多个,也就是说没有字母和其它字符,于是答案就揭晓了