Dagger 2与Android数据绑定冲突

前端之家收集整理的这篇文章主要介绍了Dagger 2与Android数据绑定冲突前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
Dagger 2Android Data Binding添加到我的项目时,我得到一些下面的构建时错误.这似乎是一个已知问题(见 [1][2]),因为我得到完全相同的错误.不幸的是我无法解决它们(就像其他人一样).有人对当前版本的Dagger 2和数据绑定有完整的工作设置,可以提供帮助吗?

这里的错误

  1. C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\ui\navigation\InfoPanelFragment.java:12: error: package com.comparilla.wearcompass.databinding does not exist
  2. import com.comparilla.wearcompass.databinding.FragmentInfoPanelBinding;
  3. ^
  4. C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\ui\navigation\InfoPanelviewmodel.java:8: error: cannot find symbol
  5. import com.comparilla.wearcompass.BR;
  6. ^
  7. symbol: class BR
  8. location: package com.comparilla.wearcompass
  9. C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\MobileApplication.java:7: error: cannot find symbol
  10. import com.comparilla.wearcompass.di.components.DaggerMobileApplicationComponent;
  11. ^
  12. symbol: class DaggerMobileApplicationComponent
  13. location: package com.comparilla.wearcompass.di.components
  14. C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\di\components\MobileActivityComponent.java:15: error: com.comparilla.wearcompass.common.services.HeadingService cannot be provided without an @Inject constructor or from an @Provides-annotated method.
  15. void inject(InfoPanelFragment fragment);
  16. ^
  17. com.comparilla.wearcompass.common.services.HeadingService is injected at
  18. com.comparilla.wearcompass.di.modules.ActivityModule.provideInfoPanelviewmodel(headingService,…)
  19. com.comparilla.wearcompass.ui.navigation.InfoPanelviewmodel is injected at
  20. com.comparilla.wearcompass.ui.navigation.InfoPanelFragment.mviewmodel
  21. com.comparilla.wearcompass.ui.navigation.InfoPanelFragment is injected at
  22. com.comparilla.wearcompass.di.components.MobileActivityComponent.inject(fragment)
  23. 4 errors
  24.  
  25. Failed
  26.  
  27. FAILURE: Build Failed with an exception.
  28.  
  29. * What went wrong:
  30. Execution Failed for task ':mobile:compileDebugJavaWithJavac'.
  31. > Compilation Failed; see the compiler error output for details.
  32.  
  33. * Try:
  34. Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
  35.  
  36. BUILD Failed

我的项目build.gradle:

  1. // Top-level build file where you can add configuration options common to all sub-projects/modules.
  2.  
  3. buildscript {
  4. repositories {
  5. jcenter()
  6. }
  7. dependencies {
  8. classpath 'com.android.tools.build:gradle:2.1.2'
  9. classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  10.  
  11. // NOTE: Do not place your application dependencies here; they belong
  12. // in the individual module build.gradle files
  13. }
  14. }
  15.  
  16. allprojects {
  17. repositories {
  18. jcenter()
  19. }
  20. }
  21.  
  22. task clean(type: Delete) {
  23. delete rootProject.buildDir
  24. }

应用程序build.gradle:

  1. apply plugin: 'com.android.application'
  2. apply plugin: 'com.neenbedankt.android-apt'
  3.  
  4. android {
  5. compileSdkVersion 24
  6. buildToolsVersion "23.0.3"
  7.  
  8. dataBinding {
  9. enabled = true
  10. }
  11. defaultConfig {
  12. applicationId "com.comparilla.wearcompass"
  13. minSdkVersion 21
  14. targetSdkVersion 24
  15. versionCode 1
  16. versionName "1.0"
  17. }
  18. buildTypes {
  19. release {
  20. minifyEnabled false
  21. proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
  22. }
  23. }
  24. }
  25.  
  26. dependencies {
  27. compile fileTree(include: ['*.jar'],dir: 'libs')
  28. wearApp project(':wear')
  29. testCompile 'junit:junit:4.12'
  30.  
  31. // to enable BuildConfig.DEBUG in the common library
  32. // see https://stackoverflow.com/a/29163361/166229
  33. releaseCompile project(path: ':common',configuration: 'release')
  34. debugCompile project(path: ':common',configuration: 'debug')
  35.  
  36. compile 'com.android.support:appcompat-v7:24.0.0'
  37. compile 'com.google.android.gms:play-services-maps:9.0.2'
  38. compile 'com.android.support:design:24.0.0'
  39. compile 'com.android.support:preference-v14:24.0.0'
  40.  
  41. compile 'com.google.dagger:dagger:2.5'
  42. apt 'com.google.dagger:dagger-compiler:2.5'
  43. provided 'javax.annotation:jsr250-api:1.0'
  44. }

我也尝试在apt’com.google.dagger:dagger-compiler:2.5’中提供而不是apt,但没有成功.同时注释掉apply plugin:’com.neenbedankt.android-apt’没有帮助(就像提供的资源中所建议的那样).

解决方法

我确实在同一个项目中配置了Dagger 2和DataBinding,它没有任何问题.

您在Dagger 2配置中确实有错误.您尝试注入的HeadingService无法创建,因为您没有为它提供@Provides注释,并且该类在构造函数上没有@Inject注释.

猜你在找的Android相关文章