How to configure dagger + gradle

Necronet picture Necronet · Apr 10, 2014 · Viewed 21.8k times · Source

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: com.myapp.android.application.InjectingApplication$InjectingApplicationModule$$ModuleAdapter

I try using android-apt plugin and configured as in the documentation but I still get the same error (https://bitbucket.org/hvisser/android-apt/overview)

I also try using provided dependency instead like in this tutorial (https://github.com/frankdu/android-gradle-dagger-tutorial) of compile but no luck so far.

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

EDIT

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 "com.myapp.android"

    }

    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 {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.9.+'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.2'
    }
}

allprojects {
    repositories {
        mavenCentral()
    }
}

EDIT#2:

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 {
     apt
}

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 {
        aptOutput.mkdirs()
    }
}

Answer

Marco RS picture Marco RS · Apr 10, 2014

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'