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,实现的逻辑基本一样,代码的书写使用Kotlin语言的风格,结合上一篇的内容,是不是对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,没有移除大家可以对比着看,区分之前的不同。
学到东西的小伙伴,记得点个赞哦!
下一篇可能会跟大家聊一聊Java以及Android性能优化方面的内容,希望可以和各位小老弟互相学习,共同进步,好了,高手话不多,告辞!
附一张运行截图:

image.png

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容