【问题标题】:Dagger build error匕首构建错误
【发布时间】:2018-04-20 14:19:18
【问题描述】:

我有两个对象,我用 dagger2 注入没有任何问题。当我添加第三个(与之前的两个相同)时,项目不会重建并给出错误,这指向前两个(这里没有添加第三个):

error: com.google.firebase.database.DatabaseReference cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.
com.google.firebase.database.DatabaseReference is injected at
com.hotmail.at.jablonskimichal.dni.wolne.od.handlu.ui.splash.SplashPresenter.freeDaysCloudReference
com.hotmail.at.jablonskimichal.dni.wolne.od.handlu.ui.splash.SplashPresenter is injected at
com.hotmail.at.jablonskimichal.dni.wolne.od.handlu.providers.components.MainPresenterComponent.inject(destination)

每三个模块和组件都以相同的方式实现。当然,当我删除添加的第三个时,项目很容易编译。

搞乱项目构建的模块:

@Module
public class MainPresenterModule {

    private final MainPresenter presenter;

    public MainPresenterModule(MainPresenter presenter) {
        this.presenter = presenter;
    }

    @Singleton
    @Provides
    MainPresenter provideMainPresenter() {
        return presenter;
    }

}

组件:

@Singleton
@Component(modules = {MainPresenterModule.class})
public interface MainPresenterComponent {
    void inject(SplashPresenter destination);
}

也许有用: 我将这些库用于带有 dagger2 的 DI:

    implementation 'com.google.dagger:dagger-android:2.11'
    implementation 'com.google.dagger:dagger-android-support:2.11'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

在 mac osx 上使用最新的 android studio (3.1.1) 和 gradle (3.1.1)。当然,我尝试过使缓存无效并重新启动、清理等。我检查了导入,所有这些都是相同的。

编辑: 当我删除使用注入时

    @Inject
    DatabaseReference shopsCloudReference;

    public MainPresenter(MainController controller) {
        super(controller);
        Core.injectShopsComponent().into(this);
    }

    @Inject
    DatabaseReference freeDaysCloudReference;

    public SplashPresenter(SplashController controller) {
        super(controller);
        Core.injectFreeDaysComponent().into(this);
    }

项目可以编译,但不是我想要的。

编辑 有很多答案我没有在 gradle 中添加 firebase 库。我的完整毕业典礼:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "com.hotmail.at.jablonskimichal.dni.wolne.od.handlu"
        minSdkVersion 21
        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'
        }
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
}

allprojects {
    repositories {
        mavenCentral()
        google()
        maven { url "https://jitpack.io" }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

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

    //support
    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.0'

    //images loading
    implementation 'com.github.bumptech.glide:glide:4.4.0'
    annotationProcessor 'com.github.bumptech.glide:compiler:4.4.0'

    //network
    implementation 'com.squareup.okhttp3:okhttp:3.9.0'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.9.0'
    implementation 'com.squareup.retrofit2:retrofit:2.2.0'

    //json parsing
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
    implementation 'com.google.code.gson:gson:2.8.0'

    //for view bindings
    implementation 'com.jakewharton:butterknife:8.8.1'
    annotationProcessor 'com.jakewharton:butterknife-compiler:8.8.1'

    //rxjava
    def rxVersion = '2.0.2'
    implementation "io.reactivex.rxjava2:rxandroid:$rxVersion"
    implementation "io.reactivex.rxjava2:rxjava:$rxVersion"
    implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'

    //dagger
    implementation 'com.google.dagger:dagger-android:2.11'
    implementation 'com.google.dagger:dagger-android-support:2.11'
    annotationProcessor 'com.google.dagger:dagger-android-processor:2.11'
    annotationProcessor 'com.google.dagger:dagger-compiler:2.11'

    //google
    implementation 'com.google.android.gms:play-services-maps:15.0.0'
    implementation 'com.google.firebase:firebase-database:15.0.0'
    implementation 'com.google.firebase:firebase-core:15.0.0'

    //datetime
    implementation group: 'joda-time', name: 'joda-time', version: '2.9.9'

    //transitions
    implementation "com.andkulikov:transitionseverywhere:1.7.9"

    //kotlin
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
}

apply plugin: 'com.google.gms.google-services'

【问题讨论】:

  • 为什么会在错误中显示 firebase?你在使用火力基地吗?它没有显示在您的依赖项中...
  • 另外,您要注入的第三个对象是什么?
  • 我使用了两个 firebase 数据库对象,并且在添加第三个 DI 对象(MainPresenter.class)之前效果很好。我以相同的方式实现它们中的每一个。
  • 更多信息请查看How to fix cannot be provided,您没有在任何地方提供DatabaseReference,因此Dagger 无法注入。

标签: java android dagger-2 dagger build-error


【解决方案1】:

嗯,你写的:

@Component(modules = {MainPresenterComponent.class})

应该是:

@Component(modules = {MainPresenterModule.class})

【讨论】:

  • 哦.. 我以前见过这个,但是复制了错误的文本。与适当班级有关的错误。我已经编辑了问题。对不起
  • 正如@Levi_Albuquerque 指出的那样,您正在尝试注入DatabaseReference,但它没有出现在您的依赖项中
  • 确实有,但我没提过。
  • 您可能应该“提及它”,因为它对于回答问题很重要。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多