Open In App

How to Use Dagger in Dynamic Feature Module in Android?

Improve
Improve
Like Article
Like
Save
Share
Report

In our previous article, we learned how to set up dagger in an Android project with a multi-module architecture. Click here to see how we can use Dagger in Multi-Module Architecture. We will be expanding on the project that we worked on in our previous project. But first, we must learn how to create and configure a dynamic-feature module. Click here to find out how to do it. Let’s start with a quick recap.

What exactly are Dynamic feature modules?

It is similar to any other module in Android, except that it must be separated from the module. It comes with app bundles and allows users to download or delete modules based on their needs. So, let us get started.

Step 1: First, in the project, I will create a dynamic-feature module from:

Go to File-> Then Click on New -> Tap, New Module and select Dynamic Feature Module then name it as:

featureThree

Step 2: After that, we’ll add

android {
    ....
    dynamicFeatures = [":featureThree"]
}

in the build.gradle file of the app. In featureThree’s build.gradle, we’ll include the app, base, and dagger dependencies, such as,

implementation project(':app')
implementation project(':base')
implementation "com.google.dagger:dagger:2.26"
kapt "com.google.dagger:dagger-compiler:2.26"

Step 3: The new module

In the featureThree module, we will now create an activity called FeatureThreeActivity. Now, all we have to do is to perform some basic steps to get our work in action. Will we include FeatureThreeComponent in the component package and update the code as follows?

Kotlin




@FeatureThreeScope
@Component(
    dependencies = [BaseComponent::class],
    modules = [FeatureThreeModule::class]
)
interface GeeksComponent {
  
    fun inject(activity: FeatureThreeActivity)
  
}


Now, in the module package, we will create FeatureThreeModule.

@Module
class FeatureThreeModule {
}

Step 4: Finally, we will update the FeatureThreeActivity code to implement the FeatureThreeComponent, as shown below.

Kotlin




override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.gfgMainActivity)
    DaggerFeatureThreeComponent
        .builder()
        .baseComponent(InjectUtils.provideBaseComponent(applicationContext))
        .build()
        .inject(this)
}


Rather than creating a new instance, we are just passing some vital information from here to there to make the complexity easier. More information can be found by clicking here.

Step 5: Checking and Ticking

We’ll now inject the DatabaseService and NetworkService into FeatureThreeActivity and update the activity’s code as follows:

Kotlin




class ThirdFeatureActivity : AppCompatActivity() {
    @Inject
    lateinit var aDatabaseServer: DatabaseService
    @Inject
    lateinit var someNetworkService: NetworkService
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.gfgMainActivity)
        DaggerFeatureThreeComponent
            .builder()
            .baseComponent(InjectUtils.provideBaseComponent(applicationContext))
            .build()
            .inject(this)
  
        Log.d("Geeks for Geeks \n Spandan Saxena", databaseService.toString())
    }
}


We’ve finished configuring the dagger for this module. Now we must determine whether or not the implementation of the dagger in the dynamic-feature module was successful. As a result, we will verify that the injection of DatabaseService and NetworkService in MainActivity and FeatureThreeActivity receives the same memory allocation. We’ll test it with the Log statement we added to the activity files. Now, we will update the code in the MainActivity’s onCreate() function to open the FeatureThreeActivity using,

Kotlin




val toMainIntent = Intent().setClassName(this, "com.geeksforgeeks.sample.feature.three.FeatureThreeActivity")
startActivity(toMainIntent)


Conclusion

So, as you can see, we’re getting the same memory allocation for database service across multiple modules. So, this is how the dagger can be used in dynamic feature modules.



Last Updated : 20 Dec, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads