迁移

迁移settings.gradle

迁移时,先把文件名改为 settings.gradle.kts,然后 sync gradle。就完事儿了。因为经过前面两部操作,settings.gradle 当中的这一行代码已经是合法的 Kotlin 代码了。迁移后加入了阿里云镜像如下:

  1. pluginManagement {
  2. repositories {
  3. maven {
  4. setUrl("https://maven.aliyun.com/repository/public")
  5. }
  6. maven {
  7. setUrl("https://maven.aliyun.com/repository/central")
  8. }
  9. maven {
  10. setUrl("https://maven.aliyun.com/repository/google")
  11. }
  12. maven {
  13. setUrl("https://maven.aliyun.com/repository/gradle-plugin")
  14. }
  15. gradlePluginPortal()
  16. google()
  17. mavenCentral()
  18. }
  19. }
  20. dependencyResolutionManagement {
  21. repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
  22. repositories {
  23. maven {
  24. setUrl("https://maven.aliyun.com/repository/public")
  25. }
  26. maven {
  27. setUrl("https://maven.aliyun.com/repository/central")
  28. }
  29. maven {
  30. setUrl("https://maven.aliyun.com/repository/google")
  31. }
  32. maven {
  33. setUrl("https://maven.aliyun.com/repository/gradle-plugin")
  34. }
  35. maven {
  36. setUrl("https://www.jitpack.io")
  37. }
  38. google()
  39. mavenCentral()
  40. }
  41. }
  42. rootProject.name = "TestKts"
  43. include(":app")
  44. //include(":version")
  45. includeBuild("version")

迁移根工程下的build.gradle

也是直接修改文件名,然后修改内容。上面的buildscript是需要引入第三方库的插件需要新增加的内容,下面是迁移修改后的内容如下:

  1. buildscript {
  2. // repositories {
  3. //// maven { url 'https://maven.aliyun.com/repository/public' }
  4. //// maven { url 'https://maven.aliyun.com/repository/central' }
  5. //// maven { url 'https://maven.aliyun.com/repository/google' }
  6. //// maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
  7. //// maven { url 'https://www.jitpack.io' }
  8. // google()
  9. // mavenCentral()
  10. // }
  11. dependencies {
  12. // classpath "com.yanzhenjie.andserver:plugin:2.1.9"
  13. // GreenDao插件
  14. classpath("org.greenrobot:greendao-gradle-plugin:3.3.0")
  15. }
  16. }
  17. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  18. plugins {
  19. // id 'com.android.application' version '7.2.1' apply false
  20. // id 'com.android.library' version '7.2.1' apply false
  21. // id 'org.jetbrains.kotlin.android' version '1.6.10' apply false
  22. id("com.android.application") version "7.2.1" apply false
  23. id("com.android.library") version "7.2.1" apply false
  24. id("org.jetbrains.kotlin.android") version "1.6.10" apply false
  25. }
  26. tasks.register("clean", Delete::class) {
  27. delete(rootProject.buildDir)
  28. }

module 的build.gradle

最后使用的 module 的build.gradle修改文件名,后面加.kts,然后对里面的内容进行kotlin语法修改接口,示例如下:

  1. import com.kiwilss.version.*;
  2. plugins {
  3. // id 'com.android.application'
  4. // id 'org.jetbrains.kotlin.android'
  5. id("com.android.application")
  6. id("org.jetbrains.kotlin.android")
  7. id("com.kiwilss.version")
  8. //apply plugin: 'org.greenrobot.greendao' // apply plugin
  9. id("org.greenrobot.greendao")
  10. }
  11. android {
  12. compileSdk = AndroidConfig.compileSdkVersion
  13. defaultConfig {
  14. applicationId = AndroidConfig.applicationId
  15. minSdk = AndroidConfig.minSdkVersion
  16. targetSdk = AndroidConfig.targetSdkVersion
  17. versionCode = AndroidConfig.versionCode
  18. versionName = AndroidConfig.versionName
  19. testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
  20. }
  21. buildTypes {
  22. getByName("release"){
  23. isMinifyEnabled = false
  24. proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")
  25. }
  26. // release {
  27. // minifyEnabled false
  28. // proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
  29. // }
  30. }
  31. compileOptions {
  32. sourceCompatibility = JavaVersion.VERSION_1_8
  33. targetCompatibility = JavaVersion.VERSION_1_8
  34. }
  35. kotlinOptions {
  36. jvmTarget = "1.8"
  37. }
  38. buildFeatures {
  39. viewBinding = true
  40. }
  41. }
  42. dependencies {
  43. implementation (Libs.core_ktx)
  44. implementation (Libs.appcompat)
  45. implementation (Libs.material)
  46. implementation (Libs.constraintlayout)
  47. implementation (Libs.navigation_fragment_ktx)
  48. implementation (Libs.navigation_ui_ktx)
  49. testImplementation (Libs.junit)
  50. androidTestImplementation (Libs.ext_junit)
  51. androidTestImplementation (Libs.espresso_core)
  52. implementation(Libs.lutils)
  53. implementation(Libs.greendao)
  54. }

参考

demo
快速迁移 Gradle 脚本至 KTS
Android Gradle脚本从Groovy迁移到Kotlin DSL
使用Kotlin编写gradle脚本
升级新版本Android Studio之后找不到了插件classpath的位置