dagger2-android,Dagger2在Android开发中的新用法.

本文介绍了Dagger2在Android应用中的使用,特别是针对依赖注入的复杂性问题,探讨了Dagger2的新特性。通过引入AndroidInjectionModule,创建AndroidInjector,使用@ContributesAndroidInjector简化子组件的创建,以及实现HasActivityInjector,最终减少MyAppComponent在ActComponent注入过程中的参与,降低耦合。作者还分享了如何逐步实施这些新用法,以实现更优雅的依赖注入。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文假设读者已经有一定Dagger2使用经验

使用疑惑

之前工作中一直在使用dagger2进行开发,用起来确实很爽,但是我从我第一次使用我就一直有一个问题或者说疑问(本人才疏学浅脑子不够使),通常情况下我们有如下清单

MyApplication,MyAppComponent,MyAppModule

ActActivity,ActComponent,ActModule

简单解释下,MyAppModule提供全局单例功能,比如打印日志,ActModule提供Activity级别的功能比如发起网络请求(只是举个栗子),现在我们希望在发起网络请求的时候打印日志,那么解决方法也很简单——SubComponent或者Component(dependencies=X.class)

于是我们首先在MyApplication中初始化MyAppcomponent(使用抽象类实现单例)

@Component(modules = MyAppModule.class)

public abstract class MyAppComponent {

......

//使用SubComponent功能来完成component的组合

abstract ActComponent plus();

}

@Subcomponent(modules = ActModule.class)

public interface ActComponent {

void inject(ActActivity act);

}

public class MyApplication extends Application {

@Override

public void onCreate() {

super.onCreate();

MyAppComponent.getInstance().inject(this);

}

}

然后就是就在Activity中使用ActComponent来提供注入功能,代码看上去就像如下...

MyAppComponent.getInstance()

.plus()

.inject(this);

为神马我使用的明明是ActComponent,关MyAppComponent什么事?(我最开始学习使用dagger2的时候完全无法接受这种写法),而且这似乎不太符合依赖注入的一个根本原则a class shouldn’t know anything about how it is injected.

新用法

谷歌爸爸很明显也注意到了这个问题,谁叫Dagger2在Android开发中也那么火呢,于是在Dagger2新版本中我们有了一个新东西dagger.android

Gradle引入方式

//dagger2

compile 'com.google.dagger:dagger:2.11'

compile 'com.google.dagger:dagger-android:2.11'

compile 'com.google.dagger:dagger-android-support:2.11'

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'

Talk is cheap, show me the code

结合Demo和官方文档粗略翻译如下

在AppComponent中安装AndroidInjectionModule

@Component(modules = {AndroidInjectionModule.class})

public interface AppComponent {

//....

}

2.编写实现了AndroidInjector的Lychee3Activity

@Subcomponent(modules = ...)

public interface ActSubComponent extends AndroidInjector {

@Subcomponent.Builder

public abstract class Builder extends AndroidInjector.Builder {

}

}

3.定义了ActSubComponent后,将其安装在绑定了ActSubComponent.Builder的Module中,并且将该Module安装在我们的AppComponent中

@Module(subcomponents = {ActSubComponent.class})

public abstract class BuildersModule {

@Binds

@IntoMap

@ActivityKey(Lychee3Activity.class)

abstract AndroidInjector.Factory extends Activity> lychee3Activity(ActSubComponent.Builder builder);

}

@Component(modules = {AndroidInjectionModule.class,

BuildersModule.class})

public interface AppComponent {

//....

}

但是如果你的ActSubComponent若同我们在步骤2中定义的一样,不管在类中还是在其Builder中没有的方法和超类型,你可以用下面的代码跳过2,3步骤

原文 Pro-tip: If your subcomponent and its builder have no other methods or supertypes than the ones mentioned in step #2, you can use @ContributesAndroidInjector to generate them for you

@ContributesAndroidInjector

abstract Lychee2Activity lychee2Activity();

4.让你的MyApplication实现HasActivityInjector,并且注入DispatchingAndroidInjector,

public class MyApplication extends Application implements HasActivityInjector {

@Inject

DispatchingAndroidInjector dispatchingAndroidInjector;

@Override

public void onCreate() {

super.onCreate();

DaggerAppComponent.builder().AppContent(this).build().inject(this);//最好结合demo来看,不然AppContent是啥你不知道

}

@Override

public AndroidInjector activityInjector() {

return dispatchingAndroidInjector;

}

}

5.最后,在你Lychee3Activity和Lychee2Activity中的onCreate中,调super.onCreate()之前调用AndroidInjection.inject(this);

public class Lychee2Activity extends AppCompatActivity {

public void onCreate(Bundle savedInstanceState) {

AndroidInjection.inject(this);

super.onCreate(savedInstanceState);

}

}

至此,新东西的使用差不多就到这了,但是为什么我会有一种“天,怎么越来越复杂啦”的感觉呢...

第一次写文章,有什么不足的地方或者你觉得很不爽的东西欢迎交流指出。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值