I have an Android project for a library. It's using an outdated Android Gradle Plugin version, and I am prompted by Android Studio to upgrade the plugin to the latest version. I have a module that is published to a (local) Maven repository, and I want to make sure that the module is still published to the repository after the upgrade.
When I let the AGP upgrade assistant do its job, I see diffs like the following:
diff --git a/build.gradle b/build.gradle
index e2cd93d..880db61 100644
--- a/build.gradle
+++ b/build.gradle
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
- id 'com.android.application' version '7.2.1' apply false
- id 'com.android.library' version '7.2.1' apply false
+ id 'com.android.application' version '7.4.2' apply false
+ id 'com.android.library' version '7.4.2' apply false
id "co.uzzu.dotenv.gradle" version "2.0.0"
}
diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties
index 082da6b..2041d30 100644
--- a/gradle/wrapper/gradle-wrapper.properties
+++ b/gradle/wrapper/gradle-wrapper.properties
@@ -1,6 +1,6 @@
#Thu Jun 09 22:24:26 CEST 2022
distributionBase=GRADLE_USER_HOME
-distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
+distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
This is all expected. However, since this is a library that I am versioning, AGP upgrade also removes the versionName and versionCode from the build.gradle file. This is a problem because I want to keep the versioning of the library in the build.gradle file.
Unfortunately, the AGP upgrade assistant does not provide a way to keep the versioning in the build.gradle file because it does not make sense for libraries, so they claim.
This leads to an issue during the publish task ./gradlew example:publish, which is using the maven-publish plugin, and this config:
afterEvaluate {
publishing {
publications {
release(MavenPublication) {
from components.release
groupId = 'com.example'
version = android.defaultConfig.versionName
}
}
repositories {
maven {
url = "file://$projectDir/example"
}
}
}
}
This error appears:
> Configure project :example
Configuration 'debugCompileClasspath' was resolved during configuration time.
This is a build performance and scalability issue.
See https://github.com/gradle/gradle/issues/2298
Run with --info for a stacktrace.
Configuration 'debugRuntimeClasspath' was resolved during configuration time.
This is a build performance and scalability issue.
See https://github.com/gradle/gradle/issues/2298
Run with --info for a stacktrace.
Configuration 'releaseCompileClasspath' was resolved during configuration time.
This is a build performance and scalability issue.
See https://github.com/gradle/gradle/issues/2298
Run with --info for a stacktrace.
Configuration 'releaseRuntimeClasspath' was resolved during configuration time.
This is a build performance and scalability issue.
See https://github.com/gradle/gradle/issues/2298
Run with --info for a stacktrace.
FAILURE: Build failed with an exception.
* What went wrong:
Could not determine the dependencies of task ':example:publishReleasePublicationToMavenRepository'.
> Cannot query the value of this property because it has no value available.
* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
> Get more help at https://help.gradle.org.
BUILD FAILED in 578ms
Obviously, the versionCode and versionName are missing.
What can I do to prevent that?
I realize they are referenced to the defaultConfig, unlike what is shown in the Gradle docs, where the version is hardcoded there. This again would prevent me from using the version code inside the application itself.
I have seen this question where the solution is to simply add back the versionName and versionCode to the build.gradle file and reference them as build config fields.
However, I am looking for a more robust solution that does not break on an AGP upgrade.
Indeed, you should no longer use the versionName or versionCode in a library.
To still keep the version in the gradle file, you can define a custom extension variable e.g. ext.library_version = "1.2.3". You can reference the variable directly in the publication script.
To reference library version from code, declare a buildConfigField and just assign it the value of custom variable. It will be available in the BuildConfig, the same as earlier the VERSION_NAME and VERSION_CODE.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With