dagger-2 Getting started with dagger-2 Description and Setup


Example

What is Dagger 2 ?

The website describes itself as:

Dagger is a fully static, compile-time dependency injection framework

The library makes it easy to model dependency graphs as well as to reuse objects. Since reflection is only used at compile time as part of the annotation processing Dagger 2 has improved speed for dependency injection.

Setup

1- Add support for annotation processing:

Android

Top level build.gradle script:

 repositories {
    mavenCentral()
  }
  dependencies {
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  }

Module level build.gradle script:

apply plugin: 'com.neenbedankt.android-apt'

Java

plugins {
  id "net.ltgt.apt" version "0.5"
}    

2- Add the dagger 2 dependencies

 dependencies {
      compile 'com.google.dagger:dagger:2.x'
      apt 'com.google.dagger:dagger-compiler:2.x'
    }