psh / auto-dagger2

Annotation processor on top of Dagger 2 that generates components for you.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Auto Dagger2

Build Status Codacy Badge GitHub License GitHub last commit Libraries.io for GitHub Dependabot Status

Auto Dagger2 has a simple goal: to reduce the boilerplate code required by Dagger2.

You can also mix manually written components with the ones generated by Auto Dagger2. Auto Dagger2 produces the human-readable code you would (hopefully) write yourself.

Getting started

@AutoComponent
@Singleton
class ExampleApplication : Application() {
}

Will generate ExampleApplicationComponent

@Generated("autodagger.compiler.AutoDaggerAnnotationProcessor")
@Component
@Singleton
public interface ExampleApplicationComponent {
}

API Overview

  • @AutoComponent - define a component
  • @AutoSubcomponent - define a subcomponent along with the plus___() methods to extend the dagger graph
  • @AutoBinds - defines a binding module for implementors of interfaces and adds the modules to components
  • @AutoExpose - creates methods on a component / subcomponent to expose dependencies
  • @AutoInject - creates injection methods on a component / subcomponent

@AutoComponent

Annotate a class with @AutoComponent to generated an associated Component. On the component, you can add dependencies, modules and superinterfaces. As you can see, the @Singleton annotation is applied to the generated component as well.

@AutoComponent(
    modules = [MainActivity.ModuleOne::class],
    dependencies = [ExampleApplication::class],
    superinterfaces = [ExampleApplication::class, GlobalComponent::class]
)
@Singleton
class MainActivity : AppCompatActivity() {

    @Module
    class ModuleOne {}

    ...
}

It generates MainActivityComponent

@Generated("autodagger.compiler.AutoDaggerAnnotationProcessor")
@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.ModuleOne.class
)
@Singleton
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
}

@AutoInjector

@AutoInjector allows to add injector methods into a generated component.

@AutoInjector(FirstActivity::class)
class ObjectA {
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
  void inject(ObjectA objectA);
}

If you apply the @AutoInjector on the same class that has the @AutoComponent annotation, you can skip the value member:

@AutoInjector
@AutoComponent(
    modules = [MainActivity.ModuleOne::class],
    dependencies = [ExampleApplication::class],
    superinterfaces = [ExampleApplication::class, GlobalComponent::class],
)
class MainActivity : Activity() {
}

If your class have parameterized type, you can also specify it:

@AutoInjector(value = [MainActivity::class], parameterizedTypes = [String::class, String::class])
class MyObject3<T, E> {
    val t: T? = null
    val e: E? = null
}

@AutoExpose

@AutoExpose allows to expose a dependency within a generated component.

@AutoExpose(MainActivity::class)
class SomeObject @Inject constructor() {
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
public interface MainActivityComponent extends ExampleApplicationComponent, GlobalComponent {
  SomeObject someObject();
}

If you apply the @AutoExpose on the same class that has the @AutoComponent annotation, you can skip the value member:

@AutoInjector
@AutoComponent(
    modules = [MainActivity.ModuleOne::class],
    dependencies = [ExampleApplication::class],
    superinterfaces = [ExampleApplication::class, GlobalComponent::class],
)
@AutoExpose
class MainActivity : Activity() {
}

@AutoExpose can also expose dependency from a module's provider method:

@Provides
@DaggerScope(FirstActivity::class)
@Named("1")
@AutoExpose(FirstActivity::class)
fun providesMyObject4Qualifier1(): MyObject4 {
    return MyObject4()
}

If your class have parameterized type, you can also specify it:

@AutoExpose(value = MainActivity.class, parameterizedTypes = {String.class, String.class})
public class MyObject3<T, E> {
    private T t;
    private E e;

    @Inject
    public MyObject3() {
    }
}

Reuse @AutoComponent

You can reuse @AutoComponent by creating an annotation that is itself annotated with @AutoComponent.

@AutoComponent(
        dependencies = MyApp.class,
        superinterfaces = {HasDependenciesOne.class, HasDependenciesTwo.class},
        modules = StandardModule.class
)
public @interface StandardActivityComponent { }

You can then create an auto component that reuse directly that annotation.
It will adds to the already defined dependencies, modules and superinterfaces.

@AutoComponent(
        modules = SixthActivity.Module.class,
        includes = StandardActivityComponent.class)
public class SixthActivity extends Activity { }

You can also directly annotate the class:

@StandardActivityComponent
public class SixthActivity extends Activity { }

Scope

Whenever you use @AutoComponent, you also need to annotate the class with a dagger scope annotation (an annotation that is itself annotated with @Scope). Auto Dagger2 will detect this annotation, and will apply it on the generated component.

If you don't provide scope annotation, the generated component will be unscoped.

Installation

Beware that the groupId changed to com.github.lukaspili.autodagger2

apply plugin: 'com.android.application'

dependencies {
    kapt 'com.github.matfax.auto-dagger2:compiler:1.2.0'
    implementation 'com.github.matfax.auto-dagger2:library:1.2.0'

    kapt 'com.google.dagger:dagger-compiler:2.24'
    implementation 'com.google.dagger:dagger:2.24'
    compileOnly 'javax.annotation:javax.annotation-api:1.3.2' // Android only
}

History

Auto Dagger2 was originally extracted from Auto Mortar to work as a standalone library by Lukasz Piliszczuk (@lukaspili)

Subsequently ported to Kotlin and brought up-to-date by Paul Hawke (@psh)

License

Auto Dagger2 is released under the MIT license. See the LICENSE file for details.

About

Annotation processor on top of Dagger 2 that generates components for you.

License:MIT License


Languages

Language:Kotlin 99.7%Language:Shell 0.3%