使用kts编写gradle

https://juejin.cn/post/6976825808870440996

groovy和kotin的区别

. kotlin groovy
自动代码补全
类型安全
源码快速导航
重构 自动 手动

着手更改项目中gradle脚本语言为kotlin

android项目使用gradle作为构造工具,项目目录下会生成settings.gradle,build.gradle,app/build.gradle

kotlin和groovy一些简单的语法差异

grrovy字符串可以使用单引号,而kotlin必须为双引号
groovy在方法调用时可以省略扩号,而kotlin不可省略
groovy分配属性时可以省略=赋值运算符,而kotlin不可省略

修改settings.gradle

修改扩展名为settings.gradle.kts,编辑器会提示你需要更改单引号为双引号,因为kotlin中字符串是使用双引号的!然后把include “:app” 更改为kotlin调用方法的形式 include(“:app”) 进行sync 这样settings.gradle就更改完成

1
include (":app")

修改build.gradle

1
2
3
4
5
plugins {
id ("com.android.application") version "8.0.0" apply false
id ("com.android.library") version "8.0.0" apply false
id ("org.jetbrains.kotlin.android") version "1.8.0" apply false
}

修改app/build.gralde

改扩展名为build.gradle.kts

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
}

android {
namespace = "com.zxj.kotlin_flow_learn"
compileSdk = 33

defaultConfig {
applicationId = "com.zxj.kotlin_flow_learn"
minSdk = 24
targetSdk = 33
versionCode = 1
versionName = "1.0"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}

buildTypes {
named("release") {
isMinifyEnabled = 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 {
compose = true
}

composeOptions {
kotlinCompilerExtensionVersion = "1.4.0"
}
}

dependencies {

implementation("androidx.core:core-ktx:1.10.1")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.6.1")
implementation("androidx.activity:activity-compose:1.7.2")

implementation(platform("androidx.compose:compose-bom:2022.10.00"))
implementation("androidx.compose.ui:ui")
implementation("androidx.compose.ui:ui-graphics")
implementation("androidx.compose.ui:ui-tooling-preview")
implementation("androidx.compose.material3:material3")

testImplementation("junit:junit:4.13.2")
androidTestImplementation("androidx.test.ext:junit:1.1.5")
androidTestImplementation("androidx.test.espresso:espresso-core:3.5.1")

androidTestImplementation(platform("androidx.compose:compose-bom:2022.10.00"))
androidTestImplementation("androidx.compose.ui:ui-test-junit4")
debugImplementation("androidx.compose.ui:ui-tooling")
debugImplementation("androidx.compose.ui:ui-test-manifest")

implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.6.4")
}