opened
haroldadmin
username_0
**Describe the bug**
I need some help with setting up Koin in a project with Dynamic Feature modules.

My app structure looks like this:
```
- app
---- Application.kt
---- MainActivity.kt

- base
---- SharedKoinModules.kt

- users
---- UsersKoinModules.kt
---- UsersFragment
---- UsersViewModel
---- UsersRepository

- json-placeholder-repository
---- <repo files>
```

* Users module depends on app module
* App module depends on Base module
* App Module is used to initialize **only** the koin-modules in `base/SharedKoinModules` in the Application class's `onCreate()` method.
* Koin-Modules in Users dynamic feature module are loaded in the `onCreate()` method of `UsersFragment` using the `loadKoinModules()` function.
* Users Dynamic Feature module has no Activities, only Fragments.


class MyApplication: Application() {
    ...
    override fun onCreate() {
        super.onCreate()
        startKoin {
            androidContext(this@MyApplication)
            modules(SharedKoinModules.networkModule)
        }
    }
}

I can't load the users/UsersKoinModules file because the App module can not depend on a dynamic feature module, and hence I don't have access to that file here.

class UsersFragment: Fragment() {
    ...
    override fun onCreate() {
        super.onCreate()
        loadKoinModules(UsersKoinModules.usersModule)
    }
}

This works fine...until I rotate the screen. On screen rotation, the application crashes with the following error:
```
Caused by: org.koin.core.error.DefinitionOverrideException: Already existing definition or try to override an existing one: [type:Single,class:'com.myapp.json_placeholder_repository.service.users.UsersService']
```

**To Reproduce**
Sample project available here: https://github.com/username_0/UnitTestingSample/tree/koin-issue

Steps to reproduce the behavior:
1. Build and run the app on a device.
2. Should launch fine.
3. Rotate the phone.
4. App should crash.

**Expected behavior**
Need guidance on how to use Koin with dynamic feature modules.

Koin Version: 2.0.0-rc-2

**Additional Context**
If I add an `if (savedInstanceState == null)` check around `loadKoinModules()` call in UsersFragment `onCreate()`, then the crash is fixed. However, this is not a viable solution because if the app is killed due to low memory and then resumed later, then savedInstanceState will not be null -> `loadKoinModules()` will not be called -> Required koin modules will not be loaded -> App will crash.
Loading Koin Modules in Dynamic Feature Modules results in org.koin.core.error.DefinitionOverrideException
issue
created
haroldadmin
username_0
[This article](https://proandroiddev.com/dynamic-feature-module-with-dependency-less-navigation-koin-774694f41f63) has the solution to my problem.

Just place the `loadKoinModules()` call as a top level lazily initialized property in whatever Fragment/Activity is first loaded in the Dynamic Feature Module.

```
private val loadFeatures by lazy { loadKoinModules(...) }
private fun injectFeatures = loadFeatures

class MyFragment: Fragment() {
  override fun onCreate() {
    injectFeatures()
    ...
  }
}
```

The lazy property is evaluated only once, and since it is a top level property it is not recreated on every configuration change. Works when the process is killed too.
comment
closed
haroldadmin
username_0
issue