title: Koin Context Isolation: setup correctly and avoid random NoBeanDefFoundException
body: <p>I have an Android library module that I package and publish. It's a dependency in different apps, some that use Koin and some that don't, therefore I want to use Koin's <a href="https://insert-koin.io/docs/reference/koin-core/context-isolation" rel="nofollow noreferrer">context isolation</a>.</p>
  <p>Having followed the docs, I added a koin component and context as follows:</p>
  <pre class="lang-kotlin prettyprint-override"><code>internal object LocalKoinContext {
      lateinit var koinApplication: KoinApplication
  }
  
  // Custom KoinComponent using the local instance &amp; not the Global context
  interface CustomKoinComponent : KoinComponent {
      // override the used Koin instance to use the local koin application instance (LocalKoinContext.koinApplication)
      override fun getKoin(): Koin = LocalKoinContext.koinApplication.koin
  }
  </code></pre>
  <p>and add create the koinApplication  like this:</p>
  <pre class="lang-kotlin prettyprint-override"><code>// Local Koin application instance
  LocalKoinContext.koinApplication = koinApplication {
      // use AndroidLogger as Koin Logger - default Level.INFO
      androidLogger(Level.ERROR)
  
      // use the Android context given there
      androidContext(applicationContext)
  
      // load properties from assets/koin.properties file
      androidFileProperties()
  
      // declare used modules
      modules(theModule)
  }
  </code></pre>
  <p>I have an activity that uses a view model that lives within the local Koin component and I used to instantiate the koinApplication within the <code>onCreate</code> of the that activity:</p>
  <pre class="lang-kotlin prettyprint-override"><code>class FirstActivity : AppCompatActivity(),
      CustomKoinComponent {
  
      private val sharedViewModel by viewModel&lt;MySharedViewModel&gt;()
      ...
  
      override fun onCreate(savedInstanceState: Bundle?) {
          ...
          // Local Koin application instance
          LocalKoinContext.koinApplication = koinApplication {
               // use AndroidLogger as Koin Logger - default Level.INFO
               androidLogger(Level.ERROR)
  
               // use the Android context given there
               androidContext(applicationContext)
  
               // load properties from assets/koin.properties file
               androidFileProperties()
  
               // declare used modules
               modules(theModule)
           }
       }
  </code></pre>
  <p>The issue is that I get random <code>NoBeanDefFoundException</code></p>
  <pre><code>Caused by org.koin.core.error.NoBeanDefFoundException: No definition found for class:'my.package.name.MySharedViewModel'. Check your definitions!
         at org.koin.core.scope.Scope.throwDefinitionNotFound(Scope.java:264)
         at org.koin.core.scope.Scope.resolveInstance(Scope.java:233)
         at org.koin.core.scope.Scope.get(Scope.java:204)
         at org.koin.androidx.viewmodel.factory.DefaultViewModelFactory.create(DefaultViewModelFactory.java:11)
         at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
         at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
         at org.koin.androidx.viewmodel.ViewModelResolverKt.get(ViewModelResolverKt.java:23)
         at org.koin.androidx.viewmodel.ViewModelResolverKt.resolveInstance(ViewModelResolverKt.java:12)
         at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExtKt.java:86)
         at org.koin.androidx.viewmodel.scope.ScopeExtKt.getViewModel(ScopeExtKt.java:72)
         at org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getViewModel(ViewModelStoreOwnerExtKt.java:68)
         at org.koin.androidx.viewmodel.ext.android.ViewModelStoreOwnerExtKt.getViewModel$default(ViewModelStoreOwnerExtKt.java:58)
         at my.package.name.FirstActivity$special$$inlined$viewModel$default$1.invoke(FirstActivity.java:75)
  
  </code></pre>
  <p>I suspect it's because sometimes the koinApplication doesn't have time to set itself up but as it's rare I haven't been able to reproduce the issue.</p>
  <p>In the answer to <a href="https://stackoverflow.com/a/56352142/2414033">this stack question</a>, the dev creates a <code>ContentProvider</code> to instantiate the koinApplication. I've tried it out and so far so good (although, I will only know in months from now), but is this truly the way to go? Seems to be an overkill to have to create a content provider just for that.</p>
  <p>Can anyone shed some light on where we are supposed to instantiate the local <code>KoinApplication</code>?</p>