You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cordova.apache.org by ia...@apache.org on 2014/09/10 17:04:40 UTC

[2/2] android commit: CB-7512: Determine SDK and build tools version dynamcally at build time

CB-7512: Determine SDK and build tools version dynamcally at build time


Project: http://git-wip-us.apache.org/repos/asf/cordova-android/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-android/commit/c8bbdb23
Tree: http://git-wip-us.apache.org/repos/asf/cordova-android/tree/c8bbdb23
Diff: http://git-wip-us.apache.org/repos/asf/cordova-android/diff/c8bbdb23

Branch: refs/heads/master
Commit: c8bbdb23defb4a2fc1227b7f624934df29695937
Parents: 7ee8117
Author: Ian Clelland <ic...@chromium.org>
Authored: Wed Sep 10 10:57:43 2014 -0400
Committer: Ian Clelland <ic...@chromium.org>
Committed: Wed Sep 10 10:57:43 2014 -0400

----------------------------------------------------------------------
 bin/templates/project/build.gradle   |   6 +-
 bin/templates/project/cordova.gradle | 147 ++++++++++++++++++++++++++++++
 framework/build.gradle               |   4 +-
 3 files changed, 153 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-android/blob/c8bbdb23/bin/templates/project/build.gradle
----------------------------------------------------------------------
diff --git a/bin/templates/project/build.gradle b/bin/templates/project/build.gradle
index f56784e..e3468d8 100644
--- a/bin/templates/project/build.gradle
+++ b/bin/templates/project/build.gradle
@@ -19,6 +19,8 @@
 
 import java.util.regex.Pattern
 
+ext.cordova = {}
+apply from: 'cordova.gradle', to: ext.cordova
 apply plugin: 'android'
 
 buildscript {
@@ -57,8 +59,8 @@ android {
         versionCode Integer.parseInt("" + getVersionCodeFromManifest() + "0")
     }
 
-    compileSdkVersion 19
-    buildToolsVersion "19.0.0"
+    compileSdkVersion cordova.cordovaSdkVersion
+    buildToolsVersion cordova.cordovaBuildToolsVersion
 
     if (multiarch || System.env.BUILD_MULTIPLE_APKS) {
         productFlavors {

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/c8bbdb23/bin/templates/project/cordova.gradle
----------------------------------------------------------------------
diff --git a/bin/templates/project/cordova.gradle b/bin/templates/project/cordova.gradle
new file mode 100644
index 0000000..40529d3
--- /dev/null
+++ b/bin/templates/project/cordova.gradle
@@ -0,0 +1,147 @@
+/*
+       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")
+

http://git-wip-us.apache.org/repos/asf/cordova-android/blob/c8bbdb23/framework/build.gradle
----------------------------------------------------------------------
diff --git a/framework/build.gradle b/framework/build.gradle
index 47341b3..59e1ae7 100644
--- a/framework/build.gradle
+++ b/framework/build.gradle
@@ -32,8 +32,8 @@ buildscript {
 apply plugin: 'android-library'
 
 android {
-    compileSdkVersion 19
-    buildToolsVersion "19.0.0"
+    compileSdkVersion cordova.cordovaSdkVersion
+    buildToolsVersion cordova.cordovaBuildToolsVersion
 
     compileOptions {
         sourceCompatibility JavaVersion.VERSION_1_7