Gradle Kotlin DSL

Gradle Kotlin DSL

[TOC]

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

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

为什么选择 Gradle Kotlin DSL

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

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

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

开始

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

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

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

1. 修改settings.gradle

更换名字为 settings.gradle.kts

将内容

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

替换为

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

2. 修改项目 build.gradle

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

接着将

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// 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
}

替换为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// 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 后缀

然后将

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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'
}

替换为

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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

微信公众号

wechat

You forgot to set the business and currency_code for Paypal. Please set it in _config.yml.
You forgot to set the url Patreon. Please set it in _config.yml.
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×