One would be, app: Here app is the package created when creating the project. An exploration into the Hilt library introduced in Dagger 2.28. Dagger2 will actually create the DI graph based on these modules. The application class has implemented the And now if we run the app and check the Logcat, we will see,Here, you can see we now have the same memory address located to both the DatabaseService instance in the app's MainActivity and in FeatureOneActivity.This means that the same instance is used across all the modules in the app.So, to use dagger in a project which is multi-modular, we need to make sure that we pass the instance of BaseComponent across the app. This was a tad tricky to set-up with a fair amount of knowledge required about how to build a dependency graph specific to Dagger 2. In this article I want to explain the approach we ended up taking a year ago and that we are still using. How To Setup Modules In Dagger 2. This annotation will tell Dagger to add this String to a generically typed Set, Set. Inside that monolithic app we had our features kind of separated using a In a simplified world the code would look something like this:By doing this, we have access to our ExpensiveObject from all the subcomponents that have What we really have is a circular dependency. By using our site, you acknowledge that you have read and understand our Q&A for Work In this case, using The reason here is, we are creating a new instance of We need to share the instance of For that, we will create an interface in our base module like,Now, this interface would be implemented by the Application class which we created in our app module like,In this, we need to return Here, we created the baseComponent variable and returned it from provideBaseComponent function.Now, to share the Here, we created a function provideBaseComponent which takes the context as a parameter. This means that for this dependency, our key will be “Test” and the value will be “A”. However, our feature needs to know about our The most obvious thing we can do is to extract that dependency into another module. The MULTI-Module is a non-profit open-source project, by hobbyists, for hobbyists. One of the advanced features Dagger2 gives us is Let’s say we wish to have a Set of dependencies (a collection containing no duplicates) be injectable, we can achieve this quite easily using the @IntoSet annotation.Here we have a standard module which is providing a String, “A”. Thank you for reading. Back then, there was a lot of talking around the subject but no one really focused on how you could use Dagger in a multi-module environment which was one of the main problems I had. By clicking “Post Your Answer”, you agree to our To subscribe to this RSS feed, copy and paste this URL into your RSS reader. In this blog, we would have 4 modules to start off with. @Module class ModuleA{ @Provides @IntoSet fun provideStringA(): String = "A" } Here we have a standard module which is providing a String, “A”. In theory it looks like a simple exercise but in reality is like playing Jenga, one tiny mistake moving dependencies will make the whole project fall apart pretty quickly.Let’s see what happens if we move our feature to a module.Right off the bat we can see this is not going to work. After I published the article I got a lot of questions about how I would approach a multi-module project using dagger-android instead. Since we are now basically giving Dagger a new key-value pair of , by returning an Int instead of a String with the providing method.If you were curious, here’s what the code looks like for the @StringKey annotation both in Java and Kotlin respectively:If you would like to give your Map a key that is not already included, you can easily create your own.

Is that a dependency you are going to share with other modules? Back then, there was a lot of talking around the subject but no one really focused on how you could use Dagger in a multi-module environment which was one of the main problems I had. In a multi-module project that ships as a single APK with no dynamic feature modules, it's common to have an app module that can depend on most modules of your project and a base or core module that the rest of the modules usually depend on.