简体   繁体   English

Dagger2多模块设计

[英]Dagger2 multi-module design

For Dagger2 release , i plan to split the module into few small module for re-use on other projects. 对于Dagger2版本,我计划将模块拆分为几个小模块,以便在其他项目上重用。

Application Module contains many things, i can group it into three type. 应用程序模块包含许多内容,我可以将其分为三种类型。 Type A related, Type B related, Type C related. 类型A相关,类型B相关,类型C相关。

so i want to put it into three different module , therefore it can re-use part of it if need on other projects. 所以我想将其放入三个不同的模块中,因此如果需要在其他项目上可以重用它的一部分。

Reference from the Google's Fork Google的Fork参考

build.gradle for Application build.gradle申请

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.1.0'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
    }
}
allprojects {
    repositories {
        mavenCentral()
    }
}

build.gradle for app module 应用模块的build.gradle

apply plugin: 'com.neenbedankt.android-apt'
//below lines go to dependenc
compile 'com.google.dagger:dagger:2.0'
apt 'com.google.dagger:dagger-compiler:2.0'
provided 'org.glassfish:javax.annotation:10.0-b28'

after above steps , i am going to create my application module 经过上述步骤,我将创建我的应用程序模块

@dagger.Module
public class MyApplicationModule {
  void inject(MyApplication application);
}

and my component 和我的组件

@Singleton
@Component(
        modules = {TypeA.class, TypeB.class, TypeC.class})
public interface MyApplicationComponent {

When i use it on activity , it looks like 当我在活动上使用它时,它看起来像

@Component(dependencies = MyApplicationComponent.class,
        modules = ActivityModule.class)
public interface ActivityComponent {

    void injectActivity(SplashScreenActivity activity);

There are compile issue 有编译问题

Error:(22, 10) error: XXXXXX cannot be provided without an @Provides- or @Produces-annotated method. 错误:(22,10)错误:如果没有@Provides或@Produces注释的方法,则无法提供XXXXXX。 com.myapplication.mXXX [injected field of type: XXX] com.myapplication.mXXX [类型为XXX的插入字段]

i guess there are something wrong when i config that Activity Component extends from application component. 我猜当我配置活动组件从应用程序组件扩展时出了点问题。

my purpose is some singleton object inside Application Component , activity will inject same object to reduce some object create every time on activity. 我的目的是在应用程序组件内部添加一些单例对象,活动将注入同一对象以减少每次活动时创建的某些对象。 is my design wrong?? 我的设计错了吗? or any other things need to do for config?? 或配置需要做的任何其他事情?

find out the root cause is come from @Scope 找出根本原因来自@Scope

need to expose the type for other sub-component usage 需要公开类型以供其他子组件使用

@Scope
@Retention(RetentionPolicy.RUNTIME)
public @interface ActivityScope {
}

if we want to expose the Context for application , 如果我们要公开应用程序的上下文,

@Singleton
@Component(
         {TypeA.class, TypeB.class, TypeC.class})
public interface MyApplicationComponent {

    void inject(MyApplication application);

    @ForApplication
    Context appContext();

when my sub-component want to extend this 当我的子组件想要扩展此

@ActivityScope
@Component(dependencies = MyApplicationComponent.class,
        modules = ActivityModule.class)
public interface ActivityComponent extends MyApplicationComponent {

    void injectActivity(Activity activity);

i think it is a great thing for Dagger2 , let you manually expose the object you need to use , code become more traceable. 我认为这对Dagger2来说是一件好事,让您手动显示需要使用的对象,代码可追溯性更高。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM