2

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.

the problem is:

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.

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>

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

${org.springframework.version}

How can I do the same in my gradle configuration?

Sham Fiorin
  • 403
  • 4
  • 16

1 Answers1

4

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}"
}

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

ext {
    glideVersion = "4.5.0"
    ...
}

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.

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
  • 1
    @Dims as already stated in previous comments, you need double quotes `"`... at least when evaluating expressions. https://stackoverflow.com/questions/15171049/gradle-single-vs-double-quotes – Martin Zeitler Oct 28 '18 at 08:31
  • 1
    Okay thank you it worked. Also can you explain please, why in the same context `nd4j_version` and `"${nd4j_version}"` refers to different variable? – Dims Oct 28 '18 at 08:38
  • @Dims can only guess, without seeing the script, but `def` defines a local variable - which might be defined with another value, within another scope. within the same scope, it should always be the same. – Martin Zeitler Oct 28 '18 at 09:12
  • 1
    I refer to your code. You wrote `ext { glideVersion`, but in strings you wrote `${project.ext.glideVersion}` not `${ext.glideVersion}`. You got exta nesting, specified nowhere. Why? – Dims Oct 28 '18 at 09:36
  • Also why didn't you wrote `ext { def glideVersion = `? – Dims Oct 28 '18 at 09:38
  • 1
    @Dims because the one defines a Gradle project's extra properties and the other a local Groovy variable. http://groovy-lang.org/documentation.html (Gradle DSL is based upon that). – Martin Zeitler Oct 28 '18 at 09:52
  • The question is not about definition, but about usage. Why does path in expansion string different than in direct usage (for the same way of definition) – Dims Oct 28 '18 at 13:37
  • @Dims because it is root project and sub project (module) - of which each have their own `ext` "extra properties". depending where one defines variables/properties, one has to refer to them accordingly. eg. one could even define local variables in a sub project, and initialize them with root project extra properties. here I often use a `version.properties` file, which I load in the root project and then reference in sub projects (which I'd consider the most organized way of managing the version numbers). – Martin Zeitler Oct 29 '18 at 01:37
  • I think @dims alread anwsered, but `ext` is default from gradle, one right way for use. Def ise common from Groovy. – Sham Fiorin Aug 30 '22 at 02:13
  • @ShamFiorin it depends where/when one has to access the variable or property. – Martin Zeitler Aug 30 '22 at 08:27