android - Dagger2 multi-module design -


for dagger2 release , plan split module few small module re-use on other projects.

application module contains many things, can group 3 type. type related, type b related, type c related.

so want put 3 different module , therefore can re-use part of if need on other projects.

reference google's fork

build.gradle application

buildscript { repositories {     jcenter() } dependencies {     classpath 'com.android.tools.build:gradle:1.1.0'     classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'      // note: not place application dependencies here; belong     // in individual module build.gradle files     } } allprojects {     repositories {         mavencentral()     } } 

build.gradle app module

apply plugin: 'com.neenbedankt.android-apt' //below lines go 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 , going create application module

@dagger.module public class myapplicationmodule {   void inject(myapplication application); } 

and component

@singleton @component(         modules = {typea.class, typeb.class, typec.class}) public interface myapplicationcomponent { 

when use on activity , looks

@component(dependencies = myapplicationcomponent.class,         modules = activitymodule.class) public interface activitycomponent {      void injectactivity(splashscreenactivity activity); 

there compile issue

error:(22, 10) error: xxxxxx cannot provided without @provides- or @produces-annotated method. com.myapplication.mxxx [injected field of type: xxx]

i guess there wrong when config activity component extends application component.

my purpose singleton object inside application component , activity inject same object reduce object create every time on activity. design wrong?? or other things need config??

find out root cause come @scope

need expose type other sub-component usage

@scope @retention(retentionpolicy.runtime) public @interface activityscope { } 

if want expose context application ,

@singleton @component(          {typea.class, typeb.class, typec.class}) public interface myapplicationcomponent {      void inject(myapplication application);      @forapplication     context appcontext(); 

when sub-component want extend

@activityscope @component(dependencies = myapplicationcomponent.class,         modules = activitymodule.class) public interface activitycomponent extends myapplicationcomponent {      void injectactivity(activity activity); 

i think great thing dagger2 , let manually expose object need use , code become more traceable.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -