android 组件化dagger2,Android模块化之ButterKnife和Dagger2的使用

通常来说,我们引入一个第三方库,只需要在OpenSourceLibrary中的gradle文件中添加一个依赖即可,这样任何一个业务module都可以使用该库的功能。但是,有一种库例外,这种库会利用用apt或annotationProcessor生成代码。这种库使用大量使用注解,然后在编译期间生成代码,能够为开发者省很多功夫,比如大名鼎鼎的ButterKnife和Dagger2,涉及到这种库时,在用到的每一个module中都要有相应的配置。这里我们介绍下在模块化工程中如何使用(自己琢磨的,有更好的方式欢迎告知)。

ButterKnife的使用

引入依赖

由于butterknife之前是不支持library中使用的,后来更新后利用插件生成了R2文件,支持library使用。对于我们的module来说,既可以是一个Application,也可以是一个library,在Application时用的R文件,在library又必须使用R2文件,这样就比较坑爹。为了统一,我们在所有的module以及App模块中都使用R2文件。

首先这样做:在OpenSourceLibrary的gradle文件中添加(这个依赖是每个module都要用的,统一放在OpenSourceLibrary中)

compile 'com.jakewharton:butterknife:8.6.0'

然后这样做:(注意:以下步骤需要在所有使用Butterknife的模块中进行)

引入butterknife为library准备的插件,该module的gradle中添加:

apply plugin: 'com.jakewharton.butterknife‘

同时添加编译时的依赖:

buildscript {

repositories {

mavenCentral()

}

dependencies {

com.jakewharton:butterknife-gradle-plugin:8.6.0

com.android.tools.build:gradle:2.3.1

}

}

最后在dependencies中添加:

dependencies {

annotationProcessor 'com.jakewharton:butterknife-compiler:8.6.0'

}

butterknife注入的封装

通常来说,我们使用butterknife都会在基类中注入好,然后子类中就不用注入,而是直接使用。在这里,有一个问题,我们的BaseActivity是放在第二层的基础业务层的,示例代码中放在CommonBusiness模块中,而它的子类是放在我们的业务包中的,这样没有问题吗?实践证明,没有问题:

public abstract class BaseActivity extends AppCompatActivity {

private Unbinder unbinder;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

}

@Override

public void setContentView(@LayoutRes int layoutResID) {

super.setContentView(layoutResID);

unbinder = ButterKnife.bind(this);

}

@Override

protected void onDestroy() {

super.onDestroy();

unbinder.unbind();

}

}

通过以上两个步骤,butterknife就可以正常使用了。

Dagger2的使用

Dagger2的依赖引入

和butterknife一样,首先要在OpensourceLibrary中添加一个所有模块都会用到的依赖:

compile 'com.google.dagger:dagger:2.x'

然后,在每一个要使用Dagger2的模块的gradle文件中添加如下语句:

dependencies {

annotationProcessor 'com.google.dagger:dagger-compiler:2.x'

}

这样在每个模块中就可以使用了

使用dagger2

使用dagger2分为两种情况,一种是通过dagger2注入本模块的对象,以CommonBusiness模块中的HouseDetail为例,

首先编写Module文件

@Module

public class CommonBusinessModule {

@Provides

public HouseDetail provideHouseDetail(){

return new HouseDetail();

}

}

然后编写Component文件

@Component(modules = CommonBusinessModule.class)

public interface CommonBusinessComponent {

void inject(LoginActivity loginActivity);

}

最后在LoginActivity中注入使用

@Inject

HouseDetail houseDetail;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_login);

targetUrl = getCurModuleUrl();

if(targetUrl == null){

targetUrl = ConstantRouter.APP_MAINACTIVITY;

}

DaggerCommonBusinessComponent.builder().build().inject(this);

Log.d("longyi",houseDetail.toString());

//此处用来模拟登录事件

progressBar.postDelayed(new Runnable() {

@Override

public void run() {

DataCenter.getInstance().setLogin(true);

ARouter.getInstance().build(targetUrl).navigation();

LoginActivity.this.finish();

}

},2000);

}

另外一种情况就是在业务层的module中通过dagger2注入基础业务层中的对象,比如在InstantMessagingModule中注入CommonBusiness中的HouseDetail对象。

首先直接编写Component文件

@Component(modules = { CommonBusinessModule.class})

public interface ImComponenment {

void inject(InstantMessagingMainActivity activity);

}

这里的module依赖直接指定为CommonBusiness中的module文件就可以了。

另外还有一种方式,看代码

@Module

public class ImModule extends CommonBusinessModule{

@Provides

public SecondHouse provideSecond(){

return new SecondHouse();

}

}

@Component(modules = {ImModule.class })

public interface ImComponenment {

void inject(InstantMessagingMainActivity activity);

}

这里是编写了自己的自己模块的module文件,然后继承了CommonBusinessModule,这样在Component中直接指定ImModule.class为依赖既可以了。

总结

涉及到有编译期间动态生成代码的,最起码,每个模块中都要有annotationProcessor配置(或apt)。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值