Necronet Necronet - 1 year ago 180
Android Question

How to configure dagger + gradle

I have a project and migrating to gradle dependency, but I find myself with an issue trying to setup dagger with gradle, the first time I compile it work perfectly (or if I clean) but if I try it twice then it gives me error like:

Error:(13, 14) error: duplicate class:$InjectingApplicationModule$$ModuleAdapter

I try using android-apt plugin and configured as in the documentation but I still get the same error (

I also try using provided dependency instead like in this tutorial ( of compile but no luck so far.

Do you have any ideas how to configure dagger and gradle?


My build.gradle looks like this

apply plugin: 'android'
apply plugin: 'android-apt'

android {
compileSdkVersion 19
buildToolsVersion "19.0.2"

defaultConfig {
minSdkVersion 9
targetSdkVersion 19
packageName ""


buildTypes {
release {
runProguard false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'

dependencies {
compile project(':volley')
apt 'com.squareup.dagger:dagger-compiler:1.2.0'
compile 'com.squareup.dagger:dagger:1.2.0'

And my top level build.gradle look like this

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
dependencies {
classpath ''
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'

allprojects {
repositories {


I tried with provided again as @Marco suggested no luck, I don't know if there is a library or a version of gradle that could be causing this problem, I'm currently using 1.10. On the bright side I did find a way to make it work, but I would love to do it by just adding the provided statement. The way I did it is the old way:

Define apt configuration

configurations {

add Dagger compiler lib

apt 'com.squareup.dagger:dagger-compiler:1.2.0'

And implement the this hook to applicationVariant which as far as I know android-apt does something similar. Does this make sense? why?

def getSourceSetName(variant) {
return new File(variant.dirName).getName();

android.applicationVariants.each { variant ->
def aptOutputDir = project.file("build/source/apt")
def aptOutput = new File(aptOutputDir, variant.dirName)

android.sourceSets[getSourceSetName(variant)].java.srcDirs+= aptOutput.getPath()

variant.javaCompile.options.compilerArgs += [
'-processorpath', configurations.apt.getAsPath(),
'-s', aptOutput

variant.javaCompile.source = variant.javaCompile.source.filter { p ->
return !p.getPath().startsWith(aptOutputDir.getPath())

variant.javaCompile.doFirst {

Answer Source

I am using dagger in this sample Volley Examples. I'm not experiencing any problems with dagger and I'm including the compiler using:

provided 'com.squareup.dagger:dagger-compiler:1.2.1'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download