เปรียบเทียบ Lightbuild กับ Kotlin DSL

หากคุณคุ้นเคยกับการกำหนดค่าบิลด์ Android ด้วย Kotlin DSL หน้านี้จะแสดงวิธีแมปการกำหนดค่า Kotlin DSL ทั่วไปกับ Lightbuild

ตัวอย่างในหน้านี้ใช้ Kotlin DSL และปลั๊กอิน Android Gradle (AGP) 9.0 ขึ้นไป ซึ่งรวมถึงการรองรับ Kotlin ในตัว

ไฟล์บิลด์

ตารางต่อไปนี้แสดงไฟล์บิลด์ Lightbuild ที่เทียบเท่ากับไฟล์บิลด์ Kotlin DSL แต่ละไฟล์

Kotlin DSL Lightbuild วัตถุประสงค์
settings.gradle.kts project.lightbuild.yaml ชื่อโปรเจ็กต์ โมดูล และที่เก็บข้อมูล
รูท build.gradle.kts และ gradle-wrapper.properties project.lightbuild.yaml เวอร์ชันของเครื่องมือสร้างและ Toolchain
โมดูล build.gradle.kts โมดูล lightbuild.yaml ประเภทโมดูล, การตั้งค่า Android, ทรัพยากร Dependency และการทดสอบ
gradle.lockfile resolved.deps ติดตามเวอร์ชันของทรัพยากร Dependency

การตั้งค่าโปรเจ็กต์

ใน Kotlin DSL ไฟล์ settings.gradle.kts จะกำหนดชื่อโปรเจ็กต์ โมดูล และที่เก็บเพื่อแก้ไขทรัพยากร Dependency จาก

dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.name = "MyApp"
include(":app")
include(":app-core")

ใน Lightbuild ไฟล์ project.lightbuild.yaml จะกำหนดการตั้งค่าเดียวกัน ดังนี้

project:
  name: MyApp
modules:
  - app
  - app-core
repositories:
  - google
  - mavenCentral

ตารางต่อไปนี้เปรียบเทียบการตั้งค่าทั่วทั้งโปรเจ็กต์

แนวคิด Kotlin DSL Lightbuild
ชื่อโปรเจ็กต์ rootProject.name project.name
โมดูล include(":app") modules
ที่เก็บ repositories ใน dependencyResolutionManagement repositories
ที่เก็บ Maven ที่กำหนดเอง maven { url = uri("https://maven.example.com/releases") } mavenกับnameและurl
ข้อมูลเข้าสู่ระบบของที่เก็บ credentials ในบล็อก maven credentials พร้อมคีย์ที่คุณกำหนดใน local.properties
เวอร์ชันเครื่องมือบิลด์ เวอร์ชัน Gradle ใน gradle-wrapper.properties และเวอร์ชัน AGP ในบล็อก plugins lightbuild.version
เวอร์ชัน Kotlin มาพร้อมกับ AGP หากต้องการใช้เวอร์ชันที่ใหม่กว่า ให้เพิ่ม kotlin-gradle-plugin ลงใน classpath ของ buildscript build.kotlin.version
เวอร์ชัน Java kotlin { jvmToolchain(17) } build.java.version

ประเภทโมดูลและปลั๊กอิน

ใน Kotlin DSL คุณเลือกประเภทของโมดูลโดยใช้ปลั๊กอิน และใช้ปลั๊กอินอื่นๆ เพื่อเพิ่มฟีเจอร์ เช่น KSP ใน Lightbuild คุณจะตั้งค่า type ของโมดูล และ Lightbuild จะใช้ฟีเจอร์ที่การกำหนดค่าของคุณต้องการ ดังนี้

แนวคิด Kotlin DSL Lightbuild
โมดูลแอป id("com.android.application") type: application
โมดูลคลัง id("com.android.library") type: library
โมดูล Kotlin Multiplatform (KMP) id("org.jetbrains.kotlin.multiplatform") ยังไม่รองรับในตอนนี้
Kotlin มีอยู่ใน AGP 9.0 ขึ้นไป ในตัว
KSP id("com.google.devtools.ksp") ระบบจะนำไปใช้โดยอัตโนมัติเมื่อคุณประกาศ kspProcessors
บัฟเฟอร์โปรโตคอล id("com.google.protobuf") com.android.tools.lightbuild.protobuf ใน optionalFeatures
แคตตาล็อกเวอร์ชัน libs.versions.toml ไฟล์ ยังไม่รองรับในตอนนี้
ปลั๊กอินอื่นๆ ใช้ปลั๊กอิน Gradle ยังไม่รองรับในตอนนี้

การตั้งค่า Android

ใน Kotlin DSL คุณจะกำหนดการตั้งค่า Android ในบล็อก android ของไฟล์ build.gradle.kts ของโมดูล ดังนี้

plugins {
    id("com.android.application")
}

android {
    namespace = "com.example.myapp.app"
    compileSdk = 36

    defaultConfig {
        applicationId = "com.example.myapp"
        minSdk = 24
        targetSdk = 36
        versionCode = 1
        versionName = "1.0"
    }
}

ใน Lightbuild คุณจะกำหนดการตั้งค่าเดียวกันในบล็อก android ของไฟล์ lightbuild.yaml ของโมดูล ดังนี้

platform: android
type: application
android:
  namespace: com.example.myapp.app
  compileSdk:
    version:
      release:
        apiLevel: 36
  minSdk:
    version:
      release:
        apiLevel: 24
  targetSdk:
    version:
      release:
        apiLevel: 36
  applicationId: com.example.myapp
  versionCode: 1
  versionName: "1.0"

ตารางต่อไปนี้จะเปรียบเทียบการตั้งค่าเหล่านี้

การตั้งค่า Kotlin DSL Lightbuild
เนมสเปซ namespace namespace
คอมไพล์ SDK compileSdk = 36 compileSdk.version.release.apiLevel: 36
SDK ขั้นต่ำ minSdk = 24 ใน defaultConfig minSdk.version.release.apiLevel: 24
SDK เป้าหมาย targetSdk = 36 ใน defaultConfig targetSdk.version.release.apiLevel: 36
รหัสแอปพลิเคชัน applicationId ใน defaultConfig applicationId
รหัสรุ่น versionCode ใน defaultConfig versionCode
ชื่อเวอร์ชัน versionName ใน defaultConfig versionName
การรับรองรุ่น signingConfigs และ signingConfig ในประเภทบิลด์ release packaging.release.signing

ความสัมพันธ์

ใน Kotlin DSL คุณจะประกาศทรัพยากร Dependency ในบล็อก dependencies โดยใช้การกำหนดค่าต่างๆ เช่น implementation

dependencies {
    implementation(project(":app-core"))
    implementation("androidx.core:core-ktx:1.18.0")
}

ใน Lightbuild คุณจะแสดงรายการทรัพยากร Dependency ของโมดูลในส่วน module และไลบรารีภายนอกในส่วน maven ดังนี้

android:
  dependencies:
    module:
      - //app-core
    maven:
      - androidx.core:core-ktx:1.18.0

Kotlin DSL ใช้การกำหนดค่าอื่นๆ เช่น api, compileOnly และ runtimeOnly เพื่อควบคุมวิธีที่โมดูลใช้ทรัพยากร Dependency ดังนี้

dependencies {
    api(project(":data"))
    compileOnly("androidx.annotation:annotation:1.9.1")
}

ใน Lightbuild คุณจะเพิ่มการตั้งค่า compile และ runtime ลงในการขึ้นต่อกันแทน ดังนี้

android:
  dependencies:
    module:
      - //data:
          compile: public
    maven:
      - androidx.annotation:annotation:1.9.1:
          runtime: off

ดูข้อมูลเพิ่มเติมเกี่ยวกับการตั้งค่าเหล่านี้ได้ที่การตั้งค่าทรัพยากร Dependency

ตารางต่อไปนี้เปรียบเทียบการประกาศทรัพยากร Dependency ทั่วไป

การขึ้นต่อกัน Kotlin DSL Lightbuild
โมดูลในโปรเจ็กต์ implementation(project(":app-core")) //app-core ใน android.dependencies.module
คลังภายนอก implementation("group:artifact:version") group:artifact:version ใน android.dependencies.maven
ทรัพยากร Dependency ของ API api("group:artifact:version") group:artifact:version ใน android.dependencies.maven โดยมี compile: public
ทรัพยากร Dependency แบบคอมไพล์เท่านั้น compileOnly("group:artifact:version") group:artifact:version ใน android.dependencies.maven โดยมี runtime: off
ทรัพยากร Dependency เฉพาะรันไทม์ runtimeOnly("group:artifact:version") group:artifact:version ใน android.dependencies.maven โดยมี compile: off
โปรเซสเซอร์ KSP ksp("group:artifact:version") group:artifact:version ใน android.kspProcessors.maven
ทรัพยากร Dependency ของการทดสอบในเครื่อง testImplementation("group:artifact:version") group:artifact:version ใน android.tests.test.dependencies.maven
ทรัพยากร Dependency ของการทดสอบที่มีการวัดคุม androidTestImplementation("group:artifact:version") group:artifact:version ใน android.tests.androidTest.dependencies.maven

KSP

ใน Kotlin DSL คุณจะใช้ปลั๊กอิน KSP และเพิ่มโปรเซสเซอร์ด้วยการกำหนดค่า ksp

plugins {
    id("com.android.library")
    id("com.google.devtools.ksp")
}

dependencies {
    implementation("com.squareup.moshi:moshi:1.15.2")
    implementation("com.squareup.moshi:moshi-kotlin:1.15.2")
    ksp("com.squareup.moshi:moshi-kotlin-codegen:1.15.2")
}

ใน Lightbuild คุณจะเพิ่มโปรเซสเซอร์ลงในบล็อก kspProcessors และ Lightbuild จะใช้ปลั๊กอิน KSP โดยอัตโนมัติ ดังนี้

type: library
android:
  dependencies:
    maven:
      - com.squareup.moshi:moshi:1.15.2
      - com.squareup.moshi:moshi-kotlin:1.15.2
  kspProcessors:
    maven:
      - com.squareup.moshi:moshi-kotlin-codegen:1.15.2

การทดสอบ

ใน Kotlin DSL คุณตั้งค่าโปรแกรมเรียกใช้การทดสอบการวัดคุมใน defaultConfig และเพิ่มทรัพยากร Dependency ของการทดสอบด้วยการกำหนดค่า testImplementation และ androidTestImplementation ดังนี้

android {
    defaultConfig {
        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }
}

dependencies {
    testImplementation("junit:junit:4.13.2")
    androidTestImplementation("androidx.test.ext:junit:1.3.0")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.7.0")
}

ใน Lightbuild คุณจะกำหนดค่าการทดสอบในเครื่องใน tests.test และการทดสอบการวัดคุมใน tests.androidTest ดังนี้

android:
  tests:
    test:
      dependencies:
        maven:
          - junit:junit:4.13.2
    androidTest:
      runner: androidx.test.runner.AndroidJUnitRunner
      dependencies:
        maven:
          - androidx.test.ext:junit:1.3.0
          - androidx.test.espresso:espresso-core:3.7.0

สร้างคำสั่ง

เมื่อใช้ Kotlin DSL คุณจะสร้างและทดสอบจากบรรทัดคำสั่งได้โดยเรียกใช้งาน Gradle ด้วย Wrapper ของ Gradle เมื่อใช้ Lightbuild คุณจะใช้ Lightbuild CLI

งาน Gradle Wrapper Lightbuild CLI
สร้างโมดูลแอป ./gradlew :app:assembleDebug lightbuild build app
สร้างเป้าหมายทั้งหมด ./gradlew assemble lightbuild build
เรียกใช้การทดสอบในเครื่องของโมดูล ./gradlew :app-core:test lightbuild test app-core:hostTest
เรียกใช้การทดสอบที่มีการวัดคุมของโมดูล ./gradlew :app-core:connectedAndroidTest lightbuild test app-core:deviceTest
เรียกใช้การทดสอบทั้งหมด ./gradlew test connectedAndroidTest lightbuild test
นำเอาต์พุตบิลด์และแคชออก ./gradlew clean lightbuild clean

ดูข้อมูลเพิ่มเติมได้ที่สร้างแอปจากบรรทัดคำสั่งและสร้างโปรเจ็กต์ Lightbuild จากบรรทัดคำสั่ง

การล็อกทรัพยากร Dependency

เมื่อใช้ Kotlin DSL การล็อกทรัพยากร Dependency จะเป็นตัวเลือกที่ไม่บังคับ โดยคุณสามารถเปิดใช้ในสคริปต์บิลด์ แล้วเขียนไฟล์ล็อกโดยเรียกใช้บิลด์ที่มีแฟล็ก --write-locks ใน Lightbuild ทุกโมดูลจะมีไฟล์ resolved.deps ที่บันทึกทรัพยากร Dependency ที่ได้รับการแก้ไขแล้วของโมดูล ซึ่ง Lightbuild จะสร้างขึ้นโดยอัตโนมัติ เช่นเดียวกับไฟล์ล็อกของ Gradle ให้เช็คอินไฟล์ resolved.deps ลงในการควบคุมเวอร์ชัน

หากต้องการล็อกทรัพยากร Dependency เช่น ในบิลด์ CI ให้เพิ่มแฟล็ก --locked-dependencies ลงใน lightbuild resolve หรือ lightbuild test เมื่อใช้แฟล็กนี้ คำสั่งจะดำเนินการไม่สำเร็จหากไฟล์ resolved.deps ไม่ตรงกับทรัพยากร Dependency ของโมดูล แทนที่จะอัปเดตไฟล์

lightbuild resolve --locked-dependencies

ฟีเจอร์ที่ไม่รองรับ

Lightbuild ไม่รองรับฟีเจอร์ Kotlin DSL ต่อไปนี้

  • แคตตาล็อกเวอร์ชันและปลั๊กอินแบบแผน
  • ปลั๊กอินที่กำหนดเอง
  • เวอร์ชันผลิตภัณฑ์ แต่ให้สร้างโมดูลแอปแยกต่างหากสำหรับแอปแต่ละเวอร์ชันแทน ดูข้อมูลเพิ่มเติมได้ที่โมดูลแอปหลายโมดูล
  • โมดูล Kotlin Multiplatform (KMP)
  • การแก้ไขทรัพยากร Dependency ผ่านพร็อกซีเซิร์ฟเวอร์

นอกจากนี้ โปรเจ็กต์จะใช้โมดูล Kotlin DSL และ Lightbuild ร่วมกันไม่ได้ ดูปัญหาที่ทราบได้ที่บันทึกประจำรุ่นของ Lightbuild