dependency injection checks

Dependency Injection Usage Checks

97
3
Java

Dependency Injection usage Checks



What does it do?

DI checks is an annotation processor used to detect common issues when using dependency injection frameworks that use JSR 330 like Toothpick or Dagger.
When an issue is found compilation will fail.

Currently, the library has a single check that verifies a common programming error when using dependency injection: duplicating the same injection of the same dependency within a given hierarchy of types (i.e. a super class and a subclass perform the same injection, which is useless). This check is called DuplicateInjectionInHierarchy.

Diagram

Why should you use it?

  • It’s fast (couple of ms in production code)
  • Easy to setup (see setup section)
  • Customizable (each check can be customized to issue warnings or compiler error)
  • Works with multiple DI libraries
  • Tested on projects that rely heavily on dependency injection

Setup

Just add the following line in your module’s gradle file to get started:

dependencies {
    ...
    annotationProcessor "com.groupon.android.dichecks:compiler:1.0.2" <--- Add this to your dependencies.
    ...
}

You have fine grained control over checks with this pattern:


android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [
                    ...
                    'com.groupon.android.dichecks.duplicateCheck.failOnError': 'false', <--- Issue warnings instead of compiler errors.
                    'com.groupon.android.dichecks.duplicateCheck.enabled': 'true',     <--- Enable or disable check completely.
                    ...
                ]
            }
        }
    }
}

Future plans

We will add other useful checks related to dependency injection.

Credits