Dagger 2, sometimes on compiling I get "cannot find symbol class DaggerApplicationComponent"

Solution 1:

It's seems that it have something to do with incremental compilation added in Gradle 2.10

I managed to fix it adding the following command to gradle:

-Pandroid.incrementalJavaCompile=false

You can add it in Android Studio in: File | Settings | Build, Execution, Deployment | Compiler adding it as a Command line option.

edit as of 2.0.0-beta3 the plugin gives a warning telling you that this option has been added to the Gradle DSL:

android {
    compileOptions.incremental = false
}

Solution 2:

Changes in 2017:

Android Studio Canary uses a newer version of Gradle and apt plugins may not work, replaced by annotationProcessor. It may fail despite the compiler warning saying that it will be removed in a future version of gradle.

Change this dependency line:

apt 'com.google.dagger:dagger-compiler:2.7'

to

annotationProcessor 'com.google.dagger:dagger-compiler:2.7'

and remove the apt plugin.

Solution 3:

You need to update your version 2.11 for dagger.

Your build.gradle's dependencies block should looks like following.

dependencies {
    // Other dependencies should go here
    compile "com.google.dagger:dagger:2.11"
    annotationProcessor "com.google.dagger:dagger-compiler:2.11"
    provided 'javax.annotation:jsr250-api:1.0'
    compile 'javax.inject:javax.inject:1'
}

Hope this helps.

Solution 4:

I was using a pure Java Library module, but was using the kotlin plugin and the dagger dependencies, like this:

build.gradle

apply plugin: 'kotlin'
dependencies {
    implementation "com.google.dagger:dagger:2.22.1"
    kapt "com.google.dagger:dagger-compiler:2.22.1"
}

The error was, I missed to add the kotlin-kapt plugin. So, my build.gradle file ended up like this:

apply plugin: 'kotlin'
apply plugin: "kotlin-kapt" // make sure you added this line

dependencies {
    implementation "com.google.dagger:dagger:2.22.1"
    kapt "com.google.dagger:dagger-compiler:2.22.1"
}