プラットフォーム実装を注入する
Koinまたはファクトリで適切なactualを提供します
「プラットフォーム実装を注入する」はCoddyKit上の無料Kotlin Multiplatform Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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. 🎉
よくある質問
「プラットフォーム実装を注入する」レッスンは無料ですか?
はい。「プラットフォーム実装を注入する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Kotlin Multiplatform Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Kotlin Multiplatform Academyコースには全4レッスンが含まれています。
「プラットフォーム実装を注入する」で何を学びますか?
Koinまたはファクトリで適切なactualを提供します ブラウザで直接実行するハンズオンコードでKotlin Multiplatform Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
Kotlin Multiplatform Academyを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのKotlin Multiplatform Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「プラットフォーム実装を注入する」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このKotlin Multiplatform Academyレッスンでコードを書いて実行できますか?
はい。すべてのKotlin Multiplatform Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。
このコースのすべてのレッスン
- 機能インターフェースを設計する
- デバイス情報のexpect/actual
- プラットフォームごとのファイルシステムとパス
- プラットフォーム実装を注入する