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

三世轮回 提交于 2019-12-02 18:42:00

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
}

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.

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.

The latest version of Dagger (2.8) is causing this error. Make sure your dependencies are as mentioned below

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

I had a similar problem but for different reason.
I had the problem only when trying to generate the apk. Otherwise it was working correctly.
In my case the problem was that the class was in the test directory instead of the main directory for some unknown reason, I moved it to main and it worked.
Hope it helps someone

File->InvalidateCaches/Restart worked for me

Make sure you are using Java version 1.7. Also, if something else is broken in your dagger pipeline, it will cause this error as well.

In my case none of above works.

Follow steps to generate DaggerApplicationComponent class.

  1. Clean project
  2. Rebuild project
  3. Import manually by Option + Return OR Alter + Enter if you do not have Auto import on fly setting in Android studio

Done

use the same dagger version for all the dagger dependencies. worked for me.

implementation "com.google.dagger:dagger:$daggerVersion"
implementation "com.google.dagger:dagger-android-support:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-android-processor:$daggerVersion"
annotationProcessor "com.google.dagger:dagger-compiler:$daggerVersion"


//define version in main build.gradle
ext {
    daggerVersion = '2.11'
}

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"
}

First, add this line to the dependencies in the buildscript in the Project build.gradle file.

 classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

Second, add this line at the top in the Module: app build.gradle file

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

I had this issue but it was two failures during build. One was the missing component but the other was a ButterKnife @BindView couldn't find the view. Fixing this fixed the missing component. I assume any failed annotation processing will cause similar issues.

Try to run the application, the build will fail and you will get the option to import the classes then. This happens because dagger only imports these classes during runtime and not at compile time.

Mayuresh Deshmukh

In my case in presenter i didn't use @Inject annotation for overridden method of data manager and etc.

 @Inject
 public RegisterPresenter(DataManager dataManager, SchedulerProvider 
             schedulerProvider, CompositeDisposable compositeDisposable) {
                super(dataManager, schedulerProvider, compositeDisposable);
}

Double Check your annotations everywhere for prticular component and module. Mine problem was i was using @Named in module but not defining it in the constructor.

For the instance where I received this error, the reported error was off-the-mark in that an error with DaggerMyComponent was reported, but the root cause was that -- elsewhere in the code -- I had, mistakenly, used ButterKnife's @BindView with a private view-type member; fixing this eliminated the error.

That's very wired problem I face it! i don't have any idea why this related to Dagger!

I use ButterKnife for binding views but some how in coding (Rush copy/paste!) i wrote two different views with same id like below (both view with fab id)

@BindView(R.id.fab)
FloatingActionButton fab;
@BindView(R.id.fab)
Toolbar toolbar;

After trying to run app throw this error on build tab

Compilation failed; see the compiler error output for details.

And compiler error is

error: cannot find symbol class DaggerApplicationComponent

I know it seems ridiculous but it happen to me and after fix ids my problem solved.

Fixed Code

@BindView(R.id.fab)
FloatingActionButton fab;
@BindView(R.id.toolbar)
Toolbar toolbar;

Hope to help some one.

UPDATE

Once again it happens for me after a year and it same id in RecyclerView Adapter ids

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!