Package dagger

Annotation Type Component.Builder


  • @Retention(RUNTIME)
    @Target(TYPE)
    @Documented
    public static @interface Component.Builder
    A builder for a component.

    A builder is a type with setter methods for the modules, dependencies and bound instances required by the component and a single no-argument build method that creates a new component instance.

    Components may have a single nested static abstract class or interface annotated with @Component.Builder. If they do, then Dagger will generate a builder class that implements that type. Note that a component with a @Component.Builder may not also have a @Component.Factory.

    Builder types must follow some rules:

    • There must be exactly one abstract no-argument method that returns the component type or one of its supertypes, called the "build method".
    • There may be other other abstract methods, called "setter methods".
    • Setter methods must take a single argument and return void, the builder type or a supertype of the builder type.
    • There must be a setter method for each component dependency.
    • There must be a setter method for each non-abstract module that has non-static binding methods, unless Dagger can instantiate that module with a visible no-argument constructor.
    • There may be setter methods for modules that Dagger can instantiate or does not need to instantiate.
    • There may be setter methods annotated with @BindsInstance. These methods bind the instance passed to them within the component. See @BindsInstance for more information.
    • There may be non-abstract methods, but they are ignored as far as validation and builder generation are concerned.
    For example, this could be a valid Component with a Builder:
    
     @Component(modules = {BackendModule.class, FrontendModule.class})
     interface MyComponent {
       MyWidget myWidget();
    
       @Component.Builder
       interface Builder {
         Builder backendModule(BackendModule bm);
         Builder frontendModule(FrontendModule fm);
         @BindsInstance
         Builder foo(Foo foo);
         MyComponent build();
       }
     }