Dagger2 dependency - Gradle

Dabler picture Dabler · Feb 22, 2015 · Viewed 36k times · Source

I'm trying to add Dagger2 to my project in Android Studio but I can't find proper dependency to paste in build.gradle. Could you help and send me the proper line?

Answer

bcorso picture bcorso · Jun 11, 2015

Installing Dagger 2 on Android Studio 2

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    annotationProcessor "com.google.dagger:dagger-compiler:2.4"
}

Maven Repositories:

Find the latest versions of the above dependencies in the Maven Repository:


Notes: Android Studio < 2.2

Older versions of Android Studio need android-apt for annotation processing.

// Project build.gradle
buildscript {
    dependencies {
        // Assists in working with annotation processors for Android Studio.
        // No longer needed with Android Studio 2.2+
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}
apply plugin: 'com.neenbedankt.android-apt'

And

// Application build.gradle
dependencies {
    compile 'com.google.dagger:dagger:2.4'
    apt "com.google.dagger:dagger-compiler:2.4"
}

Notes: Dagger < 2.1

For Dagger < 2.1-SNAPSHOT the javax.annotation is needed for the @Generated annotation used in Dagger generated code (see github.com/google/dagger/issues/95). The annotation is not included in the Android API jar, so you'll need to use one of these libraries (see differences):

// Application build.gradle
dependencies {
    compile 'javax.annotation:jsr250-api:1.0'
}