如何在Gradle上设置依赖项的版本? [英] How to set a version of dependencies on Gradle?

查看:898
本文介绍了如何在Gradle上设置依赖项的版本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的Gradle中的一个地方设置依赖项的版本 (app/build.gradle)配置文件,以防万一更新.

I want to set the version of my dependencies in just one place in my Gradle (app/build.gradle) configuration file , to became easier to change in case of a update.

问题是:

dependencies {
implementation 'com.google.android.gms:play-services-maps:12.0.0'
implementation 'com.google.android.gms:play-services-auth:12.0.0'
implementation 'com.google.firebase:firebase-core:12.0.0'
implementation 'com.google.firebase:firebase-auth:12.0.0'
implementation 'com.google.firebase:firebase-database:12.0.0'
implementation 'com.google.firebase:firebase-storage:12.0.0'
implementation 'com.google.firebase:firebase-messaging:12.0.0'
implementation 'com.google.android.gms:play-services-ads:12.0.0'
implementation 'com.github.bumptech.glide:glide:4.5.0'
}

您能看到吗,我多次投放相同的版本,这使将所有版本更改为下一版本变得很缓慢且毫无成效.

Can you see that, i'm repiting the same version many times and this make slow and unprodutive to change all version to the next version.

就像在Maven中一样,我可以这样做:

Like in Maven i could just do like this:

<properties>
    <org.springframework.version>5.0.8.RELEASE</org.springframework.version>
</properties>

设置版本后,我只需添加以下内容:

After set the version, I just add like this:

<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
</dependencies>

Maven配置的最后一部分确实在此部分中设置了版本:

The last piece of Maven configuration did set the version in this parte:

$ {org.springframework.version}

${org.springframework.version}

如何在Gradle配置中执行相同的操作?

How can I do the same in my gradle configuration?

推荐答案

假定它们都将具有相同版本是完全错误的...

the assumption, that they'd all would have the same version is categorically wrong ...

def glideVersion = "4.5.0"

dependencies {

    implementation "com.google.android.gms:play-services-base:16.0.1"
    implementation "com.google.android.gms:play-services-auth:16.0.1"

    implementation "com.google.android.gms:play-services-maps:16.0.0"
    implementation "com.google.android.gms:play-services-ads:16.0.0"

    implementation "com.google.firebase:firebase-core:16.0.4"
    implementation "com.google.firebase:firebase-auth:16.0.4"

    implementation "com.google.firebase:firebase-database:16.0.3"
    implementation "com.google.firebase:firebase-storage:16.0.3"

    implementation "com.google.firebase:firebase-messaging:17.3.4"

    implementation "com.github.bumptech.glide:glide:${glideVersion}"
}

还可以设置project.ext 属性和版本号-或从外部文件加载它们.

one can also set project.ext properties with version numbers - or load them from external files.

ext {
    glideVersion = "4.5.0"
    ...
}

,然后将其与${rootProject.ext.glideVersion}${project.ext.glideVersion}一起使用.

and then use it with ${rootProject.ext.glideVersion} or ${project.ext.glideVersion}.

通常,更改并不容易...只是另一种组织方式.

in general, it's not easier to change... just another way of organizing it.

这篇关于如何在Gradle上设置依赖项的版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆