Auto Dagger2 Compiler

Auto Dagger2

License

License

Categories

Categories

Auto Application Layer Libs Code Generators Dagger Dependency Injection
GroupId

GroupId

com.github.lukaspili.autodagger2
ArtifactId

ArtifactId

autodagger2
Last Version

Last Version

1.1
Release Date

Release Date

Type

Type

jar
Description

Description

Auto Dagger2 Compiler
Auto Dagger2
Project URL

Project URL

https://github.com/lukaspili/auto-dagger2
Source Code Management

Source Code Management

https://github.com/lukaspili/auto-dagger2

Download autodagger2

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2/ -->
<dependency>
    <groupId>com.github.lukaspili.autodagger2</groupId>
    <artifactId>autodagger2</artifactId>
    <version>1.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2/
implementation 'com.github.lukaspili.autodagger2:autodagger2:1.1'
// https://jarcasting.com/artifacts/com.github.lukaspili.autodagger2/autodagger2/
implementation ("com.github.lukaspili.autodagger2:autodagger2:1.1")
'com.github.lukaspili.autodagger2:autodagger2:jar:1.1'
<dependency org="com.github.lukaspili.autodagger2" name="autodagger2" rev="1.1">
  <artifact name="autodagger2" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.lukaspili.autodagger2', module='autodagger2', version='1.1')
)
libraryDependencies += "com.github.lukaspili.autodagger2" % "autodagger2" % "1.1"
[com.github.lukaspili.autodagger2/autodagger2 "1.1"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

Auto Dagger2

Auto Dagger2 is an annotation processor built on top of the Dagger2 annotation processor.
It basically generates the component for you.

The goal is to reduce the boilerplate code required by Dagger2 when you have "empty" or simple components. It is usually the case in Android development.

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
public class ExampleApplication extends Application { 
}

It generates ExampleApplicationComponent

@Component
@Singleton
public interface ExampleApplicationComponent { 
}

As you can see, the @Singleton annotation is applied to the generated component as well.

API

@AutoComponent

Annotate a class with @AutoComponent to generated an associated Component. On the component, you can add dependencies, modules and superinterfaces.

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@Singleton
public class MainActivity extends Activity {
}

It generates MainActivityComponent

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

@AutoInjector

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

@AutoInjector(MainActivity.class)
public class ObjectA {
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
@Singleton
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:

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@AutoInjector
@Singleton
public class MainActivity extends Activity {
}

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

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

@AutoExpose

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

@AutoExpose(MainActivity.class)
@Singleton
public class SomeObject {

    @Inject
    public SomeObject() {
    }
}

It updates the MainActivityComponent by adding the following method:

@Component(
    dependencies = ExampleApplicationComponent.class,
    modules = MainActivity.Module.class
)
@Singleton
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:

@AutoComponent(
    dependencies = ExampleApplication.class,
    modules = MainActivity.Module.class,
    superinterfaces = {ExampleApplication.class, GlobalComponent.class})
@AutoExpose
@Singleton
public class MainActivity extends Activity {
}

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

@dagger.Module
public class Module {
    @Provides
    @Singleton
    @AutoExpose(MainActivity.class)
    public SomeOtherObject providesSomeOtherObject() {
        return new SomeOtherObject();
    }
}

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

@AutoExpose(value = MainActivity.class, parameterizedTypes = {String.class, String.class})
@Singleton
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)
@Singleton
public class SixthActivity extends Activity { }

You can also directly annotate the class:

@StandardActivityComponent
@Singleton
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

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
		classpath 'com.android.tools.build:gradle:1.1.3'
		classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    apt 'com.github.lukaspili.autodagger2:autodagger2-compiler:1.1'
    compile 'com.github.lukaspili.autodagger2:autodagger2:1.1'

    apt 'com.google.dagger:dagger-compiler:2.0.1'
    compile 'com.google.dagger:dagger:2.0.1'
    provided 'javax.annotation:jsr250-api:1.0' // Android only
}

Status

Stable API.

Auto Dagger2 was extracted from Auto Mortar to work as a standalone library.
You can find more about Auto Mortar here: https://github.com/lukaspili/Auto-Mortar

Author

License

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

Versions

Version
1.1