0Pricing
Kotlin Multiplatform Academy · บทเรียน

การอ่าน build.gradle.kts ของ shared

ถอดความบล็อก kotlin{} และการประกาศเป้าหมาย

การอ่าน build.gradle.kts ของ shared เป็นบทเรียน Kotlin Multiplatform Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Kotlin Multiplatform Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

The Module's Recipe

The shared build.gradle.kts is the recipe for your module: which targets to build and which libraries to pull in.

Plugins Come First

At the top, the plugins block turns on multiplatform support. Without it, Kotlin has no idea about targets.

plugins {
    kotlin("multiplatform")
    id("com.android.library")
}

The kotlin {} Block

Everything multiplatform lives inside the kotlin block. This is where targets and source sets are declared.

kotlin {
    // targets and source sets go here
}

Declaring Android

The androidTarget line tells Kotlin to produce an Android library artifact from your shared code.

kotlin {
    androidTarget()
}

Declaring iOS

iOS needs a few targets, one per architecture, so it runs on simulators and real devices alike.

iosX64()
iosArm64()
iosSimulatorArm64()

Naming the Framework

A binaries.framework entry names the iOS framework Swift will import. Pick a clean name your iOS team will type often.

iosArm64 {
    binaries.framework {
        baseName = "Shared"
    }
}

Source Set Dependencies

The sourceSets block lists libraries per set. commonMain gets cross-platform deps; platform sets get their own.

sourceSets {
    commonMain.dependencies {
        // shared libraries
    }
}

Common Dependencies Are Shared

A library added to commonMain must support every target. Add it here and all platforms can use it.

commonMain.dependencies {
    implementation("io.ktor:ktor-client-core:2.3.0")
}

Platform Dependencies

Platform-only libraries go in their own set, like an Android engine that only the JVM target understands.

androidMain.dependencies {
    implementation("io.ktor:ktor-client-okhttp:2.3.0")
}

Android Config Block

A separate android block sets the namespace and SDK levels for the Android library half of the module.

android {
    namespace = "com.example.shared"
    compileSdk = 34
}

Read It Top to Bottom

To grok any shared build file, read it as a story: plugins, then targets, then source sets, then platform config. 📖

Quick Check

Where does a cross-platform library belong?

Recap

The shared build.gradle.kts flows plugins to targets to source sets. Common deps for all, platform deps for one.

คำถามที่พบบ่อย

บทเรียน “การอ่าน build.gradle.kts ของ shared” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การอ่าน build.gradle.kts ของ shared” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Kotlin Multiplatform Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Kotlin Multiplatform Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การอ่าน build.gradle.kts ของ shared”

ถอดความบล็อก kotlin{} และการประกาศเป้าหมาย คุณปฏิบัติ Kotlin Multiplatform Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Kotlin Multiplatform Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Kotlin Multiplatform Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การอ่าน build.gradle.kts ของ shared” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Kotlin Multiplatform Academy นี้ได้ไหม

ได้ บทเรียน Kotlin Multiplatform Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. โมดูล shared เทียบกับโมดูลแอป
  2. commonMain, androidMain และ iosMain
  3. การอ่าน build.gradle.kts ของ shared
  4. การจัดวาง Test: commonTest และส่วนที่เกี่ยวข้อง
← กลับไปที่ Kotlin Multiplatform Academy