kwi wi February 2016

Why does Dagger-2 subcomponent scope not work?

I'm trying to scope subcomponents in Dagger 2 but get the following error on build

cannot be provided without an @Inject constructor or from an @Provides- or @Produces-annotated method.

If I comment the scope annotations, build succeeds. Anyone know why? Is Dagger 2 not designed for this scenario?

ParentComponent.java

import com.cueyoueye.android.inject.annotation.ActivityScope;

import javax.inject.Inject;
import javax.inject.Singleton;

import dagger.Component;
import dagger.Module;
import dagger.Provides;
import dagger.Subcomponent;

@Singleton
@Component(modules = ParentComponent.ParentModule.class)
public interface ParentComponent {
    SubComponent subComponent();

//    @ActivityScope
    @Subcomponent(modules = SubComponentModule.class)
    interface SubComponent {
        void inject(Example activity);
    }

    @Module
    class SubComponentModule {
        @Provides
//        @ActivityScope
        B provideB() {
            return new B("Better");
        }
    }

    class B {
        public B(String s) {
        }
    }

    @Module
    class ParentModule {
        @Provides
        @Singleton
        Ab provideAb() {
            return new Ab("s");
        }
    }

    @Singleton
    class A {
        @Inject
        public A() {
        }
    }

    class Ab {
        public Ab(String s) {
        }
    }
}

Example.java

import javax.inject.Inject;

public class Example {
    @Inject
    ParentComponent.B b;

    public void injectSelf() {
        DaggerParentComponent.builder()
                .build()
                .subComponent()
                .inject(this);
    }
}

Answers


kwi wi February 2016

This code is functional in complete isolation. Something else is causing that build error.

Post Status

Asked in February 2016
Viewed 2,296 times
Voted 7
Answered 1 times

Search




Leave an answer