简介
- 官方文档:https://docs.gradle.org/current/userguide/kotlin_dsl.html
- kotlin优势:可自动代码补全,自动重构等
- 源码:https://github.com/Peakmain/BasicLibrary
实战
重构setting.gradle,重命名为setting.gradle.kts
setting.gradle的源码
include ':basicLibrary'
include ':app'
rootProject.name = "BasicLibary"
setting.gradle.kts的源码
include (":app")
include (":basicLibrary")
rootProject.name = "BasicLibrary"
rootProject.buildFileName="build.gradle.kts"
- include ''->include("")
- rootProject.buildFileName指向我们根目录下的build文件(即将重构build.gradle之后的文件build.gradle.kts)
重构根目录下的build.gradle,重命名为build.gradle.kts
- 左边是build.gradle,右边是build.gradle.kts
- classpath ""->classpath ("")
- KotlinConstants.gradle_version和KotlinConstants.kotlin_version,大家可以先不管,就是对应的版本号,后面会介绍
重构basicLibrary下的build.gradle,重命名为build.gradle.kts
- apply->plugins
-
kotlin前缀的都可以换成kotlin("")
- 大家可以直接查看对应的对比
- 这时候肯定有人好奇了上面的KotlinConstants.gradle_version和KotlinConstants.kotlin_version到底是怎么配置的,接下来就说说buildSrc
buildSrc
- 概念
当你运行Gradle时,它会检查项目中是否存在一个名为buildSrc的目录。
然后Gradle会自动编译并测试这段代码,并将其放入构建脚本的类路径中。您不需要提供任何进一步的操作提示。
注意:buildSrc每次依赖更新将重新构建整个项目
- 新建目录为buildSrc,并新建setting.gradle.kts
rootProject.buildFileName = "build.gradle.kts"
- 新建build.gradle.kts
apply {
plugin("kotlin")
}
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath(kotlin("gradle-plugin", "1.3.72"))
}
}
dependencies {
implementation(gradleKotlinDsl())
implementation(kotlin("stdlib", "1.3.72"))
}
repositories {
gradlePluginPortal()
}
1.72是我自己的kotlin版本,大家可以自行替换成自己项目中用到的kotlin版本
- 新建目录src/main/kotlin,并新建KotlinConstants.kt文件
object KotlinConstants {
const val gradle_version = "4.0.1"//gradle版本
const val kotlin_version = "1.3.72"//kotlin版本
}
- 之后我们还可以做版本的统一管理,比如依赖的管理和Android的配置管理
如:以下是我项目对的配置
object KotlinConstants {
const val gradle_version = "4.0.1"
const val kotlin_version = "1.3.72"
}
object AndroidConfigConstants {
const val compileSdkVersion = 30
const val minSdkVersion = 16
const val targetSdkVersion = 30
const val versionCode = 1
const val versionName = "1.0"
}
object DependenciesConfigConstants {
const val kotlin_stdlib = "org.jetbrains.kotlin:kotlin-stdlib:${KotlinConstants.kotlin_version}"
const val core_ktx = "androidx.core:core-ktx:1.3.2"
const val appcompat = "androidx.appcompat:appcompat:1.2.0"
const val kotlin_coroutines_core = "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.2.1"
const val kotlin_coroutines_android = "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.2.0"
const val okhttp3_logging_interceptor = "com.squareup.okhttp3:logging-interceptor:3.11.0"
const val retrofit_gson = "com.squareup.retrofit2:converter-gson:2.6.2"
const val retrofit_rxjava = "com.squareup.retrofit2:adapter-rxjava2:2.4.0"
const val retrofit_adapter =
"com.jakewharton.retrofit:retrofit2-kotlin-coroutines-adapter:0.9.2"
const val rxandroid = "io.reactivex.rxjava2:rxandroid:2.1.1"
const val mmkv = "com.tencent:mmkv:1.2.11"
const val cardview = "androidx.cardview:cardview:1.0.0"
const val databinding = "com.android.databinding:compiler:3.1.4"
const val basicUI = "com.github.Peakmain:BasicUI:1.1.12"
const val recyclerview = "androidx.recyclerview:recyclerview:1.2.0"
const val material = "com.google.android.material:material:1.3.0"
}
之后项目就可以变成这样
整体其实还是很简单,大家可以自行根据自己的项目进行修改,提升自己的逼格