Skip to main content

Articles, snippets and thoughts about code from Andrew Lord.

Manage Gradle dependencies using Kotlin code in buildSrc

Published on · Updated on
1 minute read

Deprecated

Gradle now includes a mechanism called 'Version Catalogs' that allow dependency coordinates and versions to be shared between subprojects. On top of this Gradle generates a type-safe API to reference the dependencies in your Gradle build scripts. This snippet will no longer be maintained meaning the code samples may become out-of-date. Please check out my latest articles for more up-to-date topics.

Browse all Android

Using Kotlin to maintain our Gradle dependencies within the buildSrc directory can be really great.

  1. Allows us to use a familiar syntax
  2. Provides auto-complete within Groovy or Kotlin Gradle scripts
  3. Can click through to a particular dependencies definition

Sweet!

→ /buildSrc/src/main/java/com/myapp/Versions.kt

package com.myapp

object Versions {
  object AndroidX {
    const val appCompat = "1.0.2"
  }
  const val kotlin = "1.3.10"
  ...
}

→ /buildSrc/src/main/java/com/myapp/Libs.kt

package com.myapp

object Libs {
  object AndroidX {
    const val appCompat = "androidx.appcompat:appcompat:${Versions.AndroidX.appCompat}"
  }
  const val kotlinStdlib = "org.jetbrains.kotlin:kotlin-stdlib:${Versions.kotlin}"
}

→ /src/app/build.gradle

import com.myapp.Libs

dependencies {
  implementation Libs.kotlinStdlib
  implementation Libs.AndroidX.appCompat
}

I hope the article was useful. If you have any feedback or questions please feel free to reach out to me on Twitter.

Thanks for reading and happy coding!

Like what you read? Please share the article.

Avatar of Andrew Lord

Hi, I hope you enjoyed the article. I am Andrew - a builder of apps and developer tools. Articles on the blog focus on all aspects of Android and iOS development using Kotlin and Swift.

Subscribe via RSS