注入平台实现
通过 Koin 或工厂提供正确的 actual 实现
注入平台实现 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Who Supplies the Real Thing
An interface defines a capability, but something must hand shared code the real platform implementation. That wiring is dependency injection.
Avoid new in Shared Code
Shared logic should not construct platform classes itself. Instead it receives the implementation from outside, keeping it decoupled and testable.
Inject via Constructor
The simplest approach is a constructor parameter. Pass the capability in, and the class never knows which platform built it.
class Profile(private val info: DeviceInfo)Provide the Android Instance
On Android, the app module creates the real actual and passes it down when it builds the shared object.
val profile = Profile(DeviceInfo())Provide the iOS Instance
On iOS, Swift constructs the framework's DeviceInfo and injects it the same way, so shared code stays identical across platforms.
let profile = Profile(info: DeviceInfo())Scale Up With Koin
For many dependencies, a DI library like Koin declares each capability once in a shared module and resolves it on demand.
val appModule = module {
single<DeviceInfo> { DeviceInfo() }
}Bind Interface to Actual
In Koin you bind the interface type to the platform implementation, so callers ask for DeviceInfo and get the right concrete one.
single<Storage> { PlatformStorage() }Resolve Where You Need It
Anywhere in shared code you can pull a dependency from the graph by its type, no manual construction required.
val info: DeviceInfo by inject()Swap Fakes in Tests
Because everything is injected, a test simply provides a fake implementation. No platform, emulator, or device is involved. ✅
Profile(FakeDeviceInfo())One Wiring Point per App
Keep injection at each app's entry point. That single setup spot decides which actuals run, while shared code stays blissfully unaware.
Factories for Cheap Cases
If a DI library feels heavy, a plain factory function works too. It builds the platform object in one place and hands it to shared code.
fun deviceInfo(): DeviceInfo = DeviceInfo()Quick Check
Think about why shared code receives its dependencies.
Recap
You learned to inject platform implementations via the constructor or Koin, binding interfaces to actuals so shared code stays clean and testable. 🎉
常见问题解答
「注入平台实现」课时是免费的吗?
是的 — 「注入平台实现」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「注入平台实现」这节课中我会学到什么?
通过 Koin 或工厂提供正确的 actual 实现 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「注入平台实现」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。