Gradle Kotlin DSL

首发地址点这里

我觉得你们可以先看最后的彩蛋再决定是不是要看

强烈推荐先看文章最后的彩蛋!!!

为什么选择 Gradle Kotlin DSL

首先我们要先明确我们这么做的原因有哪些?是否值的我们从 groovy 迁移到 kotlin.

Kotlin 相比于 Groovy 有这么几个好处:

  • 可以更好的支持自动补全和内容提示
  • 源码导航
  • 重构等支持
  • 语言和Android开发一致,更加舒适

开始

首先将所有的 build.gradle 修改为 build.gradle.kts .这个时候运行会报语法错误的.

接下来开始修改,基本上就是使用kotlin的语法替代了原来的groovy语法(虽然我一直没有很了解groovy的语法 )

如果整个过程还不是很了解,下面是一个手把手替换步骤,按照下面步骤就可以了

1. 修改settings.gradle

更换名字为 settings.gradle.kts

将内容

rootProject.name='qc'
include ':app'

替换为

//声明使用build.gradle.kts来构建项目
rootProject.buildFileName = "build.gradle.kts"
include(":app")

首发地址点这里

2. 修改项目 build.gradle

一样的,先重命名加上 .kts 后缀为 build.gradle

接着将

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

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

替换为

// Top-level build file where you can add configuration options common to all sub-projects/modules.

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

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

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

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

3. 修改module 的 build.gradle

老配方,加上.kts 后缀

然后将

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.darenscm.www.qc"
        minSdkVersion 21
        targetSdkVersion 29
        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.1.0'
}

替换为

import org.jetbrains.kotlin.config.KotlinCompilerVersion

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

//apply plugin: 'com.android.application'
//apply plugin: 'kotlin-android'
//apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion(29)

    defaultConfig {
        applicationId= "com.darenscm.www.qc"
        minSdkVersion(21)
        targetSdkVersion(29)
        versionCode = 1
        versionName = "1.0"
        testInstrumentationRunner= "androidx.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.1.0")
}

至此就可以运行成功了

彩蛋!!!

成功运行后,IDE还是全部报红!!!

全部报红…很好…很好… #%$@%#&%&#%#&%@&^

IDE 暂时还不能很好的支持… 很好…

我撤回前面说的好处,请暂时不要入坑…强迫症表示想死…建议等到工具链完善之后再使用

再见!!

About Me

我的博客 leonchen1024.com

我的 GitHub https://github.com/LeonChen1024

微信公众号

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值