title: How to configure dagger + gradle
body: <p>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:</p>
  
  <p>Error:(13, 14) error: duplicate class: com.myapp.android.application.InjectingApplication$InjectingApplicationModule$$ModuleAdapter</p>
  
  <p>I try using android-apt plugin and configured as in the documentation but I still get the same error (<a href="https://bitbucket.org/hvisser/android-apt/overview">https://bitbucket.org/hvisser/android-apt/overview</a>)</p>
  
  <p>I also try using provided dependency instead like in this tutorial (<a href="https://github.com/frankdu/android-gradle-dagger-tutorial">https://github.com/frankdu/android-gradle-dagger-tutorial</a>) of compile but no luck so far.</p>
  
  <p>Do you have any ideas how to configure  dagger and gradle?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>My build.gradle looks like this</p>
  
  <pre><code>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'
  }
  </code></pre>
  
  <p>And my top level build.gradle look like this</p>
  
  <pre><code>// 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()
      }
  }
  </code></pre>
  
  <p>EDIT#2: </p>
  
  <p>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:</p>
  
  <p>Define apt configuration</p>
  
  <pre><code>configurations {
       apt
  }
  </code></pre>
  
  <p>add Dagger compiler lib</p>
  
  <pre><code>apt 'com.squareup.dagger:dagger-compiler:1.2.0'
  </code></pre>
  
  <p>And implement the this hook to applicationVariant which as far as I know android-apt does something similar. Does this make sense? why?</p>
  
  <pre><code>def getSourceSetName(variant) {
      return new File(variant.dirName).getName();
  }
  
  android.applicationVariants.each { variant -&gt;
      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 -&gt;
          return !p.getPath().startsWith(aptOutputDir.getPath())
      }
  
      variant.javaCompile.doFirst {
          aptOutput.mkdirs()
      }
  }
  </code></pre>