欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

详解Dagger2在Android开发中的新用法

程序员文章站 2022-05-28 10:17:08
本文假设读者已经有一定dagger2使用经验 使用疑惑 之前工作中一直在使用dagger2进行开发,用起来确实很爽,但是我从我第一次使用我就一直有一个问题或者说疑问...

本文假设读者已经有一定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'

demo地址在 https://github.com/hanliuxin5/dagger2-demo

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

1、在appcomponent中安装androidinjectionmodule

@component(modules = {androidinjectionmodule.class})
public interface appcomponent {
 //....
}

2.编写实现了androidinjector<youractivity>的lychee3activity

@subcomponent(modules = ...)
public interface actsubcomponent extends androidinjector<lychee3activity> {
 @subcomponent.builder
 public abstract class builder extends androidinjector.builder<lychee3activity> {
 }
}

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<activity> dispatchingandroidinjector;

 @override
 public void oncreate() {
  super.oncreate();
    daggerappcomponent.builder().appcontent(this).build().inject(this);//最好结合demo来看,不然appcontent是啥你不知道
 }

 @override
 public androidinjector<activity> 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);
 }
}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

参考文章