Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Version Catalog and Gradle File Syntax

I am in need of guidance on implementing a version catalog in an Android app. In this documentation there is an example of a version catalog implementation:

[versions]
ktx = "1.9.0"

[libraries]
androidx-ktx = { group = "androidx.core", name = "core-ktx", version.ref = "ktx" }

It states that the dependency is defined as this in the gradle file:

implementation(libs.androidx.ktx) 

I don't understand why that is because I don't see the exact match "libs.androidx.ktx" in the version catalog (libs.versions.toml file) - the only thing close to that is "androidx-ktx". Would someone be able to explain the syntax of the version catalog and gradle build file as well as provide a version catalog example for this dependency as well?

implementation(libs.androidx.lifecycle.runtime.ktx)
like image 723
mfusco Avatar asked Mar 15 '26 06:03

mfusco


1 Answers

Would someone be able to explain the syntax of the version catalog

It is TOML. TOML has a specification.

I don't see the exact match "libs.androidx.ktx" in the version catalog (libs.versions.toml file) - the only thing close to that is "androidx-ktx"

TOML keys (androidx-ktx) cannot have dots in them, which is why the keys are not like androidx.ktx.

As to why the Gradle side uses dots rather than dashes, my assumption is that it is for code-completion in modern IDEs. Gradle generates code based on the catalog TOML. So, you can imagine the overall catalog being akin to:

object libs {
  object androidx {
    val kts = object : SomeGradleTypeWhoseNameIForget {
      // other stuff goes here, including the version number
    }
  }
}

Then, as you are entering the implementation() line in a module's build.gradle.kts file, once you start typing libs., the IDE will help guide you to filling in the rest of the value from the currently-defined dependencies.

And the libs in libs.androidx.ktx lines up with libs in libs.versions.toml.

provide a version catalog example for this dependency as well?

androidx-lifecycle-runtime-ktx = { group = "androidx.lifecycle", name = "lifecycle-runtime-ktx", version.ref = "lifecycleRuntimeKtx" }

(assuming that up in [versions] you defined lifecycleRuntimeKtx to be some suitable version)

FWIW, here is the Gradle version catalog documentation.

like image 170
CommonsWare Avatar answered Mar 16 '26 22:03

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!