mcpField
A
android-gradle-logic

android-gradle-logic

github.com/new-silvermoon开发992716 次安装13 周前更新
代码仓库

关于 android-gradle-logic

为 Android 项目提供 Gradle 构建逻辑与约定插件配置指南,适合希望维护可扩展构建系统的开发者。它通过 build-logic 和 Version Catalog 集中复用插件与依赖配置。

主要功能

  1. 01设置 build-logic 复合构建
  2. 02使用 Version Catalog 管理版本
  3. 03编写可复用约定插件
  4. 04集中配置 Compose 与 Kotlin 等构建逻辑
  5. 05在模块中应用自定义插件

适用场景

  1. 01减少模块构建文件中的重复配置
  2. 02集中维护 Android 插件设置
  3. 03为多模块项目复用构建约定

安装

复制下面的命令,在终端中运行。

$npx skills add https://github.com/new-silvermoon/awesome-android-agent-skills --skill android-gradle-logic

安装后在 Claude Code 中输入/android-gradle-logic确认已加载。

详细介绍

Android Gradle 构建逻辑与约定插件

本技能可帮助你使用 Gradle 约定插件 和 版本目录 配置可扩展、易维护的 Android 应用构建系统,并遵循 “Now in Android”(NiA)架构。

目标

停止在 build.gradle.kts 文件之间复制粘贴代码。将构建逻辑(Compose 设置、Kotlin 选项、Hilt 等)集中到可复用的插件中。

项目结构

确保项目包含 build-logic 目录,并在 settings.gradle.kts 中将其作为复合构建引入。

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

步骤 1:配置 settings.gradle.kts

将 build-logic 添加为插件管理来源。

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

步骤 2:在 libs.versions.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 here
nowinandroid-android-application = { id = "nowinandroid.android.application", version = "unspecified" }

步骤 3:创建约定插件

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

import com.android.build.api.dsl.ApplicationExtension
import org.gradle.api.Plugin
import org.gradle.api.Project
import 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
            }
        }
    }
}

别忘了在 build-logic/convention/build.gradle.kts 中注册它:

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

用法

在模块中应用自定义插件(例如,app/build.gradle.kts):

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

这样可以大幅精简模块级构建文件。

评论

支持 Markdown