简体   繁体   中英

How to specify common string for version in build.gradle?

I have a build.gradle that has following content:-

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'
compile 'com.android.support:cardview-v7:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'

How can I specify the version number (here 25.3.1) at a common place and reuse it every where, so that when ever I need to change it, I have to change it at only one place?

You can use Gradle features to achieve this.

ext {
    supportVersion = "25.3.1"
}

dependencies {
    compile "com.android.support:appcompat-v7:$supportVersion"
    compile "com.android.support:design:$supportVersion"
    compile "com.android.support:cardview-v7:$supportVersion"
    compile "com.android.support:recyclerview-v7:$supportVersion"
}

See also:

In Project level gradle, you specify the version code. If we specify version code in project level then we can use that version in all modules. For example, we had three modules in a project. If we specify version code in project level then easily use that in module level gradle file.

ext {
    appcompatVersion = '25.3.1'
    supportDesignVersion = '25.3.1'
    cardviewVersion = '25.3.1'
    recyclerviewVersion = '25.3.1' 
}

In Module level gradle, you use it like below:

dependencies {
        compile 'com.android.support:appcompat-v7:$appcompatVersion'
        compile 'com.android.support:design:$supportDesignVersion'
        compile 'com.android.support:cardview-v7:$cardviewVersion'
        compile 'com.android.support:recyclerviewv7:$recyclerviewVersion'
}

In the project gradle file add the variable

  buildscript {
     ext.supportLibraryVersion = "25.3.1"

     dependencies {
          classpath 'com.android.tools.build:gradle:2.3.3'
          ........

And in the app gradle file refer the variable created.

  dependencies {
      compile "com.android.support:appcompat-v7:$supportLibraryVersion"
      compile "com.android.support:design:$supportLibraryVersion"

for more reference follow this link

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM