groovy 脚本转为 kotlin的 Gradle 脚本步骤
- xx.gradle 文件名更改为 xx.gradle.kts
- 单引号全部转换为双引号
- 编译版本号,目标版本号设置为赋值的方式
android {
compileSdk = 32
defaultConfig {
applicationId = libs.versions.applicationId.get()
minSdk = libs.versions.minSdk.get().toInt()
targetSdk = libs.versions.targetSdk.get().toInt()
versionCode = libs.versions.versionCode.get().toInt()
versionName = libs.versions.versionName.get()
vectorDrawables {
useSupportLibrary = true
}
}
...
}
-
插件引用方式如下所示
plugins { id("com.android.application") version "7.2.2" apply false id("com.android.library") version "7.2.2" apply false id("org.jetbrains.kotlin.android") version "1.7.10" apply false } -
创建 task 的方式
task("clean") { delete(rootProject.buildDir) } -
buildType 的更改
buildTypes { getByName("release") { isMinifyEnabled = true proguardFiles( getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro" ) } } -
依赖在线仓库的和依赖 module
implementation("androidx.core:core-ktx:1.9.0") implementation(project(":xxx"))
-
google 插件仓库转换为 gradle 插件仓库的规范
pluginManagement { repositories { gradlePluginPortal() google() mavenCentral() } resolutionStrategy { eachPlugin { if (requested.id.namespace == "com.android") { useModule("com.android.tools.build:gradle:${requested.version}") } if(requested.id.namespace=="xxx.xx"){ userModule("xxx.xxx.xx") } .... } } } -
版本统一管理方式,此示例展示
.toml文件方式dependencyResolutionManagement { repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS) repositories { google() mavenCentral() } versionCatalogs { create("libs") { from(files("${rootDir.path}/dependencies.toml")) } } } // dependencies.toml [versions] # build config compileSdk = "33" minSdk = "23" targetSdk = "33" java-major = "11" java = "11" versionCode="1" versionName="1.0.0" applicationId="com.symbol.composehello" # official library kotlin = "1.6.0" kotlin-coroutines = "1.6.0-RC2" [libraries] # official library appcompat = { module = "androidx.appcompat:appcompat", version.ref = "androidx-appcompat" } androidx-lifecycle-runtime-ktx = "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0" [plugins] android-application = { id = "com.android.application", version.ref = "android-plugin" } [bundles] compose-core = ["androidx-activity-compose", "compose-uiTooling", "compose-material"]