Preface
It is said on the Internet that Dagger2 is more difficult to get started. I have encountered many unintelligible or vague knowledge points when I read a lot of materials and used it, and most of the blog materials are relatively old. Suddenly for a moment, I suddenly understood why, so I summarized 4 articles. It is still very cumbersome to use in java. Don't be afraid to take you to get started and apply it to our Android project.
This Dagger2 explanation is divided into 4 articles:
1. Basic knowledge of Dagger2 and its use in Java (1)
2. Basic knowledge of Dagger2 and its use in Java (2)
3. Advanced knowledge of Dagger2 and use in Android
4. Dagger2 gorgeous Used in MVP framework
Through the study of the first two articles, I am already familiar with the use of Dagger2 in Java. But you will find that every time you need to inject, you must write the corresponding Component, and then Inject to the relevant side. And DaggerComponent.builder().build.inject(activity) is required on every page. This is too cumbersome, and I feel cumbersome before starting. So powerful google launched the extension package dagger.android.
Still the same, some of the code posted on the blog is omitted for ease of understanding.
The use of Dagger2 in Android
This article is just one section. As long as the relevant information is configured, there is only one global Component. You can write more than one Module, in fact, you can only write two. Why, see below.
The first is to add dependencies , the previous Java dependencies are not related here.
implementation 'com.google.dagger:dagger-android:2.24'
// if you use the support libraries(就是你需要v4.Fragment就需要加上support包)
implementation 'com.google.dagger:dagger-android-support:2.24'
annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
Here are two examples, one with Module and one without Module used in Android. The child class Children without Module is as follows, and the construction method is marked with @Inject:
public class Children {
@Inject
public Children() {
}
}
Then there is the superhuman SurperMan with Module. It is assumed that it is a third-party library and cannot be annotated with @Inject.
public class SurperMan {
}
Our Module at this time
@Module
public class SurperManModule {
@Provides
SurperMan surperManProvides(){
return new SurperMan();
}
}
At this time, the dagger.android package provides a @ContributesAndroidInjector annotation to save us from initializing Component in Activity, as follows
@Module
//抽象类,名字自定义,
public abstract class NeedInjectModules {
//这个方法的意思标识要注入到ForAndroidActivity页面。如果需要在MainActivity页面的话。继续加这个方法,返回值为
//MainActivity即可
@ContributesAndroidInjector
abstract ForAndroidActivity inject();
}
As mentioned before, it is in the dagger.android package. The general idea is to initialize ApplicationComponent in Application, and then we mark it with @ContributesAndroidInjector, and he helped me realize that Component depends on Component internally. Then our only component looks like this
//这里AndroidSupportInjectionModule是系统的,必须加上
//NeedInjectModules是我们要注册到Activity的
//SurperManModule,surperMan的
@Component(modules = {
AndroidSupportInjectionModule.class,
NeedInjectModules.class,
SurperManModule
})
//继承AndroidInjector<T>,泛型就是我们的application
public interface AppComponent extends AndroidInjector<MyApplication> {
//还记得@Component.Builder的用法吗,可以看之前的。
@Component.Builder
interface Builder {
@BindsInstance
Builder application(Application application);
AppComponent build();
}
}
After all these preparations are done, remember to Make Project. The next step is to focus. Our Application inherits DaggerApplication. Here we will implement a method to return our AppComponent.Builder, as follows
public class MyApplication extends DaggerApplication {
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent.builder().application(MyApplication.this).build();
}
}
This is also the key point. To use in Activity or Fragment, just inherit our DaggerActivity&DaggerFragment. Look at our Activity directly
public class ForAndroidActivity extends DaggerAppCompatActivity {
@Inject
Children children;
@Inject
SurperMan surperMan;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_astudy);
LogUtils.i("已经生成实例",children.hashCode()+"");
LogUtils.i("已经生成实例",surperMan.hashCode()+"");
}
}
So far, the simple use in Android has all been introduced, is the code more concise? Do you love it already!
This article github Demo address
Don't get me wrong, it's not asking for money. Can you leave a footprint star?