ရိုးရိုးလေး ရှင်းပြမယ်
Gradle ကို ရိုးရိုးလေး ပြောရရင်
Gradle ဆိုတာ Kotlin project အများစုရဲ့ နောက်ကွယ်က build tool ပါ။ Project ကို ဖိုင်သေးသေးလေး အနည်းငယ်နဲ့ ဖော်ပြလိုက်ရင် ဘာတွေ download လုပ်ရမလဲ၊ ဘာတွေကို ဘယ်အစီအစဉ်နဲ့ compile လုပ်ရမလဲ၊ ဘာမှ မပြောင်းလို့ ဘာတွေကို ကျော်လို့ရလဲ ဆိုတာ Gradle က ကိုယ်တိုင် တွက်ချက်ပေးပါတယ်။ Site manager တစ်ယောက်ကို စဉ်းစားကြည့်ပါ: ပုံစံ (build.gradle.kts) နဲ့ ပစ္စည်းပေးသူစာရင်း (repository နဲ့ dependency) ကို လက်ထဲ ထည့်ပေးလိုက်ရင် manager က အလုပ်တစ်ခုချင်းစီကို သူ့ရှေ့အလုပ်တွေ ပြီးမှ စီစဉ်ပေးပါတယ်။
Gradle ကို repository ထဲမှာပဲ ရှိတဲ့ script သေးသေးလေးတစ်ခုဖြစ်တဲ့ Gradle Wrapper နဲ့ run ပါတယ်: macOS နဲ့ Linux မှာ ./gradlew၊ Windows မှာ gradlew.bat ပါ။ Wrapper က project လိုချင်တဲ့ Gradle version ကို download လုပ်ပေးတဲ့အတွက် teammate တိုင်းနဲ့ CI စက်တိုင်းက tool တူတူနဲ့ build လုပ်ပြီး ဘာမှ ကိုယ်တိုင် install လုပ်စရာ မလိုပါဘူး။
Problem → Why → Solution
ပြဿနာ: ကိုယ်တိုင်ရေးထားတဲ့ build
Build tool မရှိရင် 'ငါ့စက်မှာတော့ ရတယ်' ဆိုတာ ပုံမှန်ဖြစ်လာပါတယ်။ Teammate တစ်ယောက်က library jar တွေကို version တူတူနဲ့ ရဖို့၊ JDK တူတူနဲ့ compile လုပ်ဖို့ လိုပါတယ်။ Test class တွေက ပို့မယ့် jar ထဲ မဝင်ရပါဘူး။ Module နှစ်ခုမြောက်ကို ပထမ module က မသုံးခင် compile လုပ်ထားရပါမယ်။ ဒါတွေကို kotlinc နဲ့ copy ကူးထားတဲ့ jar တွေနဲ့ လက်နဲ့ လုပ်ရင် library အသစ်တိုင်း manual download ဖြစ်ပြီး upgrade တိုင်းမှာ အဲဒီ library လိုတဲ့ jar တွေကို ထပ်ရှာရပါတယ်၊ build လမ်းညွှန်ချက်ကလည်း တစ်ယောက်ယောက်ရဲ့ ခေါင်းထဲမှာ ဒါမှမဟုတ် ဘယ်သူမှ မပြင်တော့တဲ့ README ထဲမှာပဲ ရှိပါတယ်။
အောက်က script က လက်နဲ့ ကြိုးစားရေးကြတဲ့ ပုံမှန်ပုံစံပါ။ ဖိုင်တွေ မှန်တဲ့နေရာမှာ ရှိတဲ့စက်မှာပဲ အလုပ်လုပ်ပြီး library ရဲ့ ကိုယ်ပိုင် dependency တွေအကြောင်း ဘာမှ မသိသလို test တွေကို ဘာလုပ်ရမလဲဆိုတာလည်း မသိပါဘူး။
# hand-rolled build: works only on the machine that has these exact files
kotlinc src/main/kotlin/*.kt -cp libs/library-a.jar:libs/library-b.jar -d out
java -cp out:libs/library-a.jar:libs/library-b.jar MainKt
# Where are the tests? The jars library-a itself needs? The second module?
# Nobody remembers, and a new teammate has to guess.Build က graph ပြဿနာတစ်ခုပါ
Library တွေက library တွေအပေါ် မှီခိုပြီး module တွေက module တွေအပေါ်၊ task တွေက task တွေအပေါ် မှီခိုပါတယ်။ Build tool က အဲဒီ graph ကို declarative ဖိုင်ထဲ သိမ်းပြီး node တစ်ခုချင်းစီကို repository ကနေ တစ်ကြိမ်ပဲ download လုပ်ကာ မျှားတိုင်းကို လိုက်နာတဲ့ အစီအစဉ်နဲ့ run ပါတယ်။ Graph က data ဖြစ်သွားပြီဆိုရင် ဘာတွေ ပြောင်းလဲ၊ ဘာတွေ ပြန် build ရမလဲ၊ မျှဝေသုံးတဲ့ library ရဲ့ ဘယ် version က နိုင်မလဲ ဆိုတာတွေကို မေးလို့ရပါပြီ။
Core concept: ဖိုင်၊ layout၊ dependency နဲ့ task
Gradle project တစ်ခုကို root မှာရှိတဲ့ ဖိုင်အနည်းငယ်က ထိန်းချုပ်ပါတယ်။ settings.gradle.kts က root project ရဲ့ နာမည်ကို ပေးပြီး include(...) နဲ့ module တွေကို စာရင်းပေးပါတယ်။ build.gradle.kts က module တစ်ခုစီမှာ တစ်ခုစီရှိပြီး apply လုပ်မယ့် plugin, download လုပ်မယ့် repository, dependency နဲ့ task ပြင်ဆင်မှုတွေကို ကြေညာပါတယ်။ gradle/libs.versions.toml ကတော့ optional ဖြစ်ပြီး library version တွေကို နေရာတစ်ခုတည်းမှာ ထိန်းတဲ့ version catalog ပါ။ .kts ဆိုတာ Kotlin script ဖြစ်လို့ editor က autocomplete နဲ့ type check လုပ်ပေးနိုင်ပါတယ် (Groovy DSL ဖြစ်တဲ့ build.gradle ကိုလည်း Gradle က လက်ခံပါတယ်)။ Wrapper ဖိုင်တွေဖြစ်တဲ့ gradlew, gradlew.bat နဲ့ gradle/wrapper/ ကို version control ထဲ ထည့်ရပြီး build/ နဲ့ .gradle/ folder တွေကိုတော့ မထည့်ရပါဘူး။
Kotlin JVM plugin က ဖိုင်တွေ ထားရမယ့်နေရာအတွက် convention တစ်ခုကို လိုက်နာပါတယ်: production code ကို src/main/kotlin၊ resource တွေကို src/main/resources၊ test တွေကို src/test/kotlin မှာ ထားပါတယ်။ အဲဒီ folder တွေက main နဲ့ test source set တွေပါ။ Package တွေက folder တွေနဲ့ ကိုက်ညီတာ ပုံမှန်ဖြစ်လို့ com.example.shop.Cart က src/main/kotlin/com/example/shop/Cart.kt မှာ ရှိပါတယ်။ Gradle က compile လုပ်ပြီးသား class နဲ့ jar တွေကို build/ ထဲ ရေးတာမို့ ဘယ်အချိန်မဆို ဖျက်လို့ရပါတယ်၊ ပြန်ထုတ်ပေးလို့ပါ။
Dependency ကို group:artifact:version ပုံစံနဲ့ ရေးပါတယ် (ဥပမာ org.jetbrains.kotlinx:kotlinx-coroutines-core:<version>)။ Gradle က ကြေညာထားတဲ့ repository (များသောအားဖြင့် mavenCentral()) မှာ ရှာပါတယ်။ ရွေးလိုက်တဲ့ configuration က library ကို ဘယ်နေရာမှာ မြင်ရမလဲ ဆုံးဖြတ်ပါတယ်: implementation က ဒီ module ရဲ့ compile နဲ့ runtime classpath ပေါ်တင်ပြီး ကိုယ့်ကို မှီခိုတဲ့ module တွေရဲ့ compile classpath ပေါ်မှာတော့ မပေါ်စေပါဘူး၊ testImplementation က test တွေအတွက်ပဲ၊ runtimeOnly က program run တဲ့အချိန်မှာပဲ၊ compileOnly က compile လုပ်စဉ်မှာပဲ ထည့်ပါတယ်။ Library တွေက သူတို့ကိုယ်ပိုင် dependency တွေ (transitive dependency) ကို ပါလာပြီး လမ်းကြောင်းနှစ်ခုက module တူတူရဲ့ version မတူတာ တောင်းရင် Gradle ရဲ့ default က တောင်းထားတဲ့ version တွေထဲက အမြင့်ဆုံးကို ရွေးတာပါ။
Gradle လုပ်တဲ့ အရာအားလုံးက task ပါ: compileKotlin, compileTestKotlin, test, jar စသဖြင့်ပေါ့။ Task တွေက graph တစ်ခုဖြစ်ပြီး test က classes ကို၊ classes က compileKotlin ကို လိုအပ်ပါတယ်၊ Gradle က တောင်းထားတဲ့ task လိုတာကိုပဲ run ပါတယ်။ Run တစ်ခုမှာ အဆင့်သုံးဆင့် ရှိပါတယ်: initialization က settings.gradle.kts ကို ဖတ်၊ configuration က build.gradle.kts တိုင်းကို evaluate လုပ်ပြီး task graph ဆောက်၊ execution က ရွေးထားတဲ့ task တွေကို run ပါတယ်။ Input နဲ့ output မပြောင်းတဲ့ task ကို ကျော်ပြီး UP-TO-DATE လို့ ပြပါတယ်၊ ဒါကြောင့် ဒုတိယအကြိမ် build က ပထမထက် ပိုမြန်တာပါ။
| Command | လုပ်ဆောင်ချက် |
|---|---|
| ./gradlew build | အားလုံးကို compile, test, assemble လုပ် |
| ./gradlew test | test source set ကို compile လုပ်ပြီး run |
| ./gradlew run | application ကို စတင် (application plugin လိုတယ်) |
| ./gradlew dependencies | resolve လုပ်ပြီးသား dependency tree ကို print ထုတ် |
- settings.gradle.kts က module တွေကို ရွေး
- build.gradle.kts က plugin, dependency ကြေညာ
- Gradle က version ရွေးပြီး jar download
- Task တွေက လိုတဲ့ task တွေပြီးမှ run
Module နဲ့ task တွေကြား မျှားတွေကို ဆွဲပြနိုင်ရင် build အစီအစဉ်နဲ့ ပြောင်းလဲမှုတစ်ခုက ဘာတွေကို ပြန် build စေလဲ ကြိုတင် ခန့်မှန်းနိုင်ပါတယ်။
အထွေထွေ ပုံစံ
plugins {
kotlin("jvm") version "<kotlin-version>"
}
repositories { mavenCentral() }
dependencies {
implementation("group:artifact:version")
testImplementation(kotlin("test"))
}
tasks.test { useJUnitPlatform() }plugins က ဘာတွေ apply လုပ်မလဲ၊ repositories က ဘယ်ကနေ download လုပ်မလဲ၊ dependencies က ဘာတွေ လိုလဲ ဆိုတာကို ကြေညာပါတယ်။ ဒါတွေက Gradle ကို ညွှန်ကြားချက်ပေးတဲ့ declarative block တွေဖြစ်ပြီး code ကို run တာ မဟုတ်ပါဘူး။
Worked example: ရိုးရိုး Kotlin နဲ့ build graph
ဒီစာမျက်နှာမှာ Gradle script အစစ်ကို run လို့ မရတဲ့အတွက် worked example က Gradle က ကိုယ့်ကိုယ်စား လုပ်ပေးတဲ့ အရာနှစ်ခုကို နမူနာပုံစံနဲ့ ပြပါတယ်: dependency တွေကို အရင် build ဖြစ်အောင် module တွေကို စီတာနဲ့ dependency များက library တူတူကို တောင်းတဲ့အခါ အမြင့်ဆုံး version ကို ရွေးတာပါ။ Code က ရိုးရိုး Kotlin ဖြစ်ပြီး check() assertion တွေ ပါလို့ module စာရင်းကို ပြောင်းပြီး အစီအစဉ်နဲ့ cycle error က ဘယ်လို တုံ့ပြန်လဲ ကိုယ်တိုင် စမ်းကြည့်လို့ရပါတယ်။
အစစ်အမှန်က ပိုရှုပ်တယ်
Gradle က version တွေကို ဒီ model ထက် စည်းမျဉ်းပိုများများနဲ့ နှိုင်းယှဉ်ပါတယ် (ဥပမာ -rc ဒါမှမဟုတ် -SNAPSHOT လို qualifier တွေ) ပြီးတော့ version တစ်ခုကို တင်းကျပ်စွာ ရွေးခိုင်းလို့လည်း ရပါတယ်။ ဒီမှာသုံးတဲ့ ကိန်းဂဏန်း နှိုင်းယှဉ်မှုက အကြောင်းအရာကို ပြဖို့ လုံလောက်ပါတယ်: စာသား '2.9.9' က '2.10.0' ထက် နောက်ကျအောင် စီပေမဲ့ 2.10.0 က ပိုသစ်ပါတယ်။
data class Module(val name: String, val dependsOn: List<String> = emptyList())
fun buildOrder(modules: List<Module>): List<String> {
val byName = modules.associateBy { it.name }
val order = mutableListOf<String>()
val state = mutableMapOf<String, Int>() // 1 = being visited, 2 = finished
fun visit(name: String, path: List<String>) {
if (state[name] == 2) return
check(state[name] != 1) {
"circular dependency: " + (path + name).joinToString(" -> ")
}
val module = byName[name] ?: error("unknown module '$name'")
state[name] = 1
module.dependsOn.forEach { visit(it, path + name) }
state[name] = 2
order += name
}
modules.forEach { visit(it.name, emptyList()) }
return order
}
fun compareVersions(a: String, b: String): Int {
val x = a.split(".").map { it.toInt() }
val y = b.split(".").map { it.toInt() }
for (i in 0 until maxOf(x.size, y.size)) {
val diff = x.getOrElse(i) { 0 }.compareTo(y.getOrElse(i) { 0 })
if (diff != 0) return diff
}
return 0
}
fun resolve(requests: List<Pair<String, String>>): Map<String, String> =
requests.groupBy({ it.first }, { it.second }).mapValues { (_, versions) ->
versions.maxWith { a, b -> compareVersions(a, b) }
}
fun main() {
val modules = listOf(
Module("app", listOf("feature", "core")),
Module("feature", listOf("core")),
Module("core"),
)
val order = buildOrder(modules)
println("build order: " + order.joinToString(" -> "))
check(order.indexOf("core") < order.indexOf("feature"))
check(order.last() == "app")
val cyclic = listOf(Module("app", listOf("core")), Module("core", listOf("app")))
try {
buildOrder(cyclic)
} catch (e: IllegalStateException) {
println(e.message)
}
val requests = listOf(
"json" to "2.9.1",
"json" to "2.10.0",
"log" to "1.2.0",
"json" to "2.9.9",
)
val resolved = resolve(requests)
check(resolved["json"] == "2.10.0")
check("2.9.9" > "2.10.0") // plain string comparison gets this wrong
resolved.forEach { (module, version) -> println("$module -> $version") }
}Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Modules form a graph
data class Module(val name: String, val dependsOn: List<String> = emptyList())Module တစ်ခုချင်းစီက နာမည်နဲ့ သူ မှီခိုတဲ့ module တွေရဲ့ စာရင်းပါ။ Gradle မှာ implementation(project(":core")) လို့ ရေးတာနဲ့ အတူတူပါပဲ။
- Dependencies first
module.dependsOn.forEach { visit(it, path + name) }Module တစ်ခုကို order ထဲ မထည့်ခင် သူ့ dependency တွေကို အရင် ပြီးအောင် လုပ်ပါတယ်။ ဒါကြောင့် core က feature မတိုင်ခင်၊ feature က app မတိုင်ခင် ရောက်ပါတယ်။
- Cycles are errors
check(state[name] != 1) {လက်ရှိ လမ်းကြောင်းပေါ်မှာ ရှိနေတဲ့ (state 1) module ကို ပြန်တွေ့ရင် cycle ဖြစ်ပါတယ်။ app -> core -> app လို module တွေက တစ်ခုကိုတစ်ခု မှီခိုနေရင် ဘယ်အရင် build ရမလဲ မဖြေနိုင်လို့ build ကို ငြင်းပါတယ်။
- Newest version wins
versions.maxWith { a, b -> compareVersions(a, b) }Module တစ်ခုအတွက် တောင်းထားတဲ့ version အားလုံးကို စုပြီး အမြင့်ဆုံးကို ယူပါတယ်။ ဒါက Gradle ရဲ့ default conflict resolution နဲ့ တူပါတယ်။
- Numeric comparison
x.getOrElse(i) { 0 }.compareTo(y.getOrElse(i) { 0 })Version ကို '.' နဲ့ ခွဲပြီး အပိုင်းတစ်ခုချင်းစီကို ကိန်းဂဏန်းအဖြစ် နှိုင်းယှဉ်ပါတယ်။ အဲဒီအတွက် 10 က 9 ထက် ကြီးပြီး 2.10.0 က 2.9.9 ကို နိုင်ပါတယ်။ အပိုင်းမပါတဲ့နေရာကို 0 အဖြစ် သတ်မှတ်ပါတယ်။
- Assert the result
check(order.last() == "app")main က ရလဒ်ကို println နဲ့ ပြရုံမက check() နဲ့ အတည်ပြုပါတယ်။ app က နောက်ဆုံးမှ build ဖြစ်ရမယ်ဆိုတဲ့ စည်းမျဉ်း ချိုးဖောက်ရင် ချက်ချင်း fail ဖြစ်ပါလိမ့်မယ်။
build order: core -> feature -> app
circular dependency: app -> core -> app
json -> 2.10.0
log -> 1.2.0- settings.gradle.kts ကို ဖတ်ပြီး module တွေ ရှာ
- build script တွေကို evaluate လုပ်ပြီး task graph ဆောက်
- Repository တွေကနေ dependency တွေ resolve လုပ်
- main ကို compile၊ test ကို compile၊ test တွေ run
- ရလဒ်ကို report ထုတ်၊ UP-TO-DATE task တွေကို ကျော်
Module တစ်ခုတည်း build အပြည့်အစုံ
အောက်က build.gradle.kts က Kotlin/JVM application တစ်ခုအတွက် လိုအပ်တာအားလုံး ပါပါတယ်။ kotlin("jvm") က Kotlin Gradle plugin ကို apply လုပ်ပြီး version ကို ကိုယ့်ဘာသာ မခန့်မှန်ဘဲ official release page မှာ လက်ရှိ version ကို စစ်ပါ။ application plugin က run task ကို ထည့်ပေးပြီး mainClass က entry point ပါ (Main.kt ထဲက top-level main() က MainKt class ဖြစ်ပါတယ်)။ jvmToolchain(21) က compile နဲ့ test လုပ်ဖို့ JDK version ကို ပြောပြပြီး useJUnitPlatform() က test တွေကို JUnit Platform နဲ့ run စေပါတယ်။
plugins {
kotlin("jvm") version "2.1.21" // check the current Kotlin version
application
}
group = "com.example"
version = "1.0.0"
repositories {
mavenCentral()
}
dependencies {
// replace <version> with the current kotlinx.coroutines release
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:<version>")
testImplementation(kotlin("test"))
}
kotlin {
jvmToolchain(21)
}
application {
mainClass.set("com.example.MainKt")
}
tasks.test {
useJUnitPlatform()
}rootProject.name = "task-app"task-app/
settings.gradle.kts
build.gradle.kts
gradlew
gradlew.bat
gradle/wrapper/
gradle-wrapper.jar
gradle-wrapper.properties
src/
main/
kotlin/com/example/Main.kt -> compiled to class MainKt
resources/
test/
kotlin/com/example/MainTest.kt
build/ -> generated, never commitVersion catalog နဲ့ version တစ်နေရာတည်း
Library ငါးခု ဆယ်ခုရှိလာရင် version နံပါတ်တွေကို build script အနှံ့ ရေးထားတာက ပျက်လွယ်ပါတယ်။ gradle/libs.versions.toml ထဲမှာ version နဲ့ library တွေကို တစ်ကြိမ်ပဲ ကြေညာပြီး script တွေက libs.coroutines.core လို type-safe accessor နဲ့ ယူပါတယ်။ kotlinx-coroutines-core နဲ့ kotlinx-coroutines-test လို အတူသွားရမယ့် library တွေက version.ref တူတူကို သုံးလို့ တစ်ခုနဲ့တစ်ခု မကွဲပါဘူး။
[versions]
kotlin = "2.1.21"
coroutines = "<version>"
[libraries.coroutines-core]
module = "org.jetbrains.kotlinx:kotlinx-coroutines-core"
version.ref = "coroutines"
[libraries.coroutines-test]
module = "org.jetbrains.kotlinx:kotlinx-coroutines-test"
version.ref = "coroutines"
[plugins.kotlin-jvm]
id = "org.jetbrains.kotlin.jvm"
version.ref = "kotlin"plugins {
alias(libs.plugins.kotlin.jvm)
application
}
dependencies {
implementation(libs.coroutines.core)
testImplementation(libs.coroutines.test)
testImplementation(kotlin("test"))
}Module အများအပြား
Project ကြီးလာရင် :core, :data, :app လို module တွေအဖြစ် ခွဲပါတယ်။ settings.gradle.kts က include နဲ့ module တွေကို စာရင်းပေးပြီး module တစ်ခုက တခြား module ကို implementation(project(":core")) နဲ့ မှီခိုပါတယ်။ ဒီမျှားတွေက အပေါ်က worked example ထဲက Module.dependsOn ပါပဲ: :app က :core ကို မှီခိုရင် core ရဲ့ task တွေ အရင် run ပြီး :core ကိုပဲ ပြင်ရင် :core နဲ့ သူ့ကို မှီခိုတဲ့ module တွေပဲ ပြန် compile ရပါတယ်။ Module တွေကို သင့်လျော်စွာ ခွဲရင် build ပိုမြန်လာပါတယ်။
// settings.gradle.kts
rootProject.name = "task-app"
include(":core", ":app")
// build.gradle.kts (root): declare the plugin once, apply it per module
plugins {
kotlin("jvm") version "2.1.21" apply false
}
// app/build.gradle.kts
plugins {
kotlin("jvm")
application
}
dependencies {
implementation(project(":core"))
}နေ့စဉ်သုံး command တွေ
ဒီ command တွေက project root ကနေ run ပါတယ်။ Module တစ်ခုတည်းရဲ့ task ကို :core:test လို prefix နဲ့ ခေါ်လို့ရပြီး dependencyInsight က library တစ်ခုရဲ့ version ကို ဘာကြောင့် အဲဒီလို ရွေးလဲ ပြပေးလို့ conflict ကို စုံစမ်းဖို့ အသုံးဝင်ပါတယ်။
./gradlew build # compile, test, assemble
./gradlew test # only the tests
./gradlew :core:test # the tests of one module
./gradlew run # start the application
./gradlew dependencies # the resolved dependency tree
# why did this library get this version?
./gradlew dependencyInsight --dependency coroutines --configuration runtimeClasspath
./gradlew clean build # delete build/ first, then rebuild everythingလက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ Gradle ကို ဘယ်မှာ တွေ့ရလဲ
Kotlin project အချိန်ရှိသမျှမှာ Gradle ကို တစ်နေရာမဟုတ်တစ်နေရာမှာ ဆုံရပါလိမ့်မယ်။ Project အသစ်တိုင်းကို ကိုယ့်ဘာသာ စရေးတာ မဟုတ်ဘဲ generator တစ်ခုကနေ စပြီး build ဖိုင်တွေကို နားလည်ထားတာက ပြဿနာဖြေရှင်းတဲ့အခါ အကူအညီ အကြီးကြီး ဖြစ်ပါတယ်။
- Android: Android Studio project တွေက Gradle ကို Android Gradle Plugin နဲ့ တွဲသုံးပြီး module တစ်ခုစီမှာ build.gradle.kts (ဒါမှမဟုတ် build.gradle) ရှိပါတယ်၊ library ထပ်ထည့်တာက dependencies block ထဲ line တစ်ကြောင်း ထည့်တာပါပဲ။
- Server framework တွေ: Spring Boot နဲ့ Ktor project generator တွေက Kotlin နဲ့ Gradle ကို ရွေးလို့ရတဲ့ build ဖိုင်တွေ ထုတ်ပေးပါတယ်၊ ထုတ်ပေးတဲ့ဖိုင်တွေက ဒီ lesson က ပုံစံတွေနဲ့ အတူတူပါ။
- CI: pull request တိုင်းမှာ CI server က ./gradlew build ကို run ပြီး test fail ရင် merge မလုပ်စေပါဘူး၊ wrapper ကြောင့် CI မှာ Gradle သီးခြား install လုပ်စရာ မလိုပါဘူး။
- Multi-module project: :core, :data, :feature လို module တွေခွဲထားရင် ပြောင်းလဲတဲ့ module နဲ့ သူ့ကို မှီခိုတဲ့ module တွေကိုပဲ ပြန် compile လုပ်ပြီး ကျန်တာတွေကို build cache က ကျော်ပေးပါတယ်။
အမှားများ
Gradle နဲ့ ဖြစ်လေ့ရှိတဲ့ အမှားများ
Test library ကို implementation နဲ့ ကြေညာခြင်း
implementation(kotlin("test")) လို့ ရေးရင် test library က production classpath ထဲပါ ဝင်သွားပြီး ပို့မယ့် application ထဲ ပါသွားနိုင်ပါတယ်။ Test အတွက်ပဲ ဆိုရင် testImplementation ကို သုံးပါ၊ ဒါမှ main source set က မြင်ရမှာ မဟုတ်ပါဘူး။
Related library တွေကို version မတူအောင် ရေးခြင်း
kotlinx-coroutines-core ကို 1.9 နဲ့ kotlinx-coroutines-test ကို 1.10 လို့ တမင်မဟုတ်ဘဲ ရေးမိရင် ထူးဆန်းတဲ့ runtime error တွေ ဖြစ်နိုင်ပါတယ်။ Version ကို version catalog ထဲ တစ်နေရာတည်းမှာ သိမ်းပြီး တွဲသုံးမယ့် library တွေက version.ref တူတူကို ကိုးကားပါ။
Wrapper ဖိုင်တွေကို commit မလုပ်ဘဲ build/ ကို commit လုပ်ခြင်း
gradlew, gradlew.bat နဲ့ gradle/wrapper/ ကို repository ထဲ မထည့်ရင် teammate တစ်ယောက်စီက Gradle version မတူတာ သုံးမိပြီး 'ငါ့စက်မှာတော့ ရတယ်' ပြဿနာ ပြန်ဖြစ်ပါတယ်။ ပြောင်းပြန်အနေနဲ့ build/ နဲ့ .gradle/ ကို commit လုပ်ရင် generated ဖိုင်တွေကြောင့် diff တွေ ရှုပ်ပြီး conflict တွေ ဖြစ်ပါတယ်၊ ဒါတွေကို .gitignore ထဲ ထည့်ပါ။
Module တွေကို တစ်ခုကိုတစ်ခု မှီခိုစေခြင်း
ဒီဇိုင်းအရ :core က :app ကို မှီခိုနေပြီး :app ကလည်း :core ကို မှီခိုရင် ဘယ်တစ်ခုက အရင် build ရမလဲ မဖြေနိုင်တော့ပါဘူး၊ Gradle က circular dependency error နဲ့ ရပ်ပါတယ်။ ဖြေရှင်းနည်းက မျှဝေတဲ့ အပိုင်းကို module တတိယတစ်ခု ထုတ်လိုက်တာပါ၊ worked example ထဲက check() က ဒီပုံစံကို ဖမ်းတာ ဒါပါပဲ။
Best Practices
Best practices
- ./gradlew ကိုပဲ သုံးပါ၊ စက်ထဲ install လုပ်ထားတဲ့ gradle ကို မသုံးပါနဲ့ — ဒါမှ project နဲ့ CI မှာ Gradle version တူပါတယ်။
- Library version တွေကို version catalog မှာ ထားပြီး build script တွေက အဲဒီ catalog ကိုပဲ ကိုးကားပါ။
- Dependency ကို လိုအပ်တဲ့ အနည်းဆုံး scope နဲ့ ကြေညာပါ: test အတွက် testImplementation၊ အများစုအတွက် implementation။
- Version conflict ကို သံသယရှိရင် ./gradlew dependencies ဒါမှမဟုတ် dependencyInsight နဲ့ စစ်ပြီး ခန့်မှန်းတာကို ရှောင်ပါ။
Mini challenge
Mini challenge: module တစ်ခုပြောင်းရင် ဘာတွေ ပြန် build ရမလဲ
Module graph တစ်ခုနဲ့ ပြောင်းလဲသွားတဲ့ module တစ်ခုကို ပေးထားရင် ပြန် build ရမယ့် module အားလုံး (ပြောင်းလဲတဲ့ module ကိုယ်တိုင်နဲ့ သူ့ကို တိုက်ရိုက်ဖြစ်စေ သွယ်ဝိုက်ဖြစ်စေ မှီခိုတဲ့ module အားလုံး) ကို ပြန်ပေးတဲ့ affectedBy ကို ရေးပါ။ Gradle ရဲ့ incremental build က ဒီအကြောင်းအရာကို အခြေခံပါတယ်။
- affectedBy(changed, modules) က Set<String> ကို ပြန်ပေးပြီး changed ကိုယ်တိုင် ပါရမယ်
- သွယ်ဝိုက်မှီခိုတဲ့ module တွေ (app -> feature -> core) ကိုပါ ထည့်ရမယ်
- မဆိုင်တဲ့ module (ဥပမာ tools) ကို မထည့်ရဘူး
- main ထဲမှာ core, feature, tools ပြောင်းတဲ့ အခြေအနေတွေကို print ထုတ်ပြီး check() နဲ့ app နဲ့ tools အတွက် အတည်ပြုပါ
data class Module(val name: String, val dependsOn: List<String> = emptyList())
// TODO: start from the changed module, then keep adding every module that
// depends on something already in the set, until nothing new appears
fun affectedBy(changed: String, modules: List<Module>): Set<String> {
TODO()
}
fun main() {
val modules = listOf(
Module("app", listOf("feature", "data")),
Module("feature", listOf("core")),
Module("data", listOf("core")),
Module("core"),
Module("tools"),
)
// TODO: print affectedBy for "core", "feature" and "tools" (sorted)
// TODO: check() that changing "app" affects only app, and that "tools" is
// not affected by a change in "core"
}ရလဒ်: core ပြောင်းရင် [app, core, data, feature]၊ feature ပြောင်းရင် [app, feature]၊ tools ပြောင်းရင် [tools] ကို print ထုတ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
Set တစ်ခုကို changed နဲ့ စပါ၊ ပြီးတော့ while loop ထဲမှာ module တိုင်းကို ကြည့်ပြီး dependsOn ထဲက တစ်ခုခုက set ထဲရှိနေပြီး ကိုယ်တိုင်က မရှိသေးရင် ထည့်ပါ။ Set မကြီးထွားတော့တဲ့အထိ ထပ်လုပ်ရင် သွယ်ဝိုက်မှီခိုတာတွေပါ ပါလာပါမယ်။
အဖြေ ကြည့်ရန်
data class Module(val name: String, val dependsOn: List<String> = emptyList())
fun affectedBy(changed: String, modules: List<Module>): Set<String> {
val result = mutableSetOf(changed)
var grew = true
while (grew) {
grew = false
for (module in modules) {
if (module.name !in result && module.dependsOn.any { it in result }) {
result += module.name
grew = true
}
}
}
return result
}
fun main() {
val modules = listOf(
Module("app", listOf("feature", "data")),
Module("feature", listOf("core")),
Module("data", listOf("core")),
Module("core"),
Module("tools"),
)
println("core changed -> " + affectedBy("core", modules).sorted())
println("feature changed -> " + affectedBy("feature", modules).sorted())
println("tools changed -> " + affectedBy("tools", modules).sorted())
check(affectedBy("app", modules) == setOf("app"))
check("tools" !in affectedBy("core", modules))
}Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- settings.gradle.kts က module တွေကို ရွေးပြီး build.gradle.kts က plugin, repository, dependency တွေကို ကြေညာပါတယ်။
- Source က src/main/kotlin နဲ့ src/test/kotlin မှာရှိပြီး build/ ကတော့ ပြန်ထုတ်လို့ရတဲ့ output ပါ၊ commit မလုပ်ရပါဘူး။
- implementation, testImplementation, runtimeOnly နဲ့ compileOnly တို့က dependency ကို ဘယ်နေရာမှာ မြင်ရမလဲ ဆုံးဖြတ်ပြီး version conflict မှာ Gradle က အမြင့်ဆုံးကို ရွေးပါတယ်။
- Gradle က task graph ကို run ပြီး မပြောင်းတဲ့ task တွေကို UP-TO-DATE အဖြစ် ကျော်ပါတယ်၊ ./gradlew wrapper ကို သုံးပါ။
Kotlin releases — Kotlin