0Pricing
Kotlin Multiplatform Academy · 课时

阅读共享的 build.gradle.kts

解析 kotlin{} 代码块和目标声明

阅读共享的 build.gradle.kts 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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」课时是免费的吗?

是的 — 「阅读共享的 build.gradle.kts」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。

「阅读共享的 build.gradle.kts」这节课中我会学到什么?

解析 kotlin{} 代码块和目标声明 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Kotlin Multiplatform Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。

「阅读共享的 build.gradle.kts」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?

能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 共享模块与应用模块
  2. commonMain、androidMain 与 iosMain
  3. 阅读共享的 build.gradle.kts
  4. 测试位于何处:commonTest 及相关源集
← 返回 Kotlin Multiplatform Academy