Setup your android project for Dagger 2.33+
This is how to setup your project with the latest and greatest to date Dagger 2.33+ (written in April 2021)
For now, we will use a single component and module, which is the AppComponent
but we shall add others in next blogs. Lets get going!
New Project
We will create a new Android Project in Android Studio.
Adding Dagger Dependency
in app/build.gradle
we add this into the dependencies
def dagger_version = "2.33"
// region Dagger
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
// endregion
And then we need to enable the kapt
extenstion, using the latest gradle
Adding the app component
First we create a folder named di
and inside that we create the AppComponent.kt
// Scope annotation that the AppComponent uses
// Classes annotated with @Singleton will have a unique instance in this Component
@Singleton
// Definition of a Dagger component that adds info from the different modules to the graph
@Component(
modules = [
AppModule::class
]
)
interface AppComponent : AndroidInjector<MyApplication> {
@Component.Factory
interface Factory {
// With @BindsInstance, the Context passed in will be available in the graph
fun create(@BindsInstance context: Context): AppComponent
}
}
Then we will create the missing AppModule
in the same di
folder like
@Module(
subcomponents = [
// We add Sub Component Here
]
)
open class AppModule
Creating the App container
This is where we start using the component. to do that we can create a new class named MyApplication
like
open class MyApplication : Application() {
val appComponent: AppComponent by lazy {
initializeComponent()
}
open fun initializeComponent(): AppComponent {
return DaggerAppComponent
.factory()
.create(applicationContext)
}
}
Now, we have an unresolved reference, but that’s ok because now we are going to build the app. you can click the hammer icon at the top, I will be using the shortcut here Ctrl+F9
.
After a few seconds the project is made and now you can auto-import the DaggerAppComponent
inside the app component.
finally we add the application class to our manifest
<!-- Manifest -->
<application
android:name=".MyApplication"
Start Injecting Stuff
For me, I will create a new Presenter Class, nothing fancy just a basic service, and I will inject this service inside the MainActivity
class
Here we have the presenter class
class MainPresenter @Inject constructor() {
fun isOdd(num: Int): Boolean {
return num % 2 == 0;
}
}
Next we modify the AppComponent
to add the inject method, in which we shall inject the MainActivity
.
// ... AppComponent.kt
fun inject(activity: MainActivity)
And then we can start injecting it inside the MainActivity
using the following
private const val TAG = "MainActivity"
class MainActivity : AppCompatActivity() {
@Inject
lateinit var mainPresenter: MainPresenter
override fun onCreate(savedInstanceState: Bundle?) {
(application as MyApplication).appComponent.inject(this)
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate: ${mainPresenter.isOdd(2)}")
setContentView(R.layout.activity_main)
}
}
Now, to check things out, we can start the app. and everything should be working!
Next, We start adding components like AuthComponent
, MainComponent
, SettingsComponent
. until then, have a great time!