Dagger 2 : Cannot resolve symbol for dagger component

有些话、适合烂在心里 提交于 2019-12-10 13:43:34

问题


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)


回答1:


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'



回答2:


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



回答3:


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!



来源:https://stackoverflow.com/questions/37527597/dagger-2-cannot-resolve-symbol-for-dagger-component

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