Skip to content

Commit cf9d1bc

Browse files
author
David Weiser
committed
updates Dagger code example
1 parent e7847d2 commit cf9d1bc

File tree

11 files changed

+86
-90
lines changed

11 files changed

+86
-90
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
apply plugin: 'com.android.application'
2-
apply plugin: 'com.neenbedankt.android-apt'
32

43
android {
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

2322
dependencies {
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
}

com.vogella.android.dagger2simple/app/src/main/java/com/vogella/android/dagger2simple/MainActivity.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,20 @@
66

77
import javax.inject.Inject;
88

9+
import dagger.android.AndroidInjection;
10+
911
public 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+
}
Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,26 @@
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+
}

com.vogella.android.dagger2simple/app/src/main/java/com/vogella/android/dagger2simple/NetworkApi.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
package com.vogella.android.dagger2simple;
22

3+
import javax.inject.Inject;
4+
35
public 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

com.vogella.android.dagger2simple/app/src/main/java/com/vogella/android/dagger2simple/components/DiComponent.java

Lines changed: 0 additions & 19 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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 numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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+
}

com.vogella.android.dagger2simple/app/src/main/java/com/vogella/android/dagger2simple/modules/NetworkApiModule.java

Lines changed: 0 additions & 17 deletions
This file was deleted.

com.vogella.android.dagger2simple/build.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff 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
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Feb 22 22:03:40 CET 2017
1+
#Tue Apr 11 11:59:12 CEST 2017
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
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

0 commit comments

Comments
 (0)