Skip to content

Commit

Permalink
KMP with Koin Annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaudgiuliani committed Jun 7, 2022
1 parent a091fde commit 6644448
Show file tree
Hide file tree
Showing 16 changed files with 72 additions and 36 deletions.
12 changes: 12 additions & 0 deletions androidApp/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
id("com.android.application")
kotlin("android")
id("com.google.devtools.ksp")
}

android {
Expand All @@ -17,6 +18,15 @@ android {
isMinifyEnabled = false
}
}

applicationVariants.all {
val variantName = name
sourceSets {
getByName("main") {
java.srcDir(File("build/generated/ksp/$variantName/kotlin"))
}
}
}
}

dependencies {
Expand All @@ -28,5 +38,7 @@ dependencies {
with(Deps.Koin) {
implementation(core)
implementation(android)
implementation(annotations)
ksp(kspCompiler)
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -16,7 +15,7 @@ class MainApplication : Application() {
startKoin {
androidContext(this@MainApplication)
androidLogger()
modules(appModule() + androidModule)
modules(appModule() + AndroidModule().module)
}
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
@Module
@ComponentScan("com.example.helloworldkmp.android")
class AndroidModule
4 changes: 3 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@ 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")
}
}

allprojects {
repositories {
google()
mavenCentral()
mavenLocal()
}
}

Expand Down
5 changes: 4 additions & 1 deletion buildSrc/src/main/java/Dependencies.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@

object Versions {
const val koin = "3.2.0"
const val koinAnnotaions = "1.0.0-beta-3"
}

object Deps {
Expand All @@ -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}"
}

}
7 changes: 6 additions & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,9 @@ kotlin.mpp.enableCInteropCommonization=true

# Native
kotlin.native.binary.memoryModel=experimental
kotlin.native.binary.freezing=disabled
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
2 changes: 1 addition & 1 deletion iosApp/iosApp/iOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import shared
struct iOSApp: App {

init() {
HelperKt.doInitKoin()
GreetingHelperKt.doInitKoin()
}

var body: some Scene {
Expand Down
8 changes: 8 additions & 0 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
14 changes: 13 additions & 1 deletion shared/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
kotlin("multiplatform")
id("com.google.devtools.ksp")
kotlin("native.cocoapods")
id("com.android.library")
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.helloworldkmp

import org.koin.core.annotation.Single

@Single
class Greeting(private val platform: Platform) {

fun greeting(): String {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.example.helloworldkmp

import org.koin.core.annotation.Single

@Single
expect class Platform() {
val name: String
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
package com.example.helloworldkmp.di

fun appModule() = listOf(commonModule, platformModule)
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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 6644448

Please sign in to comment.