简体   繁体   中英

Dagger 2 inject() contains a dependency cycle

I have the following setup in Dagger that I'm trying to migrate to Dagger 2:

public class Origin {
    final A a;
    public Origin(A a) {
        this.a = a;
    }
}

public class A {
    final B b;
    @Inject public A (B b) {
        this.b = b;
    }
}

public class B {
    final Lazy<A> a;
    @Inject public B (Lazy<A> a) {
        this.a = a;
    }
}

Then on my module I have:

@Provides @Singleton Origin providesOrigin(A a) {
    return new Origin(a);
}

Problem is, even though I'm using Lazy , Dagger 2 gives me the following compile time error:

error: AppComponent.inject() contains a dependency cycle

Am I missing something? I tried replacing Lazy with Provider but the result is the same.

My problem was that I was using version 2.0 of Dagger where this was still an open issue . Once I updated it to the current 2.1 version the problem was not present anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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