最近在写多模组的专案,也顺便读了 Effective Kotlin ,不过我决定先分享一下,使用 Gradle 从 Groovy 到 Kotlin-dsl 的小心得
迁移
基本上非常简单,围着几个概念走就行
里面字串要用 double qoute 不能用 single qoute字串或布林需要用 = 赋值implementation 变成 function 了,所以要用 implementation("org.jetbrain....")最最基本的迁移大概长这样,那一个基础专案的设置会变得大概
差点忘了说,build.gradle -> build.gradle.kts
template
plugins { id("com.android.application") kotlin("android") kotlin("kapt") id("dagger.hilt.android.plugin")}android { compileSdk = ProjectConfig.compileSdk defaultConfig { applicationId = ProjectConfig.appId minSdk = ProjectConfig.minSdk targetSdk = ProjectConfig.targetSdk versionCode = ProjectConfig.versionCode versionName = ProjectConfig.versionName testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner" vectorDrawables { useSupportLibrary = true } } buildTypes { getByName("release"){ isMinifyEnabled = false proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro") } } compileOptions { sourceCompatibility = JavaVersion.VERSION_1_8 targetCompatibility = JavaVersion.VERSION_1_8 } kotlinOptions { jvmTarget = "1.8" } composeOptions { kotlinCompilerExtensionVersion = Compose.composeCompilerVersion } buildFeatures { compose = true }}kapt { correctErrorTypes = true}dependencies { implementation(Compose.compiler) implementation(Compose.ui) implementation(Compose.uiToolingPreview) implementation(Compose.hiltNavigationCompose) implementation(Compose.material) implementation(Compose.runtime) implementation(Compose.navigation) implementation(Compose.viewModelCompose) implementation(Compose.activityCompose) implementation(DaggerHilt.hiltAndroid) kapt(DaggerHilt.daggerHiltCompiler) implementation(project(Modules.core)) testImplementation(Testing.junit4) testImplementation(Testing.junitAndroidExt) testImplementation(Testing.truth) testImplementation(Testing.coroutines) testImplementation(Testing.turbine) testImplementation(Testing.composeUiTest) testImplementation(Testing.mockk) testImplementation(Testing.mockWebServer) androidTestImplementation(Testing.junit4) androidTestImplementation(Testing.junitAndroidExt) androidTestImplementation(Testing.truth) androidTestImplementation(Testing.coroutines) androidTestImplementation(Testing.turbine) androidTestImplementation(Testing.composeUiTest) androidTestImplementation(Testing.mockkAndroid) androidTestImplementation(Testing.mockWebServer) androidTestImplementation(Testing.hiltTesting) kaptAndroidTest(DaggerHilt.daggerHiltCompiler) androidTestImplementation(Testing.testRunner)}
现在里面那些 Compose.compiler
被我抓到 buildSrc 管理了,之后再说,如果不知道的就用implementation("androidx.compose.compiler:compiler:$compose_version")
其实一样啦
迁移到 kotlin dsl 的优点
Kotlin 真香更容易地整理资源,也能为其用 Kotlin 写 class, function 等管理Type safe更容易使用 ide 重构踩过的坑
刚迁移的时候,不知道怎么下关键字,一直查不到 maven 要怎么改,后来爬别人的 source code 才看到写法
//beforerepositories { mavenCentral() maven { "https://kotlin.bintray.com/ktor" } //after maven ( url = "https://kotlin.bintray.com/ktor" )
其他设置
之后补充:)