Gradle Groovy 脚本转换为Kotlin脚本步骤

383 阅读1分钟

groovy 脚本转为 kotlin的 Gradle 脚本步骤

  1. xx.gradle 文件名更改为 xx.gradle.kts
  2. 单引号全部转换为双引号
  3. 编译版本号,目标版本号设置为赋值的方式
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
        }
    }
  
  
  ...
}
  1. 插件引用方式如下所示

    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
    }
    
  2. 创建 task 的方式

    task("clean") {
        delete(rootProject.buildDir)
    }
    
  3. buildType 的更改

     buildTypes {
            getByName("release") {
                isMinifyEnabled = true
                proguardFiles(
                    getDefaultProguardFile("proguard-android-optimize.txt"),
                    "proguard-rules.pro"
                )
            }
        }
    
  4. 依赖在线仓库的和依赖 module

        implementation("androidx.core:core-ktx:1.9.0")
        implementation(project(":xxx"))
    ​
    
  1. 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")
                }
                ....
            }
        }
    }
    
  2. 版本统一管理方式,此示例展示 .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"]
    ​
    ​
    ​
    ​