Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha
Forked from lenguyenthanh/ Dagger 2.md
Created February 29, 2016 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save digitalbuddha/cafafaec15bc1bf315c9 to your computer and use it in GitHub Desktop.
Save digitalbuddha/cafafaec15bc1bf315c9 to your computer and use it in GitHub Desktop.
Dagger 2 configuration

Small gist show how to config Dagger 2 to an Android project

// top level build.gradle
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // apt for manage code generation
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
// app build.gradle
apply plugin: 'com.android.application'
// Use to manage code generation
apply plugin: 'com.neenbedankt.android-apt'
android {
compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
applicationId "com.lenguyenthanh.nimbledagger2"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile 'com.android.support:appcompat-v7:23.2.0'
compile 'com.android.support:design:23.2.0'
//Dagger 2 dependency
compile 'com.google.dagger:dagger:2.0.2'
apt 'com.google.dagger:dagger-compiler:2.0.2'
provided 'org.glassfish:javax.annotation:10.0-b28'
}
// An example module, this module only provides one dependency - DaggerApplication object
@Module
public class AppModule {
DaggerApplication mApplication;
public AppModule(DaggerApplication application) {
mApplication = application;
}
@Provides
@Singleton
DaggerApplication providesApplication() {
return mApplication;
}
}
// Example Application object, need to declare it in manifest.xml
public class DaggerApplication extends Application{
protected AppComponent appComponent;
// provide appComponent to use in other classes
public AppComponent getAppComponent() {
return appComponent;
}
public static DaggerApplication get(Context context) {
return (DaggerApplication) context.getApplicationContext();
}
@Override
public void onCreate() {
super.onCreate();
initializeDaggerComponent();
}
// build an instance of AppComponent, we need to keep this instance for another sub components in project
protected void initializeDaggerComponent() {
// DaggerDaggerApplication_AppComponent is a generated class. It only appears after first success build.
appComponent = DaggerDaggerApplication_AppComponent.builder()
.appModule(new AppModule(this)) // add AppModule instance to the component
.build();
appComponent.inject(this); // inject dependencies to this class
}
// AppComponent component interface - root component of the project
@Singleton
@Component(modules = AppModule.class)
public interface AppComponent {
DaggerApplication application();
void inject(DaggerApplication app);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment