diff --git a/androidApp/build.gradle.kts b/androidApp/build.gradle.kts index 849499f..2c7f3a0 100644 --- a/androidApp/build.gradle.kts +++ b/androidApp/build.gradle.kts @@ -1,6 +1,7 @@ plugins { id("com.android.application") kotlin("android") + id("com.google.devtools.ksp") } android { @@ -17,6 +18,15 @@ android { isMinifyEnabled = false } } + + applicationVariants.all { + val variantName = name + sourceSets { + getByName("main") { + java.srcDir(File("build/generated/ksp/$variantName/kotlin")) + } + } + } } dependencies { @@ -28,5 +38,7 @@ dependencies { with(Deps.Koin) { implementation(core) implementation(android) + implementation(annotations) + ksp(kspCompiler) } } \ No newline at end of file diff --git a/androidApp/src/main/java/com/example/helloworldkmp/android/GreetPresenter.kt b/androidApp/src/main/java/com/example/helloworldkmp/android/GreetPresenter.kt index ac95e91..888e5a7 100644 --- a/androidApp/src/main/java/com/example/helloworldkmp/android/GreetPresenter.kt +++ b/androidApp/src/main/java/com/example/helloworldkmp/android/GreetPresenter.kt @@ -1,7 +1,9 @@ package com.example.helloworldkmp.android import com.example.helloworldkmp.Greeting +import org.koin.core.annotation.Factory +@Factory class GreetPresenter(val greeting: Greeting) { fun print() = greeting.greeting() } \ No newline at end of file diff --git a/androidApp/src/main/java/com/example/helloworldkmp/android/MainApplication.kt b/androidApp/src/main/java/com/example/helloworldkmp/android/MainApplication.kt index 726940b..a883df3 100644 --- a/androidApp/src/main/java/com/example/helloworldkmp/android/MainApplication.kt +++ b/androidApp/src/main/java/com/example/helloworldkmp/android/MainApplication.kt @@ -1,13 +1,12 @@ package com.example.helloworldkmp.android import android.app.Application -import com.example.helloworldkmp.android.di.androidModule +import com.example.helloworldkmp.android.di.AndroidModule import com.example.helloworldkmp.di.appModule -import com.example.helloworldkmp.di.commonModule -import com.example.helloworldkmp.di.platformModule import org.koin.android.ext.koin.androidContext import org.koin.android.ext.koin.androidLogger import org.koin.core.context.startKoin +import org.koin.ksp.generated.* class MainApplication : Application() { override fun onCreate() { @@ -16,7 +15,7 @@ class MainApplication : Application() { startKoin { androidContext(this@MainApplication) androidLogger() - modules(appModule() + androidModule) + modules(appModule() + AndroidModule().module) } } } \ No newline at end of file diff --git a/androidApp/src/main/java/com/example/helloworldkmp/android/di/AndroidModule.kt b/androidApp/src/main/java/com/example/helloworldkmp/android/di/AndroidModule.kt index e3184be..ff95a28 100644 --- a/androidApp/src/main/java/com/example/helloworldkmp/android/di/AndroidModule.kt +++ b/androidApp/src/main/java/com/example/helloworldkmp/android/di/AndroidModule.kt @@ -1,10 +1,8 @@ package com.example.helloworldkmp.android.di -import com.example.helloworldkmp.android.GreetPresenter -import org.koin.core.module.dsl.singleOf -import org.koin.dsl.module +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module - -val androidModule = module { - singleOf(::GreetPresenter) -} \ No newline at end of file +@Module +@ComponentScan("com.example.helloworldkmp.android") +class AndroidModule \ No newline at end of file diff --git a/build.gradle.kts b/build.gradle.kts index 36ad92b..96017e7 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,10 +3,11 @@ buildscript { gradlePluginPortal() google() mavenCentral() + mavenLocal() } dependencies { classpath("org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.21") - classpath("com.android.tools.build:gradle:7.1.1") + classpath("com.android.tools.build:gradle:7.1.3") } } @@ -14,6 +15,7 @@ allprojects { repositories { google() mavenCentral() + mavenLocal() } } diff --git a/buildSrc/src/main/java/Dependencies.kt b/buildSrc/src/main/java/Dependencies.kt index 5caab44..d5ce268 100644 --- a/buildSrc/src/main/java/Dependencies.kt +++ b/buildSrc/src/main/java/Dependencies.kt @@ -1,5 +1,7 @@ + object Versions { const val koin = "3.2.0" + const val koinAnnotaions = "1.0.0-beta-3" } object Deps { @@ -8,6 +10,7 @@ object Deps { const val core = "io.insert-koin:koin-core:${Versions.koin}" const val test = "io.insert-koin:koin-test:${Versions.koin}" const val android = "io.insert-koin:koin-android:${Versions.koin}" + const val annotations = "io.insert-koin:koin-annotations:${Versions.koinAnnotaions}" + const val kspCompiler = "io.insert-koin:koin-ksp-compiler:${Versions.koinAnnotaions}" } - } diff --git a/gradle.properties b/gradle.properties index 5212a50..a53754a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,4 +14,9 @@ kotlin.mpp.enableCInteropCommonization=true # Native kotlin.native.binary.memoryModel=experimental -kotlin.native.binary.freezing=disabled \ No newline at end of file +kotlin.native.binary.freezing=disabled + +kotlinVersion=1.6.21 +koinVersion=3.2.0 +koinKspVersion=1.0.0-beta-3 +kspVersion=1.6.21-1.0.5 \ No newline at end of file diff --git a/iosApp/iosApp/iOSApp.swift b/iosApp/iosApp/iOSApp.swift index 68e4a65..10f2f41 100644 --- a/iosApp/iosApp/iOSApp.swift +++ b/iosApp/iosApp/iOSApp.swift @@ -5,7 +5,7 @@ import shared struct iOSApp: App { init() { - HelperKt.doInitKoin() + GreetingHelperKt.doInitKoin() } var body: some Scene { diff --git a/settings.gradle.kts b/settings.gradle.kts index cf0e5c5..0a251bb 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -1,4 +1,12 @@ pluginManagement { + + val kotlinVersion: String by settings + val kspVersion: String by settings + + plugins { + id("com.google.devtools.ksp") version kspVersion apply false + } + repositories { google() gradlePluginPortal() diff --git a/shared/build.gradle.kts b/shared/build.gradle.kts index 01e52ef..e582761 100644 --- a/shared/build.gradle.kts +++ b/shared/build.gradle.kts @@ -1,5 +1,6 @@ plugins { kotlin("multiplatform") + id("com.google.devtools.ksp") kotlin("native.cocoapods") id("com.android.library") } @@ -28,15 +29,19 @@ kotlin { with(Deps.Koin) { api(core) api(test) + api(annotations) } } + kotlin.srcDir("build/generated/ksp/android/androidDebug/kotlin") } val commonTest by getting { dependencies { implementation(kotlin("test")) } } - val androidMain by getting + val androidMain by getting { + dependsOn(commonMain) + } val androidTest by getting val iosX64Main by getting val iosArm64Main by getting @@ -66,4 +71,11 @@ android { minSdk = 21 targetSdk = 32 } +} + +dependencies { + add("kspCommonMainMetadata",Deps.Koin.kspCompiler) + add("kspAndroid",Deps.Koin.kspCompiler) + add("kspIosX64",Deps.Koin.kspCompiler) + add("kspIosSimulatorArm64",Deps.Koin.kspCompiler) } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/example/helloworldkmp/Greeting.kt b/shared/src/commonMain/kotlin/com/example/helloworldkmp/Greeting.kt index 44985c5..8294ef3 100644 --- a/shared/src/commonMain/kotlin/com/example/helloworldkmp/Greeting.kt +++ b/shared/src/commonMain/kotlin/com/example/helloworldkmp/Greeting.kt @@ -1,5 +1,8 @@ package com.example.helloworldkmp +import org.koin.core.annotation.Single + +@Single class Greeting(private val platform: Platform) { fun greeting(): String { diff --git a/shared/src/commonMain/kotlin/com/example/helloworldkmp/Platform.kt b/shared/src/commonMain/kotlin/com/example/helloworldkmp/Platform.kt index 8f39345..441c1df 100644 --- a/shared/src/commonMain/kotlin/com/example/helloworldkmp/Platform.kt +++ b/shared/src/commonMain/kotlin/com/example/helloworldkmp/Platform.kt @@ -1,5 +1,8 @@ package com.example.helloworldkmp +import org.koin.core.annotation.Single + +@Single expect class Platform() { val name: String } \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/AppModule.kt b/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/AppModule.kt index 33a50a8..8d97e3d 100644 --- a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/AppModule.kt +++ b/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/AppModule.kt @@ -1,3 +1,11 @@ package com.example.helloworldkmp.di -fun appModule() = listOf(commonModule, platformModule) \ No newline at end of file +import org.koin.core.annotation.ComponentScan +import org.koin.core.annotation.Module +import org.koin.ksp.generated.module + +fun appModule() = listOf(AppModule().module) + +@Module +@ComponentScan("com.example.helloworldkmp") +class AppModule \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/CommonModule.kt b/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/CommonModule.kt deleted file mode 100644 index f40768c..0000000 --- a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/CommonModule.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.helloworldkmp.di - -import com.example.helloworldkmp.Greeting -import org.koin.core.module.dsl.singleOf -import org.koin.dsl.module - -val commonModule = module { - singleOf(::Greeting) -} \ No newline at end of file diff --git a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/PlatformModule.kt b/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/PlatformModule.kt deleted file mode 100644 index 254e780..0000000 --- a/shared/src/commonMain/kotlin/com/example/helloworldkmp/di/PlatformModule.kt +++ /dev/null @@ -1,9 +0,0 @@ -package com.example.helloworldkmp.di - -import com.example.helloworldkmp.Platform -import org.koin.core.module.dsl.singleOf -import org.koin.dsl.module - -val platformModule = module { - singleOf(::Platform) -} \ No newline at end of file diff --git a/shared/src/iosMain/kotlin/com/example/helloworldkmp/Helper.kt b/shared/src/iosMain/kotlin/com/example/helloworldkmp/GreetingHelper.kt similarity index 89% rename from shared/src/iosMain/kotlin/com/example/helloworldkmp/Helper.kt rename to shared/src/iosMain/kotlin/com/example/helloworldkmp/GreetingHelper.kt index d3cc744..d73de05 100644 --- a/shared/src/iosMain/kotlin/com/example/helloworldkmp/Helper.kt +++ b/shared/src/iosMain/kotlin/com/example/helloworldkmp/GreetingHelper.kt @@ -1,7 +1,6 @@ package com.example.helloworldkmp import com.example.helloworldkmp.di.appModule -import com.example.helloworldkmp.di.commonModule import org.koin.core.component.KoinComponent import org.koin.core.component.inject import org.koin.core.context.startKoin