iT邦幫忙

0

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

最近在寫多模組的專案,也順便讀了 Effective Kotlin ,不過我決定先分享一下,使用 Gradle 從 Groovy 到 Kotlin-dsl 的小心得

遷移

基本上非常簡單,圍著幾個概念走就行

  1. 裡面字串要用 double qoute 不能用 single qoute
  2. 字串或布林需要用 = 賦值
  3. 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 的優點

  1. Kotlin 真香
  2. 更容易地整理資源,也能為其用 Kotlin 寫 class, function 等管理
  3. Type safe
  4. 更容易使用 ide 重構

踩過的坑

剛遷移的時候,不知道怎麼下關鍵字,一直查不到 maven 要怎麼改,後來爬別人的 source code 才看到寫法

//before
repositories {
    mavenCentral()
    maven {  "https://kotlin.bintray.com/ktor" }
    
//after
    maven ( url = "https://kotlin.bintray.com/ktor" )

其他設置

之後補充:)


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言