Ralf Wickum
Ralf Wickum

Reputation: 3270

Dagger 2 : Cannot resolve symbol for dagger component

I would like to exercise this Dagger 2 Vehicle Motor example.

I made everything exact like in that tutorial, except for my gradel.build:

compile 'com.google.dagger:dagger:2.4'
apt 'com.google.dagger:dagger-compiler:2.4'
compile 'javax.annotation:javax.annotation-api:1.2'

but then I get an

error: cannot find symbol variable Dagger_VehicleComponent

Whats wrong there? (Same without '_' underscore)

Upvotes: 7

Views: 11742

Answers (3)

Mauricio Sartori
Mauricio Sartori

Reputation: 2621

Took me a while to figure this out. But I found the reason (At least on my side) I created a project from scratch, after that I was trying to set up the basic components/tools till I bump into this issue.

Reading other answers I found that DaggerApplicationComponent class is autogenerated by the compiler, then I thought, why the IDE can't "find" this class? The answer is obvious and... you got it, was because the project wasn't compiling, here are the steps to solve this issue if the scenario is the same as mine.

1) Open terminal and go to your projects path (Or simply open the terminal in the Android Studio)

2) Clean project ./gradlew clean

3) Build the project ./gradlew build

NOTE: If by this point you have noticed the project is not compiling... Bingo! That might be the real issue. Then follow the next steps:

4)Open gradle and add the buildToolsVersion '23.0.1' and very IMPORTANT has to be enabled multiDexEnabled true This is how my gradle in the app module looks like

android {
    buildToolsVersion '23.0.1' // IMPORTANT
    compileSdkVersion 26
    defaultConfig {
        applicationId "com.fixing.dagger"
        minSdkVersion 23
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
        multiDexEnabled true  // ALSO IMPORTANT
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

After that you will be able to import the DaggerApplicationComponent.

Hope this saves time for someone else!

Upvotes: 0

Fordyta Abubakar
Fordyta Abubakar

Reputation: 11

  1. Change Dagger_VehicleComponent with DaggerVehicleComponent
  2. Clean and rebuild your project
  3. Import DaggerVehicleComponent class

Upvotes: 0

Ralf Wickum
Ralf Wickum

Reputation: 3270

Another version solved it:

compile 'com.google.dagger:dagger:2.2'
apt 'com.google.dagger:dagger-compiler:2.2'
provided 'javax.annotation:jsr250-api:1.0'

Upvotes: 3

Related Questions