I’m setting up some projects in gradle 8.4 and trying to create a good versioning system for those projects, my first instinct was to set the variables for minor/major/patch etc. in gradle.properties
file, like the old days. Apparently I’m not setting up properly anymore or things have changed.
My gradle.properties file (the numbers are examples):
#Production
VERSION_CODE_PROD= 101
versionCodeProd = 101
VERSION_MAJOR_PROD=1
VERSION_MINOR_PROD=0
VERSION_PATCH_PROD=1
#Staging
VERSION_CODE_STAGING=101
VERSION_MAJOR_STAGING=1
VERSION_MINOR_STAGING=0
VERSION_PATCH_STAGING=1
Naturally i tried accessing these variables in the project’s build.gradle
file to no success:
project’s build.gradle
ext:
ext {
//prod
versionName = VERSION_MAJOR_PROD + '.' + VERSION_MINOR_PROD + '.' + VERSION_PATCH_PROD
versionCode = VERSION_CODE_PROD
//staging
versionNameStag = VERSION_MAJOR_PROD_ALT + '.' + VERSION_MINOR_PROD_ALT + '.' + VERSION_PATCH_PROD_ALT
versionCodeStag = VERSION_CODE_PROD_ALT
}
The code above states that all those references to gradle.properties
are unresolved, even a simple variable declaration (foo = bar
) cannot be resolved (and I can’t access these variables on module:app build.gradle
)
Formerly i would access these variables in module:app build.gradle
like so:
productFlavors {
production {
versionCode rootProject.ext.versionCode
versionName rootProject.ext.versionName
}
staging {
versionCode rootProject.ext.versionNameStag
versionName rootProject.ext.versionCodeStag
}
}
Tried serching the gradle 8 docs to see changes, and now I know I should be using kotlin DSL for these configs, my question is, how to translate the former implementation i had (which some older applications still use) to a similar implementation on kotlin DSL?
While searching the doc Kotlin DSL and Configuring the Build Environment
I came across the extra
delegation, and tried to implement this in the project’s build.gradle
:
ext {
val versionCodeProd : Int by extra(1)
}
But can’t access it using versionCode = project.extra["versionCodeProd"]
anywhere else (obviously I synchronized the project after declaring it)