Android Gradle Logic

new-silvermoon/awesome-android-agent-skills/.github/skills/build_and_tooling/android-gradle-logic

作者 new-silvermoon82900eacc8dbe13de93c6310af27b9df4b2bd2f6無授權條款收錄於 2026年10月9日更新於 2026年10月9日

Expert guidance on setting up scalable Gradle build logic using Convention Plugins and Version Catalogs.

AI 產生的概覽

指導使用慣例外掛與版本目錄建立可擴充的 Android Gradle 建置邏輯。

功能
提供逐步說明,協助使用 Gradle 慣例外掛與版本目錄集中管理 Android 建置設定。內容涵蓋包含 build-logic 複合建置的專案結構、settings.gradle.kts 設定、libs.versions.toml 中外掛與程式庫的定義、撰寫慣例外掛類別、註冊外掛,以及在模組中套用。產出為指引與程式碼片段,而非產生的檔案。
適用情境
適用於 Android 專案在多個 build.gradle.kts 檔案中重複建置設定、希望集中管理的情況。也適合採用 Now in Android 風格 build-logic 結構,或導入慣例外掛與版本目錄的團隊。
執行需求
使用 Kotlin DSL 的 Gradle Android 專案;不附帶指令碼,僅提供說明與程式碼範例。

Android Gradle Build Logic & Convention Plugins

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.

Goal

Stop copy-pasting code between build.gradle.kts files. Centralize build logic (Compose setup, Kotlin options, Hilt, etc.) in reusable plugins.

Project Structure

Ensure your project has a build-logic directory included in settings.gradle.kts as a composite build.

text
root/├── build-logic/│   ├── convention/│   │   ├── src/main/kotlin/│   │   │   └── AndroidApplicationConventionPlugin.kt│   │   └── build.gradle.kts│   ├── build.gradle.kts│   └── settings.gradle.kts├── gradle/│   └── libs.versions.toml├── app/│   └── build.gradle.kts└── settings.gradle.kts

Step 1: Configure settings.gradle.kts

Include the build-logic as a plugin management source.

kotlin
// settings.gradle.ktspluginManagement {    includeBuild("build-logic")    repositories {        google()        mavenCentral()        gradlePluginPortal()    }}dependencyResolutionManagement {    repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)    repositories {        google()        mavenCentral()    }}

Step 2: Define Dependencies in libs.versions.toml

Use the Version Catalog for both libraries and plugins.

toml
[versions]androidGradlePlugin = "8.2.0"kotlin = "1.9.20"
[libraries]# ...
[plugins]android-application = { id = "com.android.application", version.ref = "androidGradlePlugin" }android-library = { id = "com.android.library", version.ref = "androidGradlePlugin" }kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }# Define your own plugins herenowinandroid-android-application = { id = "nowinandroid.android.application", version = "unspecified" }

Step 3: Create a Convention Plugin

Inside build-logic/convention/src/main/kotlin/AndroidApplicationConventionPlugin.kt:

kotlin
import com.android.build.api.dsl.ApplicationExtensionimport org.gradle.api.Pluginimport org.gradle.api.Projectimport org.gradle.kotlin.dsl.configure
class AndroidApplicationConventionPlugin : Plugin<Project> {    override fun apply(target: Project) {        with(target) {            with(pluginManager) {                apply("com.android.application")                apply("org.jetbrains.kotlin.android")            }
            extensions.configure<ApplicationExtension> {                defaultConfig.targetSdk = 34                // Configure common options here            }        }    }}

Don't forget to register it in build-logic/convention/build.gradle.kts:

kotlin
gradlePlugin {    plugins {        register("androidApplication") {            id = "nowinandroid.android.application"            implementationClass = "AndroidApplicationConventionPlugin"        }    }}

Usage

Apply your custom plugin in your modules (e.g., app/build.gradle.kts):

kotlin
plugins {    alias(libs.plugins.nowinandroid.android.application)}

This drastically cleans up module-level build files.

來源與署名

來源:new-silvermoon/awesome-android-agent-skills位於.github/skills/build_and_tooling/android-gradle-logic提交82900ea

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架