$npx -y skills add hanamizuki/solopreneur --skill gradle-logicExpert guidance on setting up scalable Gradle build logic using Convention Plugins and Version Catalogs.
| 1 | # Android Gradle Build Logic & Convention Plugins |
| 2 | |
| 3 | This skill helps you configure a scalable, maintainable build system for Android apps using **Gradle Convention Plugins** and **Version Catalogs**, following the "Now in Android" (NiA) architecture. |
| 4 | |
| 5 | ## Goal |
| 6 | Stop copy-pasting code between `build.gradle.kts` files. Centralize build logic (Compose setup, Kotlin options, Hilt, etc.) in reusable plugins. |
| 7 | |
| 8 | ## Project Structure |
| 9 | |
| 10 | Ensure your project has a `build-logic` directory included in `settings.gradle.kts` as a composite build. |
| 11 | |
| 12 | ```text |
| 13 | root/ |
| 14 | ├── build-logic/ |
| 15 | │ ├── convention/ |
| 16 | │ │ ├── src/main/kotlin/ |
| 17 | │ │ │ └── AndroidApplicationConventionPlugin.kt |
| 18 | │ │ └── build.gradle.kts |
| 19 | │ ├── build.gradle.kts |
| 20 | │ └── settings.gradle.kts |
| 21 | ├── gradle/ |
| 22 | │ └── libs.versions.toml |
| 23 | ├── app/ |
| 24 | │ └── build.gradle.kts |
| 25 | └── settings.gradle.kts |
| 26 | ``` |
| 27 | |
| 28 | ## Step 1: Configure `settings.gradle.kts` |
| 29 | |
| 30 | Include the `build-logic` as a plugin management source. |
| 31 | |
| 32 | ```kotlin |
| 33 | // settings.gradle.kts |
| 34 | pluginManagement { |
| 35 | includeBuild("build-logic") |
| 36 | repositories { |
| 37 | google() |
| 38 | mavenCentral() |
| 39 | gradlePluginPortal() |
| 40 | } |
| 41 | } |
| 42 | dependencyResolutionManagement { |
| 43 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) |
| 44 | repositories { |
| 45 | google() |
| 46 | mavenCentral() |
| 47 | } |
| 48 | } |
| 49 | ``` |
| 50 | |
| 51 | ## Step 2: Define Dependencies in `libs.versions.toml` |
| 52 | |
| 53 | Use the Version Catalog for both libraries *and* plugins. |
| 54 | |
| 55 | ```toml |
| 56 | [versions] |
| 57 | androidGradlePlugin = "8.2.0" |
| 58 | kotlin = "1.9.20" |
| 59 | |
| 60 | [libraries] |
| 61 | # ... |
| 62 | |
| 63 | [plugins] |
| 64 | android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" } |
| 65 | android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" } |
| 66 | kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" } |
| 67 | # Define your own plugins here |
| 68 | nowinandroid-android-application = { id = "nowinandroid.android.application", version = "unspecified" } |
| 69 | ``` |
| 70 | |
| 71 | ## Step 3: Create a Convention Plugin |
| 72 | |
| 73 | Inside `build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt`: |
| 74 | |
| 75 | ```kotlin |
| 76 | import com.android.build.api.dsl.ApplicationExtension |
| 77 | import org.gradle.api.Plugin |
| 78 | import org.gradle.api.Project |
| 79 | import org.gradle.kotlin.dsl.configure |
| 80 | |
| 81 | class AndroidApplicationConventionPlugin : Plugin<Project> { |
| 82 | override fun apply(target: Project) { |
| 83 | with(target) { |
| 84 | with(pluginManager) { |
| 85 | apply("com.android.application") |
| 86 | apply("org.jetbrains.kotlin.android") |
| 87 | } |
| 88 | |
| 89 | extensions.configure<ApplicationExtension> { |
| 90 | defaultConfig.targetSdk = 34 |
| 91 | // Configure common options here |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | ``` |
| 97 | |
| 98 | Don't forget to register it in `build-logic/convention/build.gradle.kts`: |
| 99 | |
| 100 | ```kotlin |
| 101 | gradlePlugin { |
| 102 | plugins { |
| 103 | register("androidApplication") { |
| 104 | id = "nowinandroid.android.application" |
| 105 | implementationClass = "AndroidApplicationConventionPlugin" |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | ``` |
| 110 | |
| 111 | ## Usage |
| 112 | |
| 113 | Apply your custom plugin in your modules (e.g., `app/build.gradle.kts`): |
| 114 | |
| 115 | ```kotlin |
| 116 | plugins { |
| 117 | alias(libs.plugins.nowinandroid.android.application) |
| 118 | } |
| 119 | ``` |
| 120 | |
| 121 | This drastically cleans up module-level build files. |