How does Kotlin use Android Dagger2 and kotlindagger2?
By Antonio Leiva
Time: Apr 11,201 7
Link: https://antonioleiva.com/dagger-android-kotlin/
On Android, almost everyone who creates decoupling and easily tests code will have to resort to Dagger sooner or later.
Although there are some differences in setting Dagger in Kotlin, most of them are very simple and only a few steps are required. I will explain it here today.
At the same time, we must be aware that due to Kotlin's ability, there are other methods to solve the injection problem, and there are even some specialized libraries in Kotlin to solve it.
But DaggerStill a valid choice, Even if it is not the most common, it is also one of them.
Tip:
In this article, I will not explain how to use Dagger 2, which is known. If you have any questions, read some articles about dependency injection that I have previously written.
Configure the project with Dagger 2
If you have configured the Kotlin plug-in your project, you need to configure the kapt.
If you have already used Dagger, you probably know that apt. kapt is a version of Kotlin and it creates the required self-generated class for Dagger.
To configure it, add the following content to build. gradle:
1 kapt {2 generateStubs = true3 }
You can add it before the dependency section. If you like, you can use the new experimental plug-in, which is already quite stable:
1 apply plugin: 'kotlin-kapt'
Now, you need to add the dependency between the Dagger Compiler (using kapt instead of included in the apk) and the actual Library:
1 kapt 'com.google.dagger:dagger-compiler:2.5'2 compile 'com.google.dagger:dagger:2.5'
In this way, you can start to use Dagger.
Implementation of the main module
You may know that for a master image, you need a Module and a Component.
In this simple example, the application module only returns its own instance.
To do this, we will use the @ Module annotation to create a class, receive the application instance through the constructor, save it to the attribute, and return it using the @ Provides @ Singleton annotation method.
1 @Module class AppModule(val app: App) {2 @Provides @Singleton fun provideApp() = app3 }
As you can see, even for such a simple class, its code is much simpler than using Java.
Next we need to implement Component. It needs to load a module array and describe who can inject it manually:
1 @Singleton2 @Component(modules = arrayOf(AppModule::class))3 interface AppComponent {4 fun inject(app: App)5 }
Create only one App class, which generates images:
1 class App : Application() { 2 3 val component: AppComponent by lazy { 4 DaggerAppComponent 5 .builder() 6 .appModule(AppModule(this)) 7 .build() 8 } 9 10 override fun onCreate() {11 super.onCreate()12 component.inject(this)13 }14 }
What's interesting here is that due to the lazy statement,We can specify the image value in the attribute definition.Therefore, the read-only access to this attribute is obtained.
Before component. inject (this) is completed, the code-defined attributes will not be executed. So by then, this already exists and can be created securely.
Implementation scope of each module
The range of the module only allows the part of the graph to survive in the lifecycle of other objects.
This method is used, for example,We canCreate a subgraph in the survival of the Activity.
We will create our own modules as needed:
1 @Module2 class HomeModule(val activity: HomeActivity) {3 }
Subcomponent is very similar to the method described above, indicating that it will inject HomeActivity:
1 @Singleton2 @Subcomponent(modules = arrayOf(HomeModule::class))3 interface HomeComponent {4 fun inject(activity: HomeActivity)5 }
The plus method of AppComponent indicates the type that the component can be added to the child component:
1 interface AppComponent {2 ...3 fun plus(homeModule: HomeModule): HomeComponent4 }
Now, you only need to declare the child components in HomeActivity:
1 val component by lazy { app.component.plus(HomeModule(this)) }
You can inject it after setContentView:
1 override fun onCreate(savedInstanceState: Bundle?) {2 super.onCreate(savedInstanceState)3 setContentView(R.layout.activity_main)4 component.inject(this)5 }
If you want to know where the app comes from, it is an extension property like this:
1 val Activity.app: App2 get() = application as App
If you have your own custom application, this is just a simple way to avoid each access to it.
Conclusion
In Kotlin, It is also easy to use Dagger 2. You have no reason to implement a large decoupling architecture in Kotlin.
If you want to learn more about this and use Kotlin to create your own Android APP, take a look at the Free Guide to learn how to create your first project or buy this book directly, learn how to create a complete APP from scratch.