Dabler Dabler - 2 months ago 13
Android Question

Dagger2 dependency - Gradle

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

Android-Gradle

This answer is for installing Dagger2 on Android with Gradle.


Project build.gradle:

buildscript {
    dependencies {
        // Assists in working with annotation processors for Android Studio.
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

Application build.gradle:

// Assists in working with annotation processors for Android Studio.
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    // Dagger 2 and Compiler
    compile 'com.google.dagger:dagger:2.0.1'
    apt "com.google.dagger:dagger-compiler:2.0.1"

    // Needed for @Generated annotation (missing in Android API jar)
    // No longer needed in dagger >= 2.1-SNAPSHOT (github.com/google/dagger/issues/95)
    compile 'javax.annotation:jsr250-api:1.0'
}

Maven Repositories:

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


NOTE: The javax.annotation is needed for the @Generated annotation used in Dagger generated code. The annotation is not included in the Android API jar, so you'll need to use one of these libraries (see differences):

Update: @Generated will no longer be needed in dagger >= 2.1-SNAPSHOT (see github.com/google/dagger/issues/95).

Comments