Skip to content

KMPプラットフォーム統合 ​

このページでは、Koinを使用したKotlin Multiplatformアプリにおけるプラットフォーム固有の詳細を扱います。具体的には、共有コードからのAndroid Contextの利用、SwiftからのKoinの呼び出し、デスクトップおよびWebターゲットについて説明します。プロジェクトがKMP SetupおよびKMP Sharing Patternsに従っていることを前提としています。

完全なプロジェクト例:Compose Multiplatformサンプル(Compiler Plugin DSL)およびアノテーションサンプル。

共有コードでのAndroid Context ​

共有コード(shared code)ではandroid.content.Contextを直接使用できませんが、一部の共有クラスはAndroid上でそれを必要とします(ファイルを開いたり、設定を読み込んだりするため)。共通コード(common code)が依存できるプラットフォーム型にラップしてください:

kotlin
// commonMain
interface ContextWrapper {
    fun getContextInfo(): String
}

class FileStore(private val ctx: ContextWrapper)

val storageModule = module {
    single<FileStore>()
}
kotlin
// androidMain
class AndroidContextWrapper(val context: Context) : ContextWrapper {
    override fun getContextInfo() = "Android: ${context.packageName}"
}

actual fun platformModule(): Module = module {
    single<AndroidContextWrapper>() bind ContextWrapper::class
}
kotlin
// iosMain
class IOSContextWrapper : ContextWrapper {
    override fun getContextInfo() = "iOS"
}

actual fun platformModule(): Module = module {
    single<IOSContextWrapper>() bind ContextWrapper::class
}
kotlin
// commonMain
interface ContextWrapper {
    fun getContextInfo(): String
}

@Singleton
class FileStore(private val ctx: ContextWrapper)
kotlin
// androidMain
@Singleton
class AndroidContextWrapper(val context: Context) : ContextWrapper {
    override fun getContextInfo() = "Android: ${context.packageName}"
}
kotlin
// iosMain
@Singleton
class IOSContextWrapper : ContextWrapper {
    override fun getContextInfo() = "iOS"
}

プラットフォームクラスは、@ComponentScanを使用したactualプラットフォームモジュールによって検出されます(KMP Sharing Patternsを参照)。

kotlin
// commonMain
interface ContextWrapper {
    fun getContextInfo(): String
}

class FileStore(private val ctx: ContextWrapper)

val storageModule = module {
    singleOf(::FileStore)
}
kotlin
// androidMain
actual fun platformModule(): Module = module {
    singleOf(::AndroidContextWrapper) bind ContextWrapper::class
}

// iosMain
actual fun platformModule(): Module = module {
    singleOf(::IOSContextWrapper) bind ContextWrapper::class
}

FileStoreはcommonMainで一度だけコンパイルされ、プラットフォームラッパーを受け取ります。Contextに触れるのはAndroid側の定義1つだけであり、起動時にandroidContext()が設定されると解決されます。共通コード内でプラットフォーム型をチェックするのではなく、Android固有の操作をContextWrapperのメソッドの裏側に隠蔽するようにしてください。

iOSとSwift ​

Swiftからの依存関係の取得 ​

Swiftはget<T>()のようなKoinのreified関数を呼び出すことができません。Swiftとの境界では、iosMainにKoinComponentを実装した小さなKotlinヘルパーを作成し、それをSwiftから使用します。これは相互運用のエントリーポイントとして機能し、共有クラスは引き続きコンストラクタ経由で依存関係を受け取ることができます。

kotlin
// iosMain
class GreetingHelper : KoinComponent {
    private val greeting: Greeting by inject()
    fun greet(): String = greeting.greeting()
}
swift
struct ContentView: View {
    let greet = GreetingHelper().greet()

    var body: some View {
        Text(greet)
    }
}

ヘルパーを使用する前にKoinを開始しておく必要があります。KMP SetupのiOSエントリーポイントを参照してください。KoinComponentについての詳細はKoinComponentで解説されています。

KoinでSwiftのインスタンスを宣言する ​

SwiftクラスがKotlinのインターフェースを実装しており、Kotlinコード側がそれをKoinから取得したい場合があります。そのような場合は、Koin.declare()を使用して実行時にインスタンスを登録します。Swiftは型をObjective-Cのクラスまたはプロトコルとして渡し、このiosMainヘルパーがそれをKotlinのKClassに変換します:

kotlin
// iosMain
@OptIn(BetaInteropApi::class)
fun Koin.declareFromSwift(
    instance: Any,
    bindTo: ObjCObject,
    qualifier: Qualifier? = null,
    allowOverride: Boolean = true
) {
    val kClass: KClass<*> = when (bindTo) {
        is ObjCClass -> getOriginalKotlinClass(bindTo)
        is ObjCProtocol -> getOriginalKotlinClass(bindTo)
        else -> null
    } ?: error("Can't resolve Kotlin KClass from $bindTo")

    declare(
        instance = instance,
        qualifier = qualifier,
        secondaryTypes = listOf(kClass),
        allowOverride = allowOverride
    )
}
swift
koin.declareFromSwift(
    instance: SwiftAnalytics(),
    bindTo: Analytics.self,
    qualifier: nil,
    allowOverride: true
)

getOriginalKotlinClass()はkotlin.nativeから提供されており、クラス(クラスに対する.self)またはプロトコル(プロトコルに対する.self)を元のKotlin型へとマッピングします。宣言されたインスタンスはコンパイル時にはチェックされません。Swiftがインスタンスをまだ宣言していない場合、Kotlin側のコンシューマー(呼び出し元)は実行時に失敗します。このヘルパーは@SarahDelCastilloによる提案に基づいています。

スレッド ​

Koinは現在のKotlin/Nativeメモリモデル(Kotlin 1.7.20以降のデフォルト)で動作します。フリーズや@SharedImmutableアノテーションを必要とせず、任意のスレッドから依存関係を解決したりスコープを使用したりできます。

デスクトップ (JVM) ​

最初のウィンドウを表示する前に、main()でKoinを開始します:

kotlin
// jvmMain
fun main() = application {
    initKoin {
        printLogger()
        modules(desktopModule)
    }

    Window(onCloseRequest = ::exitApplication) {
        App()
    }
}

val desktopModule = module {
    single<DesktopFileManager>()
}

共通のinitKoinのconfigパラメータにより、デスクトップ専用のモジュールとロガーを追加します。DesktopはJVMターゲットであるため、fileProperties()のようなJVM専用の機能が利用可能です(Starting Koinを参照)。

Web (JSおよびWasm) ​

jsまたはwasmJsソースセットのmain()でKoinを開始します:

kotlin
// jsMain or wasmJsMain
fun main() {
    initKoin {
        modules(webModule)
    }
    // UIを開始
}

val webModule = module {
    single<BrowserStorage>()
}

Web向けのCompose Multiplatformでは、代わりにルートのComposableからKoinApplicationを使用してKoinを開始することもできます(Koin for Composeを参照)。

  • KMP Setup: 依存関係、initKoin、エントリーポイント。
  • KMP Sharing Patterns: プラットフォームモジュールとexpect/actualの定義。
  • KoinComponent: Koinが作成しないクラスからの依存関係の取得。
  • Koin for Compose: Compose Multiplatformの統合。
  • Testing: commonTestでの共有モジュールのテスト。