赞
踩
强烈推荐先看文章最后的彩蛋!!!
首先我们要先明确我们这么做的原因有哪些?是否值的我们从 groovy 迁移到 kotlin.
Kotlin 相比于 Groovy 有这么几个好处:
首先将所有的 build.gradle
修改为 build.gradle.kts
.这个时候运行会报语法错误的.
接下来开始修改,基本上就是使用kotlin的语法替代了原来的groovy语法(虽然我一直没有很了解groovy的语法 )
如果整个过程还不是很了解,下面是一个手把手替换步骤,按照下面步骤就可以了
更换名字为 settings.gradle.kts
将内容
rootProject.name='qc'
include ':app'
替换为
//声明使用build.gradle.kts来构建项目
rootProject.buildFileName = "build.gradle.kts"
include(":app")
一样的,先重命名加上 .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) }
老配方,加上.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 暂时还不能很好的支持… 很好…
我撤回前面说的好处,请暂时不要入坑…强迫症表示想死…建议等到工具链完善之后再使用
再见!!
我的博客 leonchen1024.com
我的 GitHub https://github.com/LeonChen1024
微信公众号
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。