Gradle command line arguments to override properties in build.gradle

杀马特。学长 韩版系。学妹 提交于 2021-02-08 13:33:17

问题


Whenever an existing android project is imported, in most cases we need to change values following with our installed tools version number

in project/build.gradle

buildscript {
    ...
    dependencies {
        classpath 'com.android.tools.build:gradle:x.x.x'
    }
}

in project/app/build.gradle

android {
    compileSdkVersion xx
    buildToolsVersion "xx.x.x"

    defaultConfig {
        applicationId "com.example.app.xyz"
        minSdkVersion xx
        targetSdkVersion xx
        versionCode x
        versionName "x.x"
    }
...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:x.xx'
    compile 'com.android.support:appcompat-v7:xx.x.x'
    compile 'com.android.support:recyclerview-v7:xx.x.x'
}

how to override above values of

classpath
compileSdkVersion
buildToolsVersion
targetSdkVersion

using command line argument e.g.

./gradlew installDebug -PCompileSdkVersion=26 -PbuildToolsVersion=26.0.0

or something like this?

My idea is to use one same command (with my installed sdk version numbers as arguments) to build any project not maintained by me.

it is helpful if we have to build multiple projects which are managed by others.it can save much time by overriding their build configuration by ours through command line args so that we do not need to change it every time by going at particular location in each newly imported project.


回答1:


Put in your gradle.properties default value:

SDK_VERSION=26

Use in build.gradle:

android {
    compileSdkVersion project.getProperties().get("SDK_VERSION")
}

Use: ./gradlew build -PSDK_VERSION=26

PS: Don't forget, that you must change support libraries major version also.



来源:https://stackoverflow.com/questions/47051593/gradle-command-line-arguments-to-override-properties-in-build-gradle

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!