简体   繁体   English

如何在build.gradle中为版本指定通用字符串?

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

I have a build.gradle that has following content:- 我有一个build.gradle具有以下内容:-

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? 如何在公共位置指定版本号(此处为25.3.1),并在每个位置重复使用,以便在需要更改时,只能在一个位置进行更改?

You can use Gradle features to achieve this. 您可以使用Gradle功能来实现。

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. 在项目级别gradle中,指定版本代码。 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. 如果我们在项目级别指定版本代码,则可以在模块级别的gradle文件中轻松使用该代码。

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: 在模块级gradle中,您可以像下面这样使用它:

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 在项目gradle文件中添加变量

  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. 并在应用gradle文件中引用创建的变量。

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

for more reference follow this link 有关更多参考,请点击链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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