Skip to content

Commit f99a46c

Browse files
committed
Adds Dagger 2 exmaple
1 parent 67c381f commit f99a46c

31 files changed

Lines changed: 586 additions & 0 deletions
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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'com.neenbedankt.android-apt'
3+
4+
android {
5+
compileSdkVersion 22
6+
buildToolsVersion "23.0.0 rc2"
7+
8+
defaultConfig {
9+
applicationId "com.vogella.android.dagger2"
10+
minSdkVersion 22
11+
targetSdkVersion 22
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(include: ['*.jar'], dir: 'libs')
25+
compile "com.google.dagger:dagger:2.0"
26+
apt "com.google.dagger:dagger-compiler:2.0"
27+
provided 'org.glassfish:javax.annotation:10.0-b28'
28+
}
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.dagger2;
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: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.vogella.android.dagger2" >
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:theme="@style/AppTheme" >
10+
<activity
11+
android:name=".MainActivity"
12+
android:label="@string/app_name" >
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.vogella.android.dagger2;
2+
3+
import javax.inject.Singleton;
4+
5+
import dagger.Module;
6+
import dagger.Provides;
7+
8+
@Module
9+
public class AppModule {
10+
private final ImportClass instance;
11+
12+
public AppModule(ImportClass instance) {
13+
this.instance = instance;
14+
}
15+
16+
@Provides
17+
@Singleton
18+
ImportClass providesImportClass() {
19+
return new ImportClass();
20+
}
21+
22+
@Provides
23+
@Singleton
24+
TestClass1 providesTestClass1() {
25+
return new TestClass1();
26+
}
27+
28+
@Provides
29+
@Singleton
30+
TestClass2 providesTestClass2() {
31+
return new TestClass2();
32+
}
33+
34+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.vogella.android.dagger2;
2+
3+
import javax.inject.Inject;
4+
5+
/**
6+
* Created by vogella on 21.07.15.
7+
*/
8+
public class ImportClass {
9+
@Inject
10+
public TestClass1 test1;
11+
12+
@Inject
13+
public TestClass1 test2;
14+
15+
16+
17+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.vogella.android.dagger2;
2+
3+
import javax.inject.Singleton;
4+
5+
import dagger.Component;
6+
7+
@Singleton
8+
@Component(
9+
modules = {
10+
AppModule.class,
11+
}
12+
)
13+
public interface ImportClassComponent {
14+
15+
void inject (MainActivity activity);
16+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.vogella.android.dagger2;
2+
3+
import android.app.Activity;
4+
import android.os.Bundle;
5+
import android.view.Menu;
6+
import android.view.MenuItem;
7+
8+
import javax.inject.Inject;
9+
10+
public class MainActivity extends Activity {
11+
12+
@Inject
13+
ImportClass myClass;
14+
15+
@Override
16+
protected void onCreate(Bundle savedInstanceState) {
17+
super.onCreate(savedInstanceState);
18+
setContentView(R.layout.activity_main);
19+
MyApplication application = (MyApplication) getApplication();
20+
application.build.inject(this);
21+
myClass.test1.sayHello();
22+
myClass.test2.sayHello();
23+
24+
25+
}
26+
27+
@Override
28+
public boolean onCreateOptionsMenu(Menu menu) {
29+
// Inflate the menu; this adds items to the action bar if it is present.
30+
getMenuInflater().inflate(R.menu.menu_main, menu);
31+
return true;
32+
}
33+
34+
@Override
35+
public boolean onOptionsItemSelected(MenuItem item) {
36+
// Handle action bar item clicks here. The action bar will
37+
// automatically handle clicks on the Home/Up button, so long
38+
// as you specify a parent activity in AndroidManifest.xml.
39+
int id = item.getItemId();
40+
41+
//noinspection SimplifiableIfStatement
42+
if (id == R.id.action_settings) {
43+
return true;
44+
}
45+
46+
return super.onOptionsItemSelected(item);
47+
}
48+
}

0 commit comments

Comments
 (0)