How to add Dagger 2 in build.gradle

suggest change

Since Android Gradle Plugin 2.2, the android-apt plugin is no longer needed. Use annotationProcessor, which that plugin version provides. For an older Android Gradle Plugin, use the android-apt setup shown below.

For Android Gradle Plugin 2.2 and newer

dependencies {
    annotationProcessor 'com.google.dagger:dagger-compiler:2.8'
    compile 'com.google.dagger:dagger:2.8'
    provided 'javax.annotation:jsr250-api:1.0'
}

For Android Gradle Plugin older than 2.2

To use Dagger 2 it’s necessary to add android-apt plugin, add this to the root build.gradle:

buildscript {
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.0'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

Then the application module’s build.gradle should contain:

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

android {
    …
}

final DAGGER_VERSION = '2.0.2'
dependencies {
    …

    compile "com.google.dagger:dagger:${DAGGER_VERSION}"
    apt "com.google.dagger:dagger-compiler:${DAGGER_VERSION}"
}

Reference: https://github.com/codepath/android_guides/wiki/Dependency-Injection-with-Dagger-2

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents