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

21

73

Recent after update Android Studio (2.0.7) (maybe this is the cause) sometimes when building i get that error.

Idea is that usually compilation goes well but sometimes I get dagger error.

Is possible that is problem in Dagger configuration?

Error itself:

Executing tasks: [:app:assembleDebug]

Configuration on demand is an incubating feature. Incremental java compilation is an incubating feature. :app:preBuild UP-TO-DATE :app:preDebugBuild UP-TO-DATE :app:checkDebugManifest :app:preReleaseBuild UP-TO-DATE :app:prepareComAndroidSupportAppcompatV72311Library UP-TO-DATE :app:prepareComAndroidSupportDesign2311Library UP-TO-DATE :app:prepareComAndroidSupportMultidex101Library UP-TO-DATE :app:prepareComAndroidSupportRecyclerviewV72311Library UP-TO-DATE :app:prepareComAndroidSupportSupportV42311Library UP-TO-DATE :app:prepareComDaimajiaSwipelayoutLibrary120Library UP-TO-DATE :app:prepareComF2prateekRxPreferencesRxPreferences101Library UP-TO-DATE :app:prepareComGithubAakiraExpandableLayout141Library UP-TO-DATE :app:prepareComGithubAfollestadMaterialDialogsCore0842Library UP-TO-DATE :app:prepareComGithubCastorflexSmoothprogressbarLibraryCircular120Library UP-TO-DATE :app:prepareComJakewhartonRxbindingRxbinding030Library UP-TO-DATE :app:prepareComPnikosisMaterialishProgress17Library UP-TO-DATE :app:prepareComTrelloRxlifecycle040Library UP-TO-DATE :app:prepareComTrelloRxlifecycleComponents040Library UP-TO-DATE :app:prepareComWdullaerMaterialdatetimepicker211Library UP-TO-DATE :app:prepareIoReactivexRxandroid110Library UP-TO-DATE :app:prepareMeRelexCircleindicator116Library UP-TO-DATE :app:prepareMeZhanghaiAndroidMaterialprogressbarLibrary114Library UP-TO-DATE :app:prepareDebugDependencies :app:compileDebugAidl UP-TO-DATE :app:compileDebugRenderscript UP-TO-DATE :app:generateDebugBuildConfig UP-TO-DATE :app:generateDebugAssets UP-TO-DATE :app:mergeDebugAssets UP-TO-DATE :app:generateDebugResValues UP-TO-DATE :app:generateDebugResources UP-TO-DATE :app:mergeDebugResources UP-TO-DATE :app:processDebugManifest UP-TO-DATE :app:processDebugResources UP-TO-DATE :app:generateDebugSources UP-TO-DATE :app:compileDebugJavaWithJavac /home/ungvas/AndroidDev/Projects/FW/paynet-android/app/src/main/java/md/fusionworks/paynet/ui/activity/BaseActivity.java:23: error: cannot find symbol import md.fusionworks.paynet.di.component.DaggerActivityComponent; ^ symbol: class DaggerActivityComponent location: package md.fusionworks.paynet.di.component /home/ungvas/AndroidDev/Projects/FW/paynet-android/app/src/main/java/md/fusionworks/paynet/PaynetApplication.java:7: error: cannot find symbol import md.fusionworks.paynet.di.component.DaggerApplicationComponent; ^ symbol: class DaggerApplicationComponent location: package md.fusionworks.paynet.di.component 2 errors

Incremental compilation of 66 classes completed in 3.719 secs. :app:compileDebugJavaWithJavac FAILED :app:compileRetrolambdaDebug

FAILURE: Build failed with an exception.

  • What went wrong: Execution failed for task ':app:compileDebugJavaWithJavac'.

    Compilation failed; see the compiler error output for details.

  • Try: Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 19.556 secs
Thanks.

Matthaeus answered 25/1, 2016 at 10:14 Comment(10)
You should provide the whole error log, since most of the times this is accompanied by more errors generated by dagger, like not providable classes or circular dependencies, ...Auspicious
are you using gradle 2.10 ?Nonchalant
what gradle version are you using?Nonchalant
Sorry yes gradle 2.10 ))Matthaeus
i have the same with 2.10, 2.8 works ok but with 2.8 i cannot use "instant run" with the latest android studio's gradle plugin (i think it has something to do with incremental compilation)Nonchalant
Thanks!Hmm... interesting guys from gradle work on this issue?Matthaeus
i have no green idea...Nonchalant
I'm having the same problem: Gradle 2.10 Android plugin 2.0.0-alpha7Bindweed
anyone able to fix this? I'm trying to run github.com/chiuki/android-test-demo and it's giving me the same errorStaffer
still bumped in this problem in 2024...Sizzle
B
34

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
}
Bindweed answered 28/1, 2016 at 11:37 Comment(3)
Hi, this is equivalent with dexOptions{incremental=false}?Matthaeus
Incremental dex and incremental build are different. Incremental dex applies to the process of building the dex file, which is a stage further down the compile process from compiling the Java code. The compiled Java .class files from the first stage are then further compiled into .dex files, as described here. And there's a bit more info in this SO question.Mclane
Sync doesn't resolve symbol DaggerApplicationComponent but when I try to run. Gradle generate it automatically.Descendant
C
7

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"
}
Confiscatory answered 10/4, 2019 at 16:16 Comment(2)
I always come back to this answer. Thank you!Bowling
The 'java' plugin has been applied, but it is not compatible with the Android plugins.Bittersweet
S
6

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.

Salvo answered 27/9, 2017 at 8:41 Comment(2)
@sud007, can you please clean and rebuild the project?Salvo
Yes buddy! I have already tried most of the solutions mentioned here. But as I understand from a couple of mentioned replies here, something else may be breaking the flow for Dagger annotation build. Will post here once resolved.Komi
Y
5

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.

Yahoo answered 31/8, 2017 at 4:25 Comment(0)
J
4

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'

Jessjessa answered 28/1, 2017 at 12:46 Comment(1)
It was hard to believe since it has previously worked with 2.8, but changing to 2.7 did actually work for me.Forbid
K
4

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'
}
Kalagher answered 30/1, 2019 at 9:58 Comment(2)
Adding in the two dependencies in the middle fixed this issue for me. I’m using Kotlin and this was the only fix. Thanks.Epizoic
I take it back, if you’re using Kotlin (as I am), you need to use the kapt declaration instead of the annotationProcessor declarations. After changing that keyword the Dagger2 code would generate and not break an hour later.Epizoic
F
3

File->InvalidateCaches/Restart worked for me

Frank answered 29/11, 2017 at 15:35 Comment(2)
happened to me once and I resolved it by invalidate/restart + rebuild; now it happenede for 2nd time and I found a bug in my XYZModule java file, so maybe check modules for errors, there might be someChifley
@arenaq: Exactly same as mine! What was that bug? when I add some lines to the my app module this error happen and when I remove that line (@Bind MyViewModel.class ...) it compiles without any error but after that produce Cannot create an instance of ViewModel class errorBittersweet
E
2

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

Escudo answered 22/2, 2017 at 8:28 Comment(1)
In my case, I had refactored the name of the package that held the class I was trying to use, but the DaggerXYZ import did not update.Aeriell
B
1

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.

Bethel answered 13/11, 2016 at 22:40 Comment(0)
S
1

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

Salvo answered 26/9, 2017 at 11:53 Comment(0)
R
1

You probably trying to Inject a class that is not Provided. A common scenario is when you Provide an Interface and trying to inject the concrete class.

For example:

@Provides
@Singleton
IConnection getIConnection(){ return new Connection(); }

And trying to Inject it as follows:

@Inject
Connection mConnection;
Runnymede answered 7/9, 2020 at 7:40 Comment(0)
S
1

If you have multimodule android application, then it is important that each .gradle file has kapt dependencies:

kapt "com.google.dagger:dagger-compiler:${dagger_version}"
kapt "com.google.dagger:dagger-android-processor:${dagger_version}"
Satyriasis answered 28/12, 2021 at 12:31 Comment(0)
P
0

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.

Polenta answered 28/7, 2017 at 20:23 Comment(0)
S
0

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.

Shaquitashara answered 24/12, 2017 at 23:14 Comment(1)
No such thing happens!Leuco
C
0

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);
}
Capias answered 7/8, 2018 at 8:54 Comment(0)
M
0

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.

Medicable answered 12/10, 2018 at 7:35 Comment(0)
L
0

Check this if you are migrating to Dagger Hilt

I was getting a similar error when trying to migrate my app from dagger.android to Dagger Hilt. Looks like the issue was an incompatibility between the library versions. What I ended up doing was to first update my current dagger.android setup to the latest release (2.28), before starting to add and configure Dagger Hilt dependencies.

Lemmons answered 4/8, 2020 at 21:52 Comment(0)
B
0

Non of the solutions worked for me. Invalid cache restart not helped also. I've resolved this issue by following these steps:

Make sure that in your ViewModel class you have used @Inject keyword before constructor. i.e.

public class MyViewModel extends ViewModel {

...

@Inject // <--- ***
public MyViewModel ( ... ){
       ...
       }
...
}
  1. Make sure that you have created @Bind for your MyViewModel in your viewModelModul class (if you have).

By following these errors will gone!

Bittersweet answered 17/2, 2023 at 14:13 Comment(0)
F
-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'
Flea answered 7/6, 2016 at 13:57 Comment(2)
I have these lines but still get the same error message :(Sporangium
android-apt is deprecated and should not be used anymoreCyna
T
-1

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.

Toulouselautrec answered 20/4, 2019 at 6:51 Comment(0)
S
-2

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

Starknaked answered 27/2, 2019 at 9:22 Comment(1)
Anything that breaks compilation, e.g. problems in butterknife, dagger, databinding can show up other components that are also not generated because compilation broke, it's just common sense. You see it missing DaggerApplicationComponent because that's the first compiled/generated class it happened to look forBelovo

© 2022 - 2024 — McMap. All rights reserved.