当前位置:  开发笔记 > 运维 > 正文

buildToolsVersion范围或gradle中的"+"

如何解决《buildToolsVersion范围或gradle中的"+"》经验,为你挑选了1个好方法。

我想为Gradle指定一系列可能的android构建工具版本.

在build.gradle中指定buildToolsVersion时是否可以使用+?有点像buildToolsVersion 23+

或者类似于编译依赖项的范围:

compile group: 'log4j', name: 'log4j', version: '[1.2.12,1.2.17]'

Konstantin L.. 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当然,代码可以改进.例如,现在,第一个参数是回退值,但是例如,它可以被重写为范围的下边界.

我希望,这有帮助



1> Konstantin L..:

好问题.

是! 这是可能的.我的回答大量使用这个片段: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当然,代码可以改进.例如,现在,第一个参数是回退值,但是例如,它可以被重写为范围的下边界.

我希望,这有帮助

推荐阅读
可爱的天使keven_464
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有