Kotlin代替Groovy編寫Gradle(2)-Android項目改造

上一篇文章介紹了如何使用Kotlin來編寫Gradle,這一篇我將帶各位小伙伴進行Android項目的Gradle改造,將Groovy徹底踢出Android,然后安安靜靜的寫Kotlin。
廢話不多說,擼起袖子就是干!下面直接開始。
大家項目里面的Gradle文件是不是這樣的

buildscript {
    ext.kotlin_version = '1.3.11'
    repositories {
        google()
        jcenter()
        
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.3.0'
        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()
        jcenter()
        
    }
}

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

是不是這樣的

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.huace.mkotlin"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.core:core-ktx:1.1.0-alpha03'
    implementation 'com.google.android.material:material:1.0.0-beta01'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.2'
    implementation 'androidx.vectordrawable:vectordrawable:1.0.0-beta01'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'
}

我將用上一篇文章中的知識點,對這兩個文件進行改造,下面開始
第一步、改文件格式(上一篇說過了,這邊不重復)
第二步、修改settings.gradle.kts,和以往的略有不同,需要指定buildFile,較為簡單。

rootProject.buildFileName = "build.gradle.kts"

include(":app")

第三步、修改Project的build.gradle.kts,實現(xiàn)的邏輯基本一樣,代碼的書寫使用Kotlin語言的風格,結(jié)合上一篇的內(nèi)容,是不是對Gradle的理解更進一步。


buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath("com.android.tools.build:gradle:3.3.0")
        classpath(kotlin("gradle-plugin", version = "1.3.11"))

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

allprojects {
    repositories {
        google()
        jcenter()
    }
}

tasks.register("clean", Delete::class) {
    delete(rootProject.buildDir)
}

第四步、修改App的build.gradle.kts,其中kotlin版本引入方式略有不同

import org.jetbrains.kotlin.config.KotlinCompilerVersion

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

android {
    compileSdkVersion(28)
    defaultConfig {
        applicationId = "org.gradle.kotlin.dsl.samples.androidstudio"
        minSdkVersion(15)
        targetSdkVersion(28)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner = "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        getByName("release") {
            isMinifyEnabled = false
            proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
        }
    }
}

dependencies {
    implementation(fileTree(mapOf("dir" to "libs", "include" to listOf("*.jar"))))
    implementation(kotlin("stdlib-jdk7", KotlinCompilerVersion.VERSION))
    implementation("androidx.appcompat:appcompat:1.0.0-beta01")
    implementation("androidx.core:core-ktx:1.1.0-alpha03")
    implementation("com.google.android.material:material:1.0.0-beta01")
    implementation("androidx.constraintlayout:constraintlayout:1.1.2")
    implementation("androidx.vectordrawable:vectordrawable:1.0.0-beta01")
    testImplementation("junit:junit:4.12")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.1.0-alpha4")
}

改造項目地址:https://github.com/ChangerD/MKotLin_Android.git
原來的gradle文件我命名成了build.gradle.old,沒有移除大家可以對比著看,區(qū)分之前的不同。
學到東西的小伙伴,記得點個贊哦!
下一篇可能會跟大家聊一聊Java以及Android性能優(yōu)化方面的內(nèi)容,希望可以和各位小老弟互相學習,共同進步,好了,高手話不多,告辭!
附一張運行截圖:

image.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容