0. According to?

  • Why dependency injection?

A: Decoupling.

  • Decouple who from whom?

Decouple object creation from object usage.

  • How to understand?

Two concepts are introduced here: IoC container and DI dependency injection. We entrust the creation and destruction of objects to IoC container (called inversion of control), and provide objects when needed (called injection), so as to reduce code coupling of complex systems and improve programming efficiency.

If you are familiar with Spring Boot, you will easily understand that the core of Spring is the IoC container and its management of Bean (object) life cycle.

  • Hilt and Dagger?

Hilt is the scenarioization of Dagger, which is encapsulated and used in Android scenarios. Therefore, introducing dependencies requires both the Dagger library and Hilt library, and it is inevitable to have access to a lot of Dagger knowledge

1. How?

  1. Introduction of depend on
// File name: build.gradle (root directory)
buildscript {
	...
	dependencies {
		...
        classpath 'com. Google. Dagger hilt - android - gradle - plugin: 2.28 alpha'}}Copy the code
// File name: build.gradle (:app)
apply plugin: 'com.android.application'
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'. dependencies { ... implementationCom. Google. "dagger hilt - android: 2.28 alpha." "
	kapt Com. Google. "dagger hilt - android - the compiler: 2.28 alpha." "
	implementation 'androidx. Hilt: hilt - lifecycle - viewmodel: 1.0.0 - alpha02'
	kapt 'androidx. Hilt: hilt - compiler: 1.0.0 - alpha02'
}
Copy the code
  1. Add the @hiltAndroidApp annotation to the Application class (required, if you don’t have a custom Application class, you need to create one and declare it in the AndroidManifest file) as the boundary of the life cycle attached to the Dagger.
@HiltAndroidApp
public class App extends Application {
	@Override
    public void onCreate(a) {... }}Copy the code
  1. We take RecyclerAdapter object as an example to illustrate the object creation process
class PictureAdapter @Inject constructor() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
	override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int){.. }override fun getItemCount(a): Int{... }}Copy the code

As you can see, for objects constructed without arguments, just add @inject before the constructor.

  1. Use of objects
@AndroidEntryPoint
class HomeActivity : AppCompatActivity() {
	// Add @inject lateinit to the declared object. Object creation and assignment are handled by Hilt
	@Inject
    lateinit var pictureAdapter: PictureAdapter

	 override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        ...
        // Assign the adapter directly to RecyclerView
        pictureRecyclerView.setAdapter(pictureAdapter)
     }
}
Copy the code

As you can see, for the Activity class, just add the @AndroidEntryPoint annotation on the class and add @Inject lateInit on the variable you want to Inject.

In this paper, the general principle and the use of Hilt only brief explanation, concrete can be to developer.android.com/training/de… , original article, reprint prohibited.