文章目錄
我覺得你們可以先看最後的彩蛋再決定是不是要看
強烈推薦先看文章最後的彩蛋!!!
爲什麼選擇 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
微信公衆號