简体   繁体   English

匕首2:无法解析匕首组件的符号

[英]Dagger 2 : Cannot resolve symbol for dagger component

I would like to exercise this Dagger 2 Vehicle Motor example. 我想以这个Dagger 2 Vehicle Motor为例。

I made everything exact like in that tutorial, except for my gradel.build: 除了我的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 错误:找不到符号变量Dagger_VehicleComponent

Whats wrong there? 怎么了 (Same without '_' underscore) (相同,没有下划线“ _”)

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'
  1. Change Dagger_VehicleComponent with DaggerVehicleComponent 使用DaggerVehicleComponent更改Dagger_VehicleComponent
  2. Clean and rebuild your project 清理并重建您的项目
  3. Import DaggerVehicleComponent class 导入DaggerVehicleComponent类

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? 阅读其他答案,我发现DaggerApplicationComponent类是由编译器自动生成的,然后我想,为什么IDE无法“找到”该类? 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) 1)打开终端并转到您的项目路径(或直接在Android Studio中打开终端)

2) Clean project ./gradlew clean 2)清理项目./gradlew clean

3) Build the project ./gradlew build 3)建立项目./gradlew建立

NOTE: If by this point you have noticed the project is not compiling... Bingo! 注意:如果此时您已经注意到该项目尚未编译... 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 4)打开gradle并添加buildToolsVersion '23 .0.1',并且必须启用非常重要的multiDexEnabled true这就是我在app模块中的gradle的样子

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. 之后,您将能够导入DaggerApplicationComponent。

Hope this saves time for someone else! 希望这可以节省其他人的时间!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM