File tree Expand file tree Collapse file tree 11 files changed +86
-90
lines changed
com.vogella.android.dagger2simple
src/main/java/com/vogella/android/dagger2simple Expand file tree Collapse file tree 11 files changed +86
-90
lines changed Original file line number Diff line number Diff line change 1
1
apply plugin : ' com.android.application'
2
- apply plugin : ' com.neenbedankt.android-apt'
3
2
4
3
android {
5
4
compileSdkVersion 25
6
- buildToolsVersion " 23 .0.3 "
5
+ buildToolsVersion " 25 .0.2 "
7
6
8
7
defaultConfig {
9
8
applicationId " com.vogella.android.dagger2simple"
10
9
minSdkVersion 22
11
- targetSdkVersion 23
10
+ targetSdkVersion 25
12
11
versionCode 1
13
12
versionName " 1.0"
14
13
}
@@ -22,9 +21,9 @@ android {
22
21
23
22
dependencies {
24
23
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"
30
29
}
Original file line number Diff line number Diff line change 6
6
7
7
import javax .inject .Inject ;
8
8
9
+ import dagger .android .AndroidInjection ;
10
+
9
11
public class MainActivity extends Activity {
10
12
11
13
@ Inject NetworkApi networkApi ;
12
14
13
15
@ Override
14
16
protected void onCreate (Bundle savedInstanceState ) {
17
+ AndroidInjection .inject (this );
15
18
super .onCreate (savedInstanceState );
16
19
setContentView (R .layout .activity_main );
17
20
18
- ((MyApplication ) getApplication ()).getComponent ().inject (this );
19
-
20
21
boolean injected = networkApi == null ? false : true ;
21
22
TextView userAvailable = (TextView ) findViewById (R .id .target );
22
23
userAvailable .setText ("Dependency injection worked: " + String .valueOf (injected ));
23
24
}
24
- }
25
+ }
Original file line number Diff line number Diff line change
1
+ package com .vogella .android .dagger2simple ;
1
2
3
+ import android .app .Activity ;
4
+ import android .app .Application ;
2
5
6
+ import com .vogella .android .dagger2simple .components .DaggerIApplicationComponent ;
3
7
8
+ import javax .inject .Inject ;
4
9
10
+ import dagger .android .DispatchingAndroidInjector ;
11
+ import dagger .android .HasDispatchingActivityInjector ;
5
12
13
+ public class MyApplication extends Application implements HasDispatchingActivityInjector {
14
+ @ Inject DispatchingAndroidInjector <Activity > dispatchingAndroidInjector ;
6
15
16
+ @ Override
17
+ public void onCreate () {
18
+ super .onCreate ();
19
+ DaggerIApplicationComponent .create ().inject (this );
20
+ }
7
21
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 1
1
package com .vogella .android .dagger2simple ;
2
2
3
+ import javax .inject .Inject ;
4
+
3
5
public class NetworkApi {
4
6
7
+ @ Inject
8
+ public NetworkApi () {
9
+
10
+ }
11
+
5
12
public boolean validateUser (String username , String password ) {
6
13
// imagine an actual network call here
7
14
// 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 {
5
5
jcenter()
6
6
}
7
7
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'
10
9
11
10
// NOTE: Do not place your application dependencies here; they belong
12
11
// in the individual module build.gradle files
Original file line number Diff line number Diff line change 1
- # Wed Feb 22 22:03:40 CET 2017
1
+ # Tue Apr 11 11:59:12 CEST 2017
2
2
distributionBase =GRADLE_USER_HOME
3
3
distributionPath =wrapper/dists
4
4
zipStoreBase =GRADLE_USER_HOME
5
5
zipStorePath =wrapper/dists
6
- distributionUrl =https\://services.gradle.org/distributions/gradle-2.14.1 -all.zip
6
+ distributionUrl =https\://services.gradle.org/distributions/gradle-3.3 -all.zip
You can’t perform that action at this time.
0 commit comments