Auto-Dagger2


Source link: https://github.com/lukaspili/Auto-Dagger2

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.

Resources

A custom radio button for Android.

The project is intended for comparing different revisions of image with other revisions.

Reactive Event Hub.

Annotation-based library for generating runtime permissions dispatcher. It supports "Xiaomi" phone.

Simple wrapper library for acquiring 2Checkout payment gateway token.

RecyclerView snapping example with SnapHelper.

Topics


2D Engines   3D Engines   9-Patch   Action Bars   Activities   ADB   Advertisements   Analytics   Animations   ANR   AOP   API   APK   APT   Architecture   Audio   Autocomplete   Background Processing   Backward Compatibility   Badges   Bar Codes   Benchmarking   Bitmaps   Bluetooth   Blur Effects   Bread Crumbs   BRMS   Browser Extensions   Build Systems   Bundles   Buttons   Caching   Camera   Canvas   Cards   Carousels   Changelog   Checkboxes   Cloud Storages   Color Analysis   Color Pickers   Colors   Comet/Push   Compass Sensors   Conferences   Content Providers   Continuous Integration   Crash Reports   Credit Cards   Credits   CSV   Curl/Flip   Data Binding   Data Generators   Data Structures   Database   Database Browsers   Date &   Debugging   Decompilers   Deep Links   Dependency Injections   Design   Design Patterns   Dex   Dialogs   Distributed Computing   Distribution Platforms   Download Managers   Drawables   Emoji   Emulators   EPUB   Equalizers &   Event Buses   Exception Handling   Face Recognition   Feedback &   File System   File/Directory   Fingerprint   Floating Action   Fonts   Forms   Fragments   FRP   FSM   Functional Programming   Gamepads   Games   Geocaching   Gestures   GIF   Glow Pad   Gradle Plugins   Graphics   Grid Views   Highlighting   HTML   HTTP Mocking   Icons   IDE   IDE Plugins   Image Croppers   Image Loaders   Image Pickers   Image Processing   Image Views   Instrumentation   Intents   Job Schedulers   JSON   Keyboard   Kotlin   Layouts   Library Demos   List View   List Views   Localization   Location   Lock Patterns   Logcat   Logging   Mails   Maps   Markdown   Mathematics   Maven Plugins   MBaaS   Media   Menus   Messaging   MIME   Mobile Web   Native Image   Navigation   NDK   Networking   NFC   NoSQL   Number Pickers   OAuth   Object Mocking   OCR Engines   OpenGL   ORM   Other Pickers   Parallax List   Parcelables   Particle Systems   Password Inputs   PDF   Permissions   Physics Engines   Platforms   Plugin Frameworks   Preferences   Progress Indicators   ProGuard   Properties   Protocol Buffer   Pull To   Purchases   Push/Pull   QR Codes   Quick Return   Radio Buttons   Range Bars   Ratings   Recycler Views   Resources   REST   Ripple Effects   RSS   Screenshots   Scripting   Scroll Views   SDK   Search Inputs   Security   Sensors   Services   Showcase Views   Signatures   Sliding Panels   Snackbars   SOAP   Social Networks   Spannable   Spinners   Splash Screens   SSH   Static Analysis   Status Bars   Styling   SVG   System   Tags   Task Managers   TDD &   Template Engines   Testing   Testing Tools   Text Formatting   Text Views   Text Watchers   Text-to   Toasts   Toolkits For   Tools   Tooltips   Trainings   TV   Twitter   Updaters   USB   User Stories   Utils   Validation   Video   View Adapters   View Pagers   Views   Watch Face   Wearable Data   Wearables   Weather   Web Tools   Web Views   WebRTC   WebSockets   Wheel Widgets   Wi-Fi   Widgets   Windows   Wizards   XML   XMPP   YAML   ZIP Codes