Kotlin-gradle-dsl 使用心得 part -1

最近在写多模组的专案,也顺便读了 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" )

其他设置

之后补充:)


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章