方式1:最简单的方式 gradle.properties代码
1 2 3 compileSdkVersion=33 minSdkVersion=21 targetSdkVersion=33
在 build.gradle.kts 中引用
1 2 3 4 5 6 7 android { compileSdk = property("compileSdkVersion" ).toString().toInt() defaultConfig { minSdk = property("minSdkVersion" ).toString().toInt() targetSdk = property("targetSdkVersion" ).toString().toInt() } }
方式 2:直接在根级 build.gradle.kts 中定义常量 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 val compileSdkVersion = 33 val minSdkVersion = 21 val targetSdkVersion = 33 subprojects { plugins.withId("com.android.application" ) { extensions.getByName<com.android.build.gradle.BaseExtension>("android" ).apply { compileSdkVersion(compileSdkVersion) defaultConfig { minSdk = minSdkVersion targetSdk = targetSdkVersion } } } plugins.withId("com.android.library" ) { extensions.getByName<com.android.build.gradle.LibraryExtension>("android" ).apply { compileSdkVersion(compileSdkVersion) defaultConfig { minSdk = minSdkVersion targetSdk = targetSdkVersion } } } }
所有子模块都会使用这些统一的版本号。
方式 3:通过扩展函数封装设置逻辑 在根级 build.gradle.kts 中定义一个扩展函数,用于配置 SDK 版本号:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 fun com.android.build.gradle.BaseExtension.applyCommonSdkConfig() { compileSdkVersion(33 ) defaultConfig { minSdk = 21 targetSdk = 33 } } subprojects { plugins.withId("com.android.application" ) { extensions.getByName<com.android.build.gradle.BaseExtension>("android" ).apply { applyCommonSdkConfig() } } plugins.withId("com.android.library" ) { extensions.getByName<com.android.build.gradle.LibraryExtension>("android" ).apply { applyCommonSdkConfig() } } }
将上述代码放入根级 build.gradle.kts 文件中,然后确保所有子模块的 build.gradle.kts 文件中正确应用了 com.android.application 或 com.android.library 插件,例如:
1 2 3 4 5 6 7 8 9 plugins { id("com.android.application" ) id("kotlin-android" ) } android { }
方式 4:将配置提取到独立的 Kotlin 脚本文件 1: 创建 config.gradle.kts 1 2 3 4 5 6 7 8 9 10 11 12 val compileSdkVersion = 33 val minSdkVersion = 21 val targetSdkVersion = 33 fun com.android.build.gradle.BaseExtension.applyCommonSdkConfig() { compileSdkVersion(compileSdkVersion) defaultConfig { minSdk = minSdkVersion targetSdk = targetSdkVersion } }
2: 在 build.gradle.kts 中应用这个脚本 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 apply(from = "config.gradle.kts" ) subprojects { plugins.withId("com.android.application" ) { extensions.getByName<com.android.build.gradle.BaseExtension>("android" ).apply { applyCommonSdkConfig() } } plugins.withId("com.android.library" ) { extensions.getByName<com.android.build.gradle.LibraryExtension>("android" ).apply { applyCommonSdkConfig() } } }
方式 5:在 Settings 脚本中统一配置 如果项目有多个模块,可以直接在 settings.gradle.kts 中定义扩展属性,供所有模块使用:
1 2 3 4 5 6 7 8 9 10 11 pluginManagement { val compileSdkVersion = 33 val minSdkVersion = 21 val targetSdkVersion = 33 extra["compileSdkVersion" ] = compileSdkVersion extra["minSdkVersion" ] = minSdkVersion extra["targetSdkVersion" ] = targetSdkVersion }
在模块级 build.gradle.kts 中使用:
1 2 3 4 5 6 7 8 android { compileSdk = extra["compileSdkVersion" ] as Int defaultConfig { minSdk = extra["minSdkVersion" ] as Int targetSdk = extra["targetSdkVersion" ] as Int } }