本系列博客主要用于自己学习积累,顺便帮助新手入门,如有问题,多多包涵。更详细的一些使用可以看看其他更加详细的博客。
在安卓实际开发中,我们需要用到许多的activity以及fragment,这样我们在进行依赖注入的之后都要进行一些重复的操作,比如在comonent中加入void inject(XXXActivity activity),以及在Activity中加入DaggerMyAppComponent.builder().build().inject(this),好在dagger2也提供了解决方法,让我们不需要每次去写inject方法。
添加依赖
implementation 'com.google.dagger:dagger:2.21'
annotationProcessor 'com.google.dagger:dagger-compiler:2.21'
implementation 'com.google.dagger:dagger-android:2.21'
implementation 'com.google.dagger:dagger-android-support:2.21'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.21'
复制代码
开始改造
AndroidInjector
创建一个接口继承AndroidInjector
@Subcomponent(modules = {
AndroidInjectionModule.class,
})
public interface MainActivit