迁移
迁移settings.gradle
迁移时,先把文件名改为 settings.gradle.kts,然后 sync gradle。就完事儿了。因为经过前面两部操作,settings.gradle 当中的这一行代码已经是合法的 Kotlin 代码了。迁移后加入了阿里云镜像如下:
pluginManagement {
repositories {
maven {
setUrl("https://maven.aliyun.com/repository/public")
}
maven {
setUrl("https://maven.aliyun.com/repository/central")
}
maven {
setUrl("https://maven.aliyun.com/repository/google")
}
maven {
setUrl("https://maven.aliyun.com/repository/gradle-plugin")
}
gradlePluginPortal()
google()
mavenCentral()
}
}
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
maven {
setUrl("https://maven.aliyun.com/repository/public")
}
maven {
setUrl("https://maven.aliyun.com/repository/central")
}
maven {
setUrl("https://maven.aliyun.com/repository/google")
}
maven {
setUrl("https://maven.aliyun.com/repository/gradle-plugin")
}
maven {
setUrl("https://www.jitpack.io")
}
google()
mavenCentral()
}
}
rootProject.name = "TestKts"
include(":app")
//include(":version")
includeBuild("version")
迁移根工程下的build.gradle
也是直接修改文件名,然后修改内容。上面的buildscript是需要引入第三方库的插件需要新增加的内容,下面是迁移修改后的内容如下:
buildscript {
// repositories {
//// maven { url 'https://maven.aliyun.com/repository/public' }
//// maven { url 'https://maven.aliyun.com/repository/central' }
//// maven { url 'https://maven.aliyun.com/repository/google' }
//// maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
//// maven { url 'https://www.jitpack.io' }
// google()
// mavenCentral()
// }
dependencies {
// classpath "com.yanzhenjie.andserver:plugin:2.1.9"
// GreenDao插件
classpath("org.greenrobot:greendao-gradle-plugin:3.3.0")
}
}
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
// id 'com.android.application' version '7.2.1' apply false
// id 'com.android.library' version '7.2.1' apply false
// id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
id("com.android.application") version "7.2.1" apply false
id("com.android.library") version "7.2.1" apply false
id("org.jetbrains.kotlin.android") version "1.6.10" apply false
}
tasks.register("clean", Delete::class) {
delete(rootProject.buildDir)
}
module 的build.gradle
最后使用的 module 的build.gradle修改文件名,后面加.kts,然后对里面的内容进行kotlin语法修改接口,示例如下:
import com.kiwilss.version.*;
plugins {
// id 'com.android.application'
// id 'org.jetbrains.kotlin.android'
id("com.android.application")
id("org.jetbrains.kotlin.android")
id("com.kiwilss.version")
//apply plugin: 'org.greenrobot.greendao' // apply plugin
id("org.greenrobot.greendao")
}
android {
compileSdk = AndroidConfig.compileSdkVersion
defaultConfig {
applicationId = AndroidConfig.applicationId
minSdk = AndroidConfig.minSdkVersion
targetSdk = AndroidConfig.targetSdkVersion
versionCode = AndroidConfig.versionCode
versionName = AndroidConfig.versionName
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
getByName("release"){
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
}
// release {
// minifyEnabled false
// proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
// }
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
buildFeatures {
viewBinding = true
}
}
dependencies {
implementation (Libs.core_ktx)
implementation (Libs.appcompat)
implementation (Libs.material)
implementation (Libs.constraintlayout)
implementation (Libs.navigation_fragment_ktx)
implementation (Libs.navigation_ui_ktx)
testImplementation (Libs.junit)
androidTestImplementation (Libs.ext_junit)
androidTestImplementation (Libs.espresso_core)
implementation(Libs.lutils)
implementation(Libs.greendao)
}
参考
demo
快速迁移 Gradle 脚本至 KTS
Android Gradle脚本从Groovy迁移到Kotlin DSL
使用Kotlin编写gradle脚本
升级新版本Android Studio之后找不到了插件classpath的位置