使用Kotlin DSL 来编写Gradle脚本,替换Groovy

平时我们使用的Gradle文件,使用的语言是Groovy,现在,我们可以使用Kotlin来编写Gradle脚本了,优势如下。

类型 Kotlin Groovy
自动代码补全 支持 不支持
是否类型安全 不是
源码导航 支持 不支持
重构 自动关联 手动修改

接下来让我们新建一个项目,然后配置为kotlin脚本吧。

1.将单引号替换为双引号

在新建的项目中,直接用Android Studio的替换功能,将gradle文件中的将'替换为"

2.修改Gradle文件扩展名

app的build.gradle修改为build.gradle.kts

同步代码,这个时候会报错

3.将groovy语法改为kotlin语法

修改前

plugins {
    id("com.android.application")
    id("kotlin-android")
}

android {
    compileSdkVersion 30

    defaultConfig {
        applicationId "com.heiko.mykotlindlstest"
        minSdkVersion 21
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {

    implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "androidx.core:core-ktx:1.3.1"
    implementation "androidx.appcompat:appcompat:1.2.0"
    implementation "com.google.android.material:material:1.2.1"
    implementation "androidx.constraintlayout:constraintlayout:2.0.1"
    testImplementation "junit:junit:4.+"
    androidTestImplementation "androidx.test.ext:junit:1.1.2"
    androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
}

修改后

plugins {
    id("com.android.application")
    id("kotlin-android")
    //kotlin("android")
    //kotlin("kapt")
}

android {
    compileSdkVersion(30)

    defaultConfig {
        applicationId("com.heiko.mykotlindlstest")
        minSdkVersion(21)
        targetSdkVersion(30)
        versionCode(1)
        versionName("1.0")

        testInstrumentationRunner("androidx.test.runner.AndroidJUnitRunner")
    }

    buildTypes {
        named("release") {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = "1.8"
    }
}

dependencies {
    val kotlin_version = "1.5.10"
    implementation("org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version")
    implementation("androidx.core:core-ktx:1.3.1")
    implementation("androidx.appcompat:appcompat:1.2.0")
    implementation("com.google.android.material:material:1.2.1")
    implementation("androidx.constraintlayout:constraintlayout:2.0.1")
    testImplementation("junit:junit:4.+")
    androidTestImplementation("androidx.test.ext:junit:1.1.2")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.3.0")
}

接着,我们运行项目,可以发现可以正常运行了。

4.修改其他gradle文件

同理,我们也可以修改其他文件。

修改前的根目录的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    ext.kotlin_version = "1.5.10"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:4.2.1"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter() // Warning: this repository is going to shut down soon
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

修改后的更目录build.gradle,改名为修改为build.gradle.kts

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    val kotlin_version = "1.5.10"
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:4.2.1")
        classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
        jcenter()
        //maven(url = "https://jitpack.io")
    }
}

tasks {
    val clean by registering(Delete::class) {
        delete(buildDir)
    }
}

修改前的settings.gradle

rootProject.name = "MyKotlinDLSTest"
include ':app'

修改后的settings.gradle,更名为settings.gradle.kts

rootProject.name = "MyKotlinDLSTest"
include(":app")

再次运行,可以发现也正常运行 !

其他

感谢 Kotlin Jetpack 实战 | 02. Gradle Kotlin DSL

上一篇:RL Implementation Tricks


下一篇:VS2013未能正确加载microsoft.visualstudio.editor.implementation.editorpackage