gradle force build tools version on third party libraries?

你说的曾经没有我的故事 提交于 2019-11-27 13:40:50

The lack easy way to do it is beyond my understanding. Tons of people use library projects that they don't own, have to build with Jenkins or have other reasons not to touch them and don't want to fork them for personal use.

Anyway, I found a solution here.

Will copy it here just in case:

In you root build.gradle add

ext {
    compileSdkVersion = 20
    buildToolsVersion = "20.0.0"
}
subprojects { subproject ->
    afterEvaluate{
        if((subproject.plugins.hasPlugin('android') || subproject.plugins.hasPlugin('android-library'))) {
            android {
                compileSdkVersion rootProject.ext.compileSdkVersion
                buildToolsVersion rootProject.ext.buildToolsVersion
            }
        }
    }
}

This will apply compileSdkVersion and buildToolsVersion to any android modules you have.

And in your main project's build.gradle change dependencies to this:

compileSdkVersion rootProject.ext.compileSdkVersion
buildToolsVersion rootProject.ext.buildToolsVersion

Basically you are defining them once and could use from anywhere.

Cheers.

Ryan H.

If you only want to update the compileSdkVersion and buildToolsVersion values only when the buildToolsVersion value is too low, you can first compare the version number of the subproject and only update if needed. This way you make minimal changes to other projects and have less projects to check if things go wrong.

So, let's say that Android Studio is telling you that you need a minimum build tools version of 25.0.0, then in your root build.gradle, here's how you would check each sub-project's buildToolsVersion and only change it if it's less than 25.0.0:

subprojects {
    afterEvaluate {project ->
        if (project.hasProperty("android") && VersionNumber.parse(project.property("android").buildToolsVersion) < VersionNumber.parse("25.0.0")) {
            android {
                compileSdkVersion 25
                buildToolsVersion '25.0.0'
            }
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!