简体   繁体   中英

Dagger2 multi-module design

For Dagger2 release , i plan to split the module into few small module for re-use on other projects.

Application Module contains many things, i can group it into three type. Type A related, Type B related, Type C related.

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

build.gradle for Application

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

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. com.myapplication.mXXX [injected field of type: 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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