Since 4.0.0-RC2 release this does not work anymore...
# koin
l
Since 4.0.0-RC2 release this does not work anymore fpr Jetpack Compose, unresolved reference for
androidLogger
,
androidContext
and `modules`:
Copy code
setContent {
            KoinApplication(application = {
                // Log Koin into Android logger
                androidLogger()
                // Reference Android context
                androidContext(this@MainActivity)
                // Load modules
                modules(
                    module {
                        // Provide lifecycle for lifecycle aware components
                        single<Lifecycle> { this@MainActivity.lifecycle }
                    },
                )
            }) {
                MyTheme {
                    Root.EntryPoint(defaultRoute = Root.ScannerRoute)
                }
            }
        }
and the documentation does not reflect the changes of the release.
application = {}
must now return a KoinApplication. How is it done correctly?
a
Hello, yes no yet documented. Good catch. I was not sure about the initial declaration that felt hard to extract. Now you can write something like:
Copy code
fun koinConfiguration() = koinApplication {
    printLogger(Level.DEBUG)
    modules(appModule)
}

@Composable
fun App() {
    KoinApplication(::koinConfiguration){
        ...
    }
}
m
Copy code
KoinApplication(application = {
    koinApplication {
        modules(appModule, ...)
    }
}) {
    MyTheme {
πŸ‘Œ 1
a
overloading is hard in this function signature πŸ€” do you think this is "shocking" in terms of usage?
m
The implementation in the previous version felt more straightforward if that's what you are asking
πŸ‘† 1
a
ok interesting
m
Also this new implementation is causing koinApplication to initialise twice on iOS for me I found a way around it and opened an issue on Github πŸ˜„
a
arf ... ok, I can go back on previous πŸ˜•
m
fixing the problem on iOS and updating the documentation might also work
a
I'll keep that natural way to do then:
can be extracted if needed
m
Sounds great Thanks πŸ˜„
πŸ™ 1
will this also solve this issue ?
a
did it was working previous RC2?
m
Yes
a
ok then, it should work back πŸ‘
m
Does this commit fix it?
a
yes
m
I tried something like this to see if this fixes it
Copy code
@Composable
fun App() {
    MyKoinApplication(application = {
        modules(personModule)
    }) {
        val person = koinInject<Person>()
        println("Person: ${person.hashCode()}")
        Scaffold {
            val person2 = koinInject<Person>()
            println("Person2: ${person2.hashCode()}")
        }
    }
}

@OptIn(KoinInternalApi::class)
@Composable
@Throws(KoinApplicationAlreadyStartedException::class)
fun MyKoinApplication(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit,
) {
    val koin = rememberKoinApplication(koinApplication = koinApplication(application))
    CompositionLocalProvider(
        LocalKoinApplication provides koin,
        LocalKoinScope provides koin.scopeRegistry.rootScope,
        content = content
    )
}
And the problem still exists
in order to prevent the app from being created twice I had to do this:
Copy code
@OptIn(KoinInternalApi::class)
@Composable
@Throws(KoinApplicationAlreadyStartedException::class)
fun MyKoinApplication(
    application: KoinAppDeclaration,
    content: @Composable () -> Unit,
) {
    val koinApplication = remember { koinApplication(application) }
    val koin = rememberKoinApplication(koinApplication = koinApplication)
    CompositionLocalProvider(
        LocalKoinApplication provides koin,
        LocalKoinScope provides koin.scopeRegistry.rootScope,
        content = content
    )
}
or remove the
key1
parameter of remember in
rememberKoinApplication
Copy code
@OptIn(KoinInternalApi::class)
@Composable
inline fun rememberKoinApplication(koinApplication: KoinApplication): Koin {
    val wrapper = remember {
        CompositionKoinApplicationLoader(koinApplication)
    }
    return wrapper.koinApplication.koin
}
a
but then, the old KoinApplication was working? not with the new CompositionKoinApplicationLoader?
I see
we are creating a koinApplication on the fly here:
Copy code
val koin = rememberKoinApplication(koinApplication = koinApplication(application))
should be later then
m
yes, the problem seems to be caused by
CompositionKoinApplicationLoader
The
start()
function is called twice on iOS but not on Android
πŸ‘ 1