Skip to content

Instantly share code, notes, and snippets.

@lenguyenthanh
Last active May 7, 2020 19:55
Show Gist options
  • Save lenguyenthanh/13242759dfd056f01961 to your computer and use it in GitHub Desktop.
Save lenguyenthanh/13242759dfd056f01961 to your computer and use it in GitHub Desktop.
Dagger 2 configuration

Small gist shows 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.4'
apt 'com.google.dagger:dagger-compiler:2.4'
}
// 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);
}
}
@JonathanMonga
Copy link

Leave a comment

@esial
Copy link

esial commented Jul 14, 2017

It would be great if you also add testing configuration

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment