Skip to content

Commit eb7d8b6

Browse files
committed
Adds Dagger 2 example
1 parent 01b6645 commit eb7d8b6

30 files changed

Lines changed: 557 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
/.idea/libraries
5+
.DS_Store
6+
/build
7+
/captures
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.neenbedankt.android-apt'
3+
4+
android {
5+
compileSdkVersion 23
6+
buildToolsVersion "23.0.0"
7+
8+
defaultConfig {
9+
applicationId "com.vogella.android.dagger2simple"
10+
minSdkVersion 22
11+
targetSdkVersion 23
12+
versionCode 1
13+
versionName "1.0"
14+
}
15+
buildTypes {
16+
release {
17+
minifyEnabled false
18+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
19+
}
20+
}
21+
}
22+
23+
dependencies {
24+
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'
30+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /home/vogella/Android/Sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.vogella.android.dagger2simple;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.dagger2simple" >
4+
5+
<application
6+
android:name=".MyApplication"
7+
android:allowBackup="true"
8+
android:icon="@mipmap/ic_launcher"
9+
android:label="@string/app_name"
10+
android:theme="@style/AppTheme" >
11+
<activity
12+
android:name=".MainActivity"
13+
android:label="@string/app_name" >
14+
<intent-filter>
15+
<action android:name="android.intent.action.MAIN" />
16+
17+
<category android:name="android.intent.category.LAUNCHER" />
18+
</intent-filter>
19+
</activity>
20+
</application>
21+
22+
</manifest>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.vogella.android.dagger2simple;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
import android.widget.TextView;
8+
import android.widget.Toast;
9+
10+
import javax.inject.Inject;
11+
12+
public class MainActivity extends Activity {
13+
14+
@Inject NetworkApi networkApi;
15+
16+
@Override
17+
protected void onCreate(Bundle savedInstanceState) {
18+
super.onCreate(savedInstanceState);
19+
setContentView(R.layout.activity_main);
20+
21+
((MyApplication) getApplication()).getComponent().inject(this);
22+
23+
boolean injected = networkApi == null ? false : true;
24+
TextView userAvailable = (TextView) findViewById(R.id.target);
25+
userAvailable.setText("Dependency injection worked: " + String.valueOf(injected));
26+
}
27+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.vogella.android.dagger2simple;
2+
3+
import android.app.Application;
4+
5+
import com.vogella.android.dagger2simple.components.DaggerDiComponent;
6+
import com.vogella.android.dagger2simple.components.DiComponent;
7+
8+
public class MyApplication extends Application {
9+
DiComponent component;
10+
11+
@Override
12+
public void onCreate() {
13+
super.onCreate();
14+
component = DaggerDiComponent.builder().build();
15+
}
16+
17+
public DiComponent getComponent() {
18+
return component;
19+
}
20+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.vogella.android.dagger2simple;
2+
3+
public class NetworkApi {
4+
5+
public boolean validateUser(String username, String password) {
6+
// imagine an actual network call here
7+
// for demo purpose return false in "real" life
8+
if (username == null || username.length() == 0) {
9+
return false;
10+
} else {
11+
return true;
12+
}
13+
}
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.vogella.android.dagger2simple.components;
2+
3+
import android.app.Activity;
4+
5+
import com.vogella.android.dagger2simple.MainActivity;
6+
import com.vogella.android.dagger2simple.NetworkApi;
7+
import com.vogella.android.dagger2simple.modules.NetworkApiModule;
8+
9+
import javax.inject.Singleton;
10+
11+
import dagger.Component;
12+
13+
@Singleton
14+
@Component(modules = {NetworkApiModule.class})
15+
16+
public interface DiComponent {
17+
// to update the fields in your activities
18+
void inject(MainActivity activity);
19+
}

0 commit comments

Comments
 (0)