Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

buildToolsVersion range or '+' in gradle

I would like to specify a range of possible android build tool versions to Gradle.

Is it possible to use + when specifying buildToolsVersion in build.gradle? Something like buildToolsVersion 23+ ?

Or a range like it's possible with compile dependencies:

compile group: 'log4j', name: 'log4j', version: '[1.2.12,1.2.17]'
like image 915
ApriOri Avatar asked Dec 30 '15 17:12

ApriOri


People also ask

What is the use of buildToolsVersion?

buildToolsVersion is the version of the compilers (aapt, dx, renderscript compiler, etc...) that you want to use. For each API level (starting with 18), there is a matching . 0.0 version. At IO 2014, we release API 20 and build-tools 20.0.

How do I check my buildToolsVersion?

For the compileSdkVersion you can goto Tools > Android > SDK Manager . This will pull up a window that will allow you to manage your compileSdkVersion and your buildToolsVersion . You can also select the link at the bottom left (Launch Standalone SDK Manager) this will give you a little bit more information.

What is the buildToolsVersion for Android SDK 32?

Android SDK Build-Tools — 32.0. 0.


1 Answers

Good question.

Yes! It's possible. My answer heavily uses this snippet: 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
}

The code of getBuildVersionFromRange can be improved, of course. For example, now, the first parameter is a fallback value, but it can be rewritten to be the bottom border of the range, for example.

I hope, it helps

like image 134
Konstantin Loginov Avatar answered Sep 22 '22 13:09

Konstantin Loginov