How to define version property in gradle

Issue #351

From Gradle tips and recipes, Configure project-wide properties

For projects that include multiple modules, it might be useful to define properties at the project level and share them across all modules. You can do this by adding extra properties to the ext block in the top-level build.gradle file.

1
2
3
4
5
ext {
navigationVersion = "2.0.0"
}

rootProject.ext.navigationVersion

Versions are used mostly in dependencies block so having them defined in global ext is not quite right. We can use def to define variables

1
2
3
4
dependencies {
def navigationVersion = "2.0.0"
implementation "androidx.navigation:navigation-fragment-ktx:$navigationVersion"
}

For better namespacing, we can use a class

1
2
3
4
5
6
7
8
9
10
class Version {
static def navigation = "2.0.0"
static def drawerLayout = "1.0.0"
static def koin = "2.0.1"
static def moshi = "1.8.0"
}

dependencies {
implementation "androidx.navigation:navigation-fragment-ktx:$Version.navigation"
}

Comments