How to inject/provide repository from Android Dynamic Feature module into static module

Im investigating Dynamic Feature modules in my current Android Application.

My application consists of an App Module, a number of static modules and a single Dynamic Feature Module.

Im attempting to inject a repository class declared in the Dynamic Feature Module into a repository class in one of my static modules.

I have a common shared module that contains an interface that defines the API of

My dynamic feature module has a repository class that resembles this:-

class DynamicFeatureRepository @Inject constructor(private val applicationContext: Context) : MyExperimentable {

    override fun accessDynamicModuleRawData(): List<MyExperimentDO> {
        val myExperimentDOs = mutableListOf<MyExperimentDO>()
        applicationContext.resources.openRawResource(R.raw.data).use {
            val reader = BufferedReader(InputStreamReader(it))
            while (reader.ready()) {
                val columns = reader.readLine().split(",")
                myExperimentDOs.add(
                    MyExperimentDO(
                        myExperimentId = columns[0].toLong(),
                        selected = columns[1].toBoolean(),
                        myExperimentName = columns[2],
                        myExperiment = columns[3]
                    )
                )
            }
        }

        return myExperimentDOs
    }

    override fun generate(data: String): Map<String, String> {

        return emptyMap()
    }
}

I have another static module that uses the above Dynamic Feature Module repository

I believe Dagger subcomponents will allow me to inject this Dynamic Feature Module repository into my static modules classes, however I cannot see how to achieve this.

So far I have the following Dagger classes declared in my common module:-

import dagger.Module
import dagger.Subcomponent
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent

@Subcomponent
interface MyExperimentComponent {
    fun inject(myExperimentable: MyExperimentable)

    @Subcomponent.Factory
    interface Factory {
        fun create(): MyExperimentComponent
    }
}

@InstallIn(SingletonComponent::class)
@Module(subcomponents = [MyExperimentComponent::class])
class SubcomponentsModule {}

In my Dynamic Feature Module I have this Dagger class:-

@InstallIn(SingletonComponent::class)
@Module
class ExperimentModule() {
    @Provides
    fun getMyExperiment(@ApplicationContext appContext: Context): MyExperimentable {
       return DynamicFeatureRepository(appContext)
    }
}

In my static module I have this @Inject

@Inject
lateinit var myExperimentable: MyExperimentable

My Application builds and runs, however the @Inject in my static module is not being satisfied as my app fails with

kotlin.UninitializedPropertyAccessException: lateinit property myExperimentable has not been initialized

Where have I gone wrong?

How do I @Provide my dynamic feature module repo for one of my static modules

Source: Android Questions

LEAVE A COMMENT