简体   繁体   English

Dagger 2与Android数据绑定冲突

[英]Dagger 2 and Android Data Binding conflict

When adding both Dagger 2 and Android Data Binding to my project I get some the below build time errors. Dagger 2Android数据绑定添加到我的项目时,我得到一些下面的构建时错误。 It seems that this is a known problem (see [1] or [2] ) as I get the exact same errors. 这似乎是一个已知的问题(见[1][2] ),因为我得到完全相同的错误。 Unfortunately I was not able to solve them (like some others). 不幸的是我无法解决它们(就像其他人一样)。 Does someone have a full working setup with current versions of Dagger 2 and Data Binding, and can help? 有人使用当前版本的Dagger 2和Data Binding进行完整的工作设置,并且可以提供帮助吗?

Here the errors: 这里的错误:

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
import com.comparilla.wearcompass.databinding.FragmentInfoPanelBinding;
                                             ^
C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\ui\navigation\InfoPanelViewModel.java:8: error: cannot find symbol
import com.comparilla.wearcompass.BR;
                                 ^
  symbol:   class BR
  location: package com.comparilla.wearcompass
C:\Users\Kai\AndroidStudioProjects\WearCompass\mobile\src\main\java\com\comparilla\wearcompass\MobileApplication.java:7: error: cannot find symbol
import com.comparilla.wearcompass.di.components.DaggerMobileApplicationComponent;
                                               ^
  symbol:   class DaggerMobileApplicationComponent
  location: package com.comparilla.wearcompass.di.components
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.
    void inject(InfoPanelFragment fragment);
         ^
      com.comparilla.wearcompass.common.services.HeadingService is injected at
          com.comparilla.wearcompass.di.modules.ActivityModule.provideInfoPanelViewModel(headingService, …)
      com.comparilla.wearcompass.ui.navigation.InfoPanelViewModel is injected at
          com.comparilla.wearcompass.ui.navigation.InfoPanelFragment.mViewModel
      com.comparilla.wearcompass.ui.navigation.InfoPanelFragment is injected at
          com.comparilla.wearcompass.di.components.MobileActivityComponent.inject(fragment)
4 errors

 FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':mobile:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.

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

BUILD FAILED

My project build.gradle: 我的项目build.gradle:

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:2.1.2'
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

And the application build.gradle: 应用程序build.gradle:

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

android {
    compileSdkVersion 24
    buildToolsVersion "23.0.3"

    dataBinding {
        enabled = true
    }
    defaultConfig {
        applicationId "com.comparilla.wearcompass"
        minSdkVersion 21
        targetSdkVersion 24
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    wearApp project(':wear')
    testCompile 'junit:junit:4.12'

    // to enable BuildConfig.DEBUG in the common library
    // see https://stackoverflow.com/a/29163361/166229
    releaseCompile project(path: ':common', configuration: 'release')
    debugCompile project(path: ':common', configuration: 'debug')

    compile 'com.android.support:appcompat-v7:24.0.0'
    compile 'com.google.android.gms:play-services-maps:9.0.2'
    compile 'com.android.support:design:24.0.0'
    compile 'com.android.support:preference-v14:24.0.0'

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

I also tried to provided instead of apt in apt 'com.google.dagger:dagger-compiler:2.5' , without success. 我也尝试在apt 'com.google.dagger:dagger-compiler:2.5' provided而不是apt ,但没有成功。 Also commenting out apply plugin: 'com.neenbedankt.android-apt' did not help (like suggested in the provided resources). 同时注释掉apply plugin: 'com.neenbedankt.android-apt'没有帮助(就像提供的资源中所建议的那样)。

I do have Dagger 2 and DataBinding configured in the same project and it's working without any problem. 我确实在同一个项目中配置了Dagger 2和DataBinding,它没有任何问题。

You do have error in Dagger 2 configuration. 您在Dagger 2配置中确实有错误。 The HeadingService you're trying to inject can't be created because you haven't provided it with @Provides annotation and also the class doesn't have @Inject annotation on constructor. 您尝试注入的HeadingService无法创建,因为您没有为它提供@Provides注释,并且该类在构造函数上没有@Inject注释。

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

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