Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle command line arguments to override properties in build.gradle

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.

like image 357
Kevan Avatar asked Nov 01 '17 09:11

Kevan


People also ask

How do I pass a Gradle argument in command line?

We first read the arguments from a project property. Since this contains all the arguments as one string, we then use the split method to obtain an array of arguments. Next, we pass this array to the args property of our JavaExec task.

How do I override in Gradle?

To override the version of a transitive dependency in Gradle, exclude it from the declared dependency that pulls it in, and then explicitly declare the version that you prefer to use in your build.


1 Answers

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.

like image 112
DeKaNszn Avatar answered Oct 05 '22 23:10

DeKaNszn