Slide 1

Slide 1 text

Migrate to Gradle version catalog and convention plugins Merab Tato Kutalia Android GDE @TatoKutalia

Slide 2

Slide 2 text

πŸ—Ώ Stone Age - Groovy ● Easy setup ● Tons of examples Pros

Slide 3

Slide 3 text

πŸ—Ώ Stone Age - Groovy ● Easy setup ● Tons of examples ● No type-safety ● Manual version management ● β€œext” blocks (no code completion) Pros / Cons

Slide 4

Slide 4 text

β€œext” block classic dependency management

Slide 5

Slide 5 text

🏰 Medieval Age- buildSrc ● Code completion ● IDE support (navigation) ● Type-safety Pros

Slide 6

Slide 6 text

🏰 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

Slide 7

Slide 7 text

buildSrc Typical module structure

Slide 8

Slide 8 text

πŸš€ 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 🀯

Slide 9

Slide 9 text

πŸš€ 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

Slide 10

Slide 10 text

πŸ“œ 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

Slide 11

Slide 11 text

σ° Ό Assemble

Slide 12

Slide 12 text

1. create lib.versions.toml root gradle folder

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

2. create Gradle plugin configure build.gradle.kts and settings.gradle.kts

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

3. add plugin to root settings.gradle - Composite Build

Slide 18

Slide 18 text

No content

Slide 19

Slide 19 text

4. Enable feature stable in Gradle 7.4 not required when using 7.4+

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Usage

Slide 22

Slide 22 text

Usage

Slide 23

Slide 23 text

🚦 Limitations ● Not supported in precompiled scripts - use type-unsafe api ● IDE support? πŸš€

Slide 24

Slide 24 text

☣ Type-Unsafe API

Slide 25

Slide 25 text

πŸͺ› Tools - Renovate https://github.com/renovatebot/renovate

Slide 26

Slide 26 text

πŸͺ› Tools - refreshVersions https://github.com/jmfayard/refreshVersions

Slide 27

Slide 27 text

Q&A

Slide 28

Slide 28 text

Proprietary + Confidential 🫠 Android Studio

Slide 29

Slide 29 text

πŸ— Convention Plugins ● Convention over configuration ● Standard API by Gradle ● Share build logic ● Composable ● Type-unsafe API for version catalogs 😭

Slide 30

Slide 30 text

🦾 Assemble

Slide 31

Slide 31 text

1. reuse/create Gradle plugin configure build.gradle.kts and settings.gradle.kts

Slide 32

Slide 32 text

No content

Slide 33

Slide 33 text

2. Code

Slide 34

Slide 34 text

class AndroidApplicationConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { } } }

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

class AndroidApplicationConventionPlugin : Plugin { override fun apply(target: Project) { with(target) { with(pluginManager) { apply("com.android.application") apply("org.jetbrains.kotlin.android") } extensions.configure { 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 } } } } }

Slide 40

Slide 40 text

🧩 Result

Slide 41

Slide 41 text

🧩 Composable

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

πŸ”Ž Explore β€œNow in Android” https://github.com/android/nowinandroid

Slide 44

Slide 44 text

🧱 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

Slide 45

Slide 45 text

Q&A