【问题标题】:Unresolved reference DaggerApplicationComponent未解决的参考 DaggerApplicationComponent
【发布时间】:2016-12-14 11:12:22
【问题描述】:

我正在尝试创建我的应用组件,但 Dagger 没有生成我的应用组件。 这是 MyApplication

class MyApplication : Application() {

companion object {
    @JvmStatic lateinit var graph: ApplicationComponent
}
@Inject
lateinit var locationManager : LocationManager

override fun onCreate() {
    super.onCreate()
    graph = DaggerApplicationComponent.builder().appModule(AppModule(this)).build()
    graph.inject(this)
  }
}

这是我的 AppComponent

@Singleton
@Component(modules = arrayOf(AppModule::class))
interface ApplicationComponent {
    fun inject(application: MyApplication)
}

这是截图

这是我在 github

上的 project

这是错误日志

Error:(7, 48) Unresolved reference: DaggerApplicationComponent
Error:(28, 17) Unresolved reference: DaggerApplicationComponent
Error:Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
Information:BUILD FAILED
Information:Total time: 21.184 secs
Error:e: .../MyApplication.kt: (7, 48): Unresolved reference: DaggerApplicationComponent
e: Unresolved reference: DaggerApplicationComponent
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

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

Information:4 errors
Information:0 warnings
Information:See complete output in console

【问题讨论】:

  • 当您尝试构建应用程序时它会说什么?
  • 我已经更新了我的问题
  • @user5543258 你的问题解决了吗?我也有同样的问题。
  • 这里没有工作,你找到解决方案了吗?

标签: android kotlin dagger-2


【解决方案1】:

我的解决方案是添加

apply plugin: 'kotlin-kapt'

并删除

kapt {
    generateStubs = true
}

【讨论】:

  • 这救了我的命。
  • 严重的是我被困在最后一天,很遗憾没有搜索。这对我帮助很大。
【解决方案2】:

这帮助我解决了这个问题

将此添加到build.gradle 的顶部

apply plugin: 'kotlin-kapt'

android内添加标签

kapt {
    generateStubs = true
}

然后替换

annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

kapt 'com.google.dagger:dagger-compiler:2.11'

现在Rebuild该项目由

Build -> Rebuild project

【讨论】:

  • 我认为你应该删除generateStubs = true
【解决方案3】:

请尝试启用stubs generation,这可能是该类在构建过程的此阶段不可见的原因。在您的 build.gradle 文件中,顶级:

kapt {
    generateStubs = true
}

【讨论】:

  • Java版本怎么样??
【解决方案4】:

我已经下载了你的 Github 项目。感谢分享!

您的问题的答案很简单:

Build -> Rebuild project

Dagger 依赖文件将被重新创建,并且应用程序在启动后会出现任何问题。

我已经用 Android Studio 2.1.2 版本检查了这一点。有效

【讨论】:

    【解决方案5】:

    你应该删除

    kapt {
    generateStubs = true}
    

    并添加到应用程序 gradle 文件的顶部

    apply plugin: 'kotlin-kapt'
    

    那么 Dagger 会处理剩下的事情 :)

    【讨论】:

      【解决方案6】:

      在我的情况下缺少 kapt 编译器依赖项。请确保也具有以下依赖项。

      kapt 'com.google.dagger:dagger-compiler:2.15'
      

      应用构建.gradle

      apply plugin: 'com.android.application'
      
      apply plugin: 'kotlin-android'
      
      apply plugin: 'kotlin-kapt'
      
      apply plugin: 'kotlin-android-extensions'
      
      android {
          compileSdkVersion 27
          defaultConfig {
              applicationId "com.usb.utility"
              minSdkVersion 14
              targetSdkVersion 27
              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 "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
          implementation 'com.android.support:appcompat-v7:27.1.0'
          implementation 'com.android.support.constraint:constraint-layout:1.0.2'
          compile 'com.google.dagger:dagger-android:2.15'
          compile 'com.google.dagger:dagger-android-support:2.15' // if you use the support libraries
          kapt 'com.google.dagger:dagger-android-processor:2.15'
          kapt 'com.google.dagger:dagger-compiler:2.15'
          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'
      }
      

      【讨论】:

        【解决方案7】:

        在 build.gradlle 上添加这个

        apply plugin: 'kotlin-kapt'
        

        在依赖项中添加这个

        kapt 'com.google.dagger:dagger-compiler:2.15'
        kapt 'com.google.dagger:dagger-android-processor:2.15'
        

        之后重建您的项目。我希望它会奏效。 谢谢

        【讨论】:

          【解决方案8】:

          同样对于 Java 用户,解决方案只是重建您的项目。

          【讨论】:

          • 这是完美的解决方案
          【解决方案9】:

          混合 Java/Kotlin 项目只需要两个步骤:

          1. apply plugin: 'kotlin-kapt' 添加到构建文件的顶部
          2. annotationProcessor 'com.google.dagger:dagger-compiler:2.14.1' 替换为kapt 'com.google.dagger:dagger-compiler:2.14.1'

          【讨论】:

            【解决方案10】:

            答案很简单:

            Build -> Rebuild project

            它对我有用。

            【讨论】:

              【解决方案11】:

              请尝试将此添加到您的build.gradle

              android {
                  dexOptions {
                      incremental false
                  }
              

              编辑:如果您不应用 kotlin-kapt 插件,显然一年后可能会发生这种情况。还要确保使用kapt而不是annotationProcessor

              【讨论】:

              【解决方案12】:

              就我而言,我在所有答案中都做了所有事情,但没有解决它!

              我的解决方案:转到您项目的目录路径并删除

              buildapp>build.idea.Gradle

              然后回到 android studio 并 rebuild 项目并完成!

              【讨论】:

                【解决方案13】:

                在新版本的hilt中,需要使用SingletonComponent::class而不是ApplicationComponent::class

                【讨论】:

                  【解决方案14】:

                  这一定能解决你的问题:

                  更新(2020 年 3 月 29 日)

                  在您的应用级 build.gradle dependencies 块内,添加以下行:

                       //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'
                  

                  应用级android块内build.gradle

                  kapt {
                          generateStubs = true
                      }
                  

                  应用级build.gradle顶部,按完全低于顺序执行此操作。

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

                  最后,您需要按照以下屏幕截图中的说明配置注释过程。你可以这样做File>Other Settings>Settings for New Projects>search"Annotation processor"

                  之后,从菜单Build > Rebuild 执行。你完成了!

                  测试:

                  @Component
                  public interface ApplicationComponent {
                  
                  }
                  

                  现在,您可以使用在编译时为您的 ApplicationComponent 接口生成的DaggerApplicationComponent

                  public class MyApplication extends Application {
                  
                      ApplicationComponent applicationComponent = DaggerApplicationComponent.create();
                  
                  
                  }
                  

                  【讨论】:

                    猜你喜欢
                    • 2021-10-25
                    • 1970-01-01
                    • 1970-01-01
                    • 2019-04-05
                    • 1970-01-01
                    • 2021-08-17
                    • 2020-05-27
                    • 2019-11-16
                    • 2021-01-24
                    相关资源
                    最近更新 更多