关于 gradle-patterns
gradle-patterns 是面向 Android 开发者的现代 Gradle 配置参考技能,涵盖版本目录、约定插件、多模块设置、依赖声明和构建优化。
主要功能
- 01版本目录配置
- 02约定插件模式
- 03Gradle 构建优化
- 04多模块项目设置
- 05依赖声明与构建命令
适用场景
- 01组织 Android 多模块构建
- 02集中管理插件和依赖版本
- 03改进 Gradle 构建配置
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ ahmed3elshaer/ everything-claude-code-mobile --skill gradle-patterns安装后在 Claude Code 中输入/gradle-patterns确认已加载。
详细介绍
Gradle 构建模式
面向 Android 的现代 Gradle 配置。
版本目录
# gradle/libs.versions.toml
[versions]
agp = "8.2.2"
kotlin = "1.9.22"
ksp = "1.9.22-1.0.17"
compose-compiler = "1.5.10"
compose-bom = "2024.02.00"
coroutines = "1.8.0"
koin = "3.5.3"
ktor = "2.3.8"
[libraries]
# Compose BOM
compose-bom = { module = "androidx.compose:compose-bom", version.ref = "compose-bom" }
compose-ui = { module = "androidx.compose.ui:ui" }
compose-material3 = { module = "androidx.compose.material3:material3" }
compose-ui-tooling = { module = "androidx.compose.ui:ui-tooling" }
# Koin
koin-android = { module = "io.insert-koin:koin-android", version.ref = "koin" }
koin-compose = { module = "io.insert-koin:koin-androidx-compose", version.ref = "koin" }
# Ktor Client
ktor-client-core = { module = "io.ktor:ktor-client-core", version.ref = "ktor" }
ktor-client-okhttp = { module = "io.ktor:ktor-client-okhttp", version.ref = "ktor" }
ktor-client-content-negotiation = { module = "io.ktor:ktor-client-content-negotiation", version.ref = "ktor" }
ktor-serialization-json = { module = "io.ktor:ktor-serialization-kotlinx-json", version.ref = "ktor" }
# Testing
junit5 = { module = "org.junit.jupiter:junit-jupiter", version = "5.10.0" }
mockk = { module = "io.mockk:mockk", version = "1.13.8" }
turbine = { module = "app.cash.turbine:turbine", version = "1.0.0" }
coroutines-test = { module = "org.jetbrains.kotlinx:kotlinx-coroutines-test", version.ref = "coroutines" }
[bundles]
compose = ["compose-ui", "compose-material3", "compose-ui-tooling"]
ktor = ["ktor-client-core", "ktor-client-okhttp", "ktor-client-content-negotiation", "ktor-serialization-json"]
testing = ["junit5", "mockk", "turbine", "coroutines-test"]
[plugins]
android-application = { id = "com.android.application", version.ref = "agp" }
android-library = { id = "com.android.library", version.ref = "agp" }
kotlin-android = { id = "org.jetbrains.kotlin.android", version.ref = "kotlin" }
约定插件
// build-logic/convention/build.gradle.kts
plugins {
`kotlin-dsl`
}
dependencies {
compileOnly(libs.android.gradlePlugin)
compileOnly(libs.kotlin.gradlePlugin)
}
// AndroidLibraryConventionPlugin.kt
class AndroidLibraryConventionPlugin : Plugin<Project> {
override fun apply(target: Project) = with(target) {
with(pluginManager) {
apply("com.android.library")
apply("org.jetbrains.kotlin.android")
}
extensions.configure<LibraryExtension> {
compileSdk = 34
defaultConfig.minSdk = 24
compileOptions {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}
}
}
}
构建优化
# gradle.properties
org.gradle.parallel=true
org.gradle.caching=true
org.gradle.configuration-cache=true
org.gradle.jvmargs=-Xmx4g -XX:+UseParallelGC
# Kotlin
kotlin.incremental=true
kotlin.caching.enabled=true
# Android
android.useAndroidX=true
android.nonTransitiveRClass=true
android.nonFinalResIds=true
多模块设置
// settings.gradle.kts
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
}
}
include(":app")
include(":core:common")
include(":core:ui")
include(":core:network")
include(":feature:home")
include(":feature:detail")
依赖声明
// Feature module build.gradle.kts
plugins {
id("convention.android.library")
id("convention.android.compose")
}
dependencies {
implementation(project(":core:common"))
implementation(project(":core:ui"))
implementation(libs.bundles.compose)
implementation(libs.koin.compose)
testImplementation(libs.bundles.testing)
}
构建命令
# Full build with timing
./gradlew build --profile
# Specific module
./gradlew :feature:home:assembleDebug
# Dependency tree
./gradlew :app:dependencies --configuration releaseRuntimeClasspath
# Clear caches
./gradlew cleanBuildCache
rm -rf ~/.gradle/caches
# Build scan
./gradlew build --scan
请记住:配置一次,每天优化。
评论
支持 Markdown