question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IllegalStateException: at org.koin.core.context.GlobalContext.get

See original GitHub issue

I’ve just released an app using Koin 2.2.2 and I’m seeing crash reports on devices running Android 8.1 (SDK 27). I haven’t seen it on any other versions of Android yet, so it could be a bug in 8.1. So far, the devices it’s crashing on are Motorola Moto G Plus and something called DigiLand DL1016. Edit: Actually, I just got a crash report on Samsung Galaxy Note8 running Android 9 (SDK 28). So it’s not just 8.1.

I’m just calling getViewModel<MyViewModel>() in MainActivity.onCreate().

java.lang.RuntimeException: 
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2861)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2943)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1630)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6626)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:811)
Caused by: java.lang.IllegalStateException: 
  at org.koin.core.context.GlobalContext.get (GlobalContext.java:1)
  at org.koin.android.ext.android.ComponentCallbackExtKt.getKoin (ComponentCallbackExtKt.java:1)
  at org.koin.androidx.viewmodel.ext.android.ActivityExtKt.getViewModel (ActivityExtKt.java:1)
  at com.mypackage.MainActivity.onCreate (MainActivity.java:1)
  at android.app.Activity.performCreate (Activity.java:7032)
  at android.app.Activity.performCreate (Activity.java:7023)
  at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1236)
  at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2814)
  at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2943)
  at android.app.ActivityThread.-wrap11 (Unknown Source)
  at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1630)
  at android.os.Handler.dispatchMessage (Handler.java:106)
  at android.os.Looper.loop (Looper.java:164)
  at android.app.ActivityThread.main (ActivityThread.java:6626)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:811)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:11

github_iconTop GitHub Comments

3reactions
dnsknrcommented, Mar 10, 2021

Hi everyone,

since I also got the error and it seems to be related to a similar issue I had with Dagger before migrating to Koin. It had to do with the app’s custom Application class not being used on some Android versions.

See following issues which seem to be related: https://github.com/InsertKoinIO/koin/issues/286#issuecomment-437994119 https://github.com/google/dagger/issues/748#issuecomment-339235062

I updated my apps as follows:

object KoinStarter {
    @Synchronized
    fun start(context: Context): Koin {
        return GlobalContext.getOrNull() ?: startKoin {
            androidLogger()
            androidContext(context.applicationContext)
            modules(appModule)
        }.koin
    }
}

Instead of starting Koin in my Application class I am now starting it in my activities as follows:

override fun onCreate(savedInstanceState: Bundle?) {
    KoinStarter.start(this)
    super.onCreate(savedInstanceState)
}

I am still in staged rollout, but so far it looks promising. But with Dagger it took me several iterations because I could never reproduce this issue.

2reactions
dnsknrcommented, Mar 15, 2021

I do not know, I started using Koin with 2.2.2 and so cannot tell if any previous version has the same issue. My assumption is, any previous version depending on a custom Application class will face sooner or later some weired exceptions on 7.x and 8.x Android versions, sometimes this may even be vendor specific (saw it for some Samsung Android versions).

Actually I never received any user complaint about this, just a ton of crash reports on Google Play, never on Firebase. Seems to crash the app even before Crashlytics is ready. So at the end this could also just be some background app starting / house keeping of Android (read something about backup processes) that the user never notice. As said before I could never reproduce the issue, but with my code above the crash reports are gone.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java.lang.IllegalStateException: KoinApplication has not been ...
While running the application in catlog I see the following message. java.lang.IllegalStateException: KoinApplication has not been started.
Read more >
Start Koin
The GlobalContext is a default JVM context strategy for Koin. It's called by startKoin and register to GlobalContext . This will allow us...
Read more >
jvmMain.org.koin.core.context.GlobalContext.kt Maven ...
jvmMain.org.koin.core.context.GlobalContext.kt maven / gradle build tool code. The class is part of the package ➦ Group: io.insert-koin ➦ Artifact: ...
Read more >
java.lang.ClassCastException: org.koin.core.context ...
I trying to migrate from Koin 2.1.6 to 2.2.2 or 3.0.1 but on both versions during run tests I have many issues related...
Read more >
Dependency Injection with Kotlin and Koin - Auth0
Koin is a dependency injection framework for Kotlin. ... package com.matthewcasperson import org.koin.core.context.GlobalContext.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found