blob: 40529d3f3bf83e18de068223704b542e0a84182c [file] [log] [blame]
/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/
int[] getAvailableSdks() {
def availableSdks = new ByteArrayOutputStream()
exec {
commandLine 'android', 'list', 'target', '--compact'
standardOutput = availableSdks
}
availableSdks
.toString()
.tokenize('\n')
.findAll { it ==~ /android-(\d+).*/ }
.collect { (it =~ /android-(\d+).*/)[0][1].toInteger() }
.sort( { a, b -> b <=> a } )
}
int highestSdkAvailable(int minSdkVersion) {
def availableSdks
try {
availableSdks = getAvailableSdks()
} catch (e) {
println "An exception occurred while trying to find the Android SDK."
throw e
}
if (availableSdks.length > 0) {
def highestSdk = availableSdks[0]
if (highestSdk < minSdkVersion) {
throw new RuntimeException(
"No usable Android SDK found. Highest installed version is " +
highestSdk + "; minimum version required is " + minSdkVersion + ".")
}
highestSdk
} else {
throw new RuntimeException(
"No installed SDKs found. Please install the Android SDK version " +
minSdkVersion + " or higher.")
}
}
String[] getAvailableBuildTools() {
def buildToolsDir = new File(getAndroidSdkDir(), "build-tools")
buildToolsDir.list()
.findAll { it ==~ /[0-9.]+/ }
.sort { a, b -> compareVersions(b, a) }
}
String latestBuildToolsAvailable(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.")
}
}
// Return the first non-zero result of subtracting version list elements
// pairwise. If they are all identical, return the difference in length of
// the two lists.
int compareVersionList(Collection aParts, Collection bParts) {
def pairs = ([aParts, bParts]).transpose()
pairs.findResult(aParts.size()-bParts.size()) {it[0] - it[1] != 0 ? it[0] - it[1] : null}
}
// Compare two version strings, such as "19.0.0" and "18.1.1.0". If all matched
// elements are identical, the longer version is the largest by this method.
// Examples:
// "19.0.0" > "19"
// "19.0.1" > "19.0.0"
// "19.1.0" > "19.0.1"
// "19" > "18.999.999"
int compareVersions(String a, String b) {
def aParts = a.tokenize('.').collect {it.toInteger()}
def bParts = b.tokenize('.').collect {it.toInteger()}
compareVersionList(aParts, bParts)
}
String getAndroidSdkDir() {
def rootDir = project.rootDir
def localProperties = new File(rootDir, 'local.properties')
def androidSdkDir = ""
if (localProperties.exists()) {
Properties properties = new Properties()
localProperties.withInputStream { instr ->
properties.load(instr)
}
def sdkDirProp = properties.getProperty('sdk.dir')
if (sdkDirProp != null) {
androidSdkDir = sdkDirProp
} else {
sdkDirProp = properties.getProperty('android.dir')
if (sdkDirProp != null) {
androidSdkDir = (new File(rootDir, sdkDirProp)).getAbsolutePath()
} else {
throw new RuntimeException(
"No sdk.dir property defined in local.properties file.")
}
}
} else {
String envVar = System.getenv("ANDROID_HOME")
if (envVar != null) {
androidSdkDir = envVar
} else {
String property = System.getProperty("android.home")
if (property != null) {
androidSdkDir = property
}
}
}
println androidSdkDir
androidSdkDir
}
cordovaSdkVersion = highestSdkAvailable(19)
cordovaBuildToolsVersion = latestBuildToolsAvailable("19.0.0")