Can not resolve symbol DaggerApplicationComponent
Asked Answered
F

4

11

I use Dagger2 with java and I got "Can not resolve symbol DaggerApplicationComponent error in my application." Seems there is something wrong with dependencies. Any help would be really appreciated. My complete code is here- https://github.com/rohitku860/AndroidMvpDagger2

Here is my app graddle with dependencies:

 apply plugin: 'com.android.application'
    android {
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.example.android.androidmvpdagger2"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
       }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

    dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso- 
    core:3.0.1'
    implementation 'com.google.dagger:dagger:2.13'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.13'
    implementation 'com.jakewharton:butterknife:8.5.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.5.1'

}

and here is project one:

        buildscript {

        repositories {
            google()
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:3.1.0'
        }
    }

        allprojects {
        repositories {
            google()
            jcenter()
        }
    }



           task clean(type: Delete) {
            delete rootProject.buildDir
        }
Fagan answered 1/4, 2018 at 20:3 Comment(1)
mvp pattern is not related to dagger, you can use dagger without using mvp and vice versa. You error is about your code rather then in your android dependenciesQuincuncial
S
12

This error is not related to your Gradle configuration.

DaggerApplicationComponent is a class that Dagger generates for ApplicationComponent interface that you defined. If there is any error during code generation (e.g. missing @Provides method), Dagger will not generate DaggerApplicationComponent and you'll get this error.

What you need to do is to read the entire error output in AndroidStudio and try to understand why Dagger failed.

I also encountered a very nasty behavior when some import statements were missing in project files. In these cases Dagger will fail, but will not tell you exactly what the problem is and you'll need to look for it by yourself.

If you need further help - attach the build error output to the question.

Sleeper answered 2/4, 2018 at 3:46 Comment(3)
I am a beginner so I don't have more idea about MVP and Dagger2 When I type DaggerApplicationComponent it shows this error. This is a small application code is here-github.com/rohitku860/AndroidMvpDagger2Fagan
@RohitUpadhyay, everything looks alright except for this code: Void inject(MainActivity target);. Try to change Void to void (lower case).Sleeper
I encountered the mentioned "nasty behaviour". I missed an @Provides function in my module for a field I @Injected. Dagger won't tell you there is something missing, it will just refuse to generate the code...Bagpipes
I
4

There is a simple solution for this, just rebuild the project and it might work

Inorganic answered 22/3, 2019 at 17:21 Comment(2)
Why this isnt the accepted answer - I wont know. Thanks though (Y)Orsola
@HaiderMalik Coz for more people this won't work.Auntie
B
3

The proper setup of your Dagger solves this issue.

Update (March 29, 2020)

Inside your app-level build.gradle inside dependencies block, add these lines:

     //dagger2
     api 'com.google.dagger:dagger:2.24'
     api 'com.google.dagger:dagger-android:2.24'
     api 'com.google.dagger:dagger-android-support:2.24'

     annotationProcessor 'com.google.dagger:dagger-compiler:2.24'
     kapt 'com.google.dagger:dagger-compiler:2.24'

     annotationProcessor 'com.google.dagger:dagger-android-processor:2.24'
     kapt 'com.google.dagger:dagger-android-processor:2.24'

     compileOnly 'javax.annotation:jsr250-api:1.0'
     implementation 'javax.inject:javax.inject:1'

Inside android block of app-level build.gradle,

kapt {
        generateStubs = true
    }

At the top of the app-level build.gradle, Do this in exactly below order.

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'

Finally, You need to configure Annotation Process as provided in the screenshot below. You can do this File>Other Settings>Settings for New Projects>search"Annotation processor"

After this, do from Menu Build > Rebuild. You are done!

Test:

@Component
public interface ApplicationComponent {

}

Now, you can use DaggerApplicationComponent that was generated at compile-time for your ApplicationComponent interface.

public class MyApplication extends Application {

    ApplicationComponent applicationComponent = DaggerApplicationComponent.create();


}
Blackguard answered 30/3, 2020 at 3:20 Comment(3)
This is proper solution for those using kotlin. Thanks!!Fluctuant
You reference a screenshot in your answer but there is none. Also in Android Studio there's no Other Settings as far as I can see?Scamp
Ah Other settings is under settingsScamp
P
0

I had this problem and none of the other answers helped me. The only errors I had was what the OP is asking about. The project uses Maven and so I had Maven reload the project and viola the problem went away.

Prediction answered 29/3 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.