Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Migrate to Gradle version catalog and conventio...

Migrate to Gradle version catalog and convention plugins

Gradle always has been hard to grasp because of multiple ways to do the same thing. With the project scaling and requirements increasing it is crucial to scale our build logic and support developers across the teams. This talk will cover how to migrate to Gradle centralized version catalogs and share some of the build logic as convention plugins. This method is adopted by AndroidX project, Now In Android, and many more.

Merab Tato Kutalia

September 08, 2022
Tweet

More Decks by Merab Tato Kutalia

Other Decks in Technology

Transcript

  1. 🗿 Stone Age - Groovy • Easy setup • Tons

    of examples • No type-safety • Manual version management • “ext” blocks (no code completion) Pros / Cons
  2. 🏰 Medieval Age- buildSrc • Code completion • IDE support

    (navigation) • Type-safety • Non-trivial setup • Frequent cache loss (version update clears the cache) • Android Studio dependency update Pros / Cons
  3. 🚀 Modern Age - Version Catalog Pros • Type safety

    • Standard for libraries and plugins • 1 line implementation via bundles • No cache loss • Auto dependency updates • Best practices and easy to setup • Publish 🤯
  4. 🚀 Modern Age - Version Catalog • Type safety •

    Standard for libraries and plugins • 1 line implementation via bundles • No cache loss • Auto dependency updates • Best practices and easy to setup • Publish 🤯 • Android Studio support 🫠 ➡ ☀ Pros / Cons
  5. 📜 lib.versions.toml [versions] is used to declare the version numbers

    that will be referenced later by plugins and libraries. [libraries] Define the libraries [bundles] Are used to define a set of dependencies [plugins] Are used to define plugin
  6. Q&A

  7. 🏗 Convention Plugins • Convention over configuration • Standard API

    by Gradle • Share build logic • Composable • Type-unsafe API for version catalogs 😭
  8. class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {

    with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } } } }
  9. class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {

    with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure<BaseAppModuleExtension> { } } } }
  10. class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {

    with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure<BaseAppModuleExtension> { compileSdk = 33 } } } }
  11. class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {

    with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure<BaseAppModuleExtension> { compileSdk = 33 defaultConfig.apply { targetSdk = 33 minSdk = 26 applicationId = "me.tatocaster.gradleversioncatalogexample" versionCode = 1 versionName = "1.0" } } } } }
  12. class AndroidApplicationConventionPlugin : Plugin<Project> { override fun apply(target: Project) {

    with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure<BaseAppModuleExtension> { compileSdk = 33 defaultConfig.apply { targetSdk = 33 minSdk = 26 applicationId = "me.tatocaster.gradleversioncatalogexample" versionCode = 1 versionName = "1.0" } compileOptions { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } } } } }
  13. 🧱 Resources • Now In Android - https://github.com/android/nowinandroid • Convention

    plugins - https://docs.gradle.org/current/samples/sample_convention_plugins.html • Version Update plugin - https://github.com/littlerobots/version-catalog-update-plugin
  14. Q&A