File tree Expand file tree Collapse file tree
com.vogella.android.dagger2simple
src/main/java/com/vogella/android/dagger2simple Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11apply plugin : ' com.android.application'
2- apply plugin : ' com.neenbedankt.android-apt'
32
43android {
54 compileSdkVersion 25
6- buildToolsVersion " 23 .0.3 "
5+ buildToolsVersion " 25 .0.2 "
76
87 defaultConfig {
98 applicationId " com.vogella.android.dagger2simple"
109 minSdkVersion 22
11- targetSdkVersion 23
10+ targetSdkVersion 25
1211 versionCode 1
1312 versionName " 1.0"
1413 }
@@ -22,9 +21,9 @@ android {
2221
2322dependencies {
2423 compile fileTree(dir : ' libs' , include : [' *.jar' ])
25- compile ' com.google.dagger:dagger:2.0 '
26- apt ' com.google.dagger:dagger-compiler :2.0 '
27- provided ' javax.annotation:jsr250-api:1.0 '
28- compile ' javax.inject:javax.inject:1 '
29- testCompile ' junit:junit:4.12'
24+ compile ' com.google.dagger:dagger:2.10 '
25+ compile ' com.google.dagger:dagger-android :2.10 '
26+ annotationProcessor ' com.google.dagger:dagger-android-processor:2.10 '
27+ annotationProcessor ' com.google.dagger:dagger-compiler:2.10 '
28+ testCompile " junit:junit:4.12"
3029}
Original file line number Diff line number Diff line change 66
77import javax .inject .Inject ;
88
9+ import dagger .android .AndroidInjection ;
10+
911public class MainActivity extends Activity {
1012
1113 @ Inject NetworkApi networkApi ;
1214
1315 @ Override
1416 protected void onCreate (Bundle savedInstanceState ) {
17+ AndroidInjection .inject (this );
1518 super .onCreate (savedInstanceState );
1619 setContentView (R .layout .activity_main );
1720
18- ((MyApplication ) getApplication ()).getComponent ().inject (this );
19-
2021 boolean injected = networkApi == null ? false : true ;
2122 TextView userAvailable = (TextView ) findViewById (R .id .target );
2223 userAvailable .setText ("Dependency injection worked: " + String .valueOf (injected ));
2324 }
24- }
25+ }
Original file line number Diff line number Diff line change 1+ package com .vogella .android .dagger2simple ;
12
3+ import android .app .Activity ;
4+ import android .app .Application ;
25
6+ import com .vogella .android .dagger2simple .components .DaggerIApplicationComponent ;
37
8+ import javax .inject .Inject ;
49
10+ import dagger .android .DispatchingAndroidInjector ;
11+ import dagger .android .HasDispatchingActivityInjector ;
512
13+ public class MyApplication extends Application implements HasDispatchingActivityInjector {
14+ @ Inject DispatchingAndroidInjector <Activity > dispatchingAndroidInjector ;
615
16+ @ Override
17+ public void onCreate () {
18+ super .onCreate ();
19+ DaggerIApplicationComponent .create ().inject (this );
20+ }
721
8-
9-
10-
11-
12-
13-
14-
15-
16-
17-
18-
19-
20-
21-
22-
23-
24-
25-
26- package com .vogella .android .dagger2simple ;
27-
28- import android .app .Application ;
29-
30- import com .vogella .android .dagger2simple .components .DaggerDiComponent ;
31- import com .vogella .android .dagger2simple .components .DiComponent ;
32-
33- public class MyApplication extends Application {
34- DiComponent component ;
35-
36- @ Override
37- public void onCreate () {
38- super .onCreate ();
39-
40- component = DaggerDiComponent .builder ().build ();
41- }
42-
43- public DiComponent getComponent () {
44- return component ;
45- }
46- }
22+ @ Override
23+ public DispatchingAndroidInjector <Activity > activityInjector () {
24+ return dispatchingAndroidInjector ;
25+ }
26+ }
Original file line number Diff line number Diff line change 11package com .vogella .android .dagger2simple ;
22
3+ import javax .inject .Inject ;
4+
35public class NetworkApi {
46
7+ @ Inject
8+ public NetworkApi () {
9+
10+ }
11+
512 public boolean validateUser (String username , String password ) {
613 // imagine an actual network call here
714 // for demo purpose return false in "real" life
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ package com .vogella .android .dagger2simple .components ;
2+
3+ import com .vogella .android .dagger2simple .MyApplication ;
4+ import com .vogella .android .dagger2simple .modules .ActivityModule ;
5+
6+ import dagger .Component ;
7+ import dagger .android .AndroidInjectionModule ;
8+
9+ @ Component (modules = {ActivityModule .class , AndroidInjectionModule .class })
10+ public interface IApplicationComponent {
11+ void inject (MyApplication application );
12+ }
Original file line number Diff line number Diff line change 1+ package com .vogella .android .dagger2simple .components ;
2+
3+ import com .vogella .android .dagger2simple .MainActivity ;
4+
5+ import dagger .Subcomponent ;
6+ import dagger .android .AndroidInjector ;
7+
8+ @ Subcomponent
9+ public interface IMainActivitySubcomponent extends AndroidInjector <MainActivity > {
10+
11+ @ Subcomponent .Builder
12+ abstract class Builder extends AndroidInjector .Builder <MainActivity > {}
13+ }
Original file line number Diff line number Diff line change 1+ package com .vogella .android .dagger2simple .modules ;
2+
3+ import android .app .Activity ;
4+
5+ import com .vogella .android .dagger2simple .MainActivity ;
6+ import com .vogella .android .dagger2simple .components .IMainActivitySubcomponent ;
7+
8+ import dagger .Binds ;
9+ import dagger .Module ;
10+ import dagger .android .ActivityKey ;
11+ import dagger .android .AndroidInjector ;
12+ import dagger .multibindings .IntoMap ;
13+
14+ @ Module (subcomponents = {IMainActivitySubcomponent .class })
15+ public abstract class ActivityModule {
16+
17+ @ Binds
18+ @ IntoMap
19+ @ ActivityKey (MainActivity .class )
20+ abstract AndroidInjector .Factory <? extends Activity > bindYourActivityInjectorFactory (IMainActivitySubcomponent .Builder builder );
21+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -5,8 +5,7 @@ buildscript {
55 jcenter()
66 }
77 dependencies {
8- classpath ' com.android.tools.build:gradle:2.2.3'
9- classpath ' com.neenbedankt.gradle.plugins:android-apt:1.7'
8+ classpath ' com.android.tools.build:gradle:2.3.1'
109
1110 // NOTE: Do not place your application dependencies here; they belong
1211 // in the individual module build.gradle files
You can’t perform that action at this time.
0 commit comments