Share dependency versions across multiple Version Catalogs

I’m trying to find a way to share a dependency’s version across multiple Version Catalog toml files.

For example, say I have the following two Version Catalog toml files:

libs.versions.toml

[versions]
dagger = "2.42"

[libraries]
dagger-android = { module = "com.google.dagger:dagger-android", version.ref = "dagger" }

buildLibs.versions.toml

[versions]
dagger = "2.42"

[libraries]
hiltAndroidGradlePlugin = { module = "com.google.dagger:hilt-android-gradle-plugin", version.ref = "dagger" }

I use the same dagger version across both Version Catalogs, and when I need to bump the dagger version I have to remember to update all toml files.

My workaround is to share the version using the Version Catalog builder API by doing:

In my settings.gradle

ext.sharedVersions = [ dagger : "2.42"]

dependencyResolutionManagement {
    versionCatalogs {
        libs {
            version('dagger', sharedVersions.dagger)
            library('dagger-android', 'com.google.dagger', 'dagger-android').versionRef('dagger')
        }
        buildLibs {
            version('dagger', sharedVersions.dagger)
            library('hiltAndroidGradlePlugin', 'com.google.dagger', 'hilt-android-gradle-plugin').versionRef('dagger')
        }
    }
}

which works, but takes away from my dependencies being centralized as some are declared in toml files, while other are declared in settings.gradle.

There might not be a way with TOML files.
They are intentionally just for the simple cases.
And for more complex use-cases the programmatic API is intended.
You could maybe also have a settings plugin that defines it or similar.

Btw. if you keep the current way, please at least do not use ext but just a local variable with def.
Almost any usage of ext is just a dirty work-around that should be replaced by something more appropriate like a local variable or a properly typed extension.

1 Like