Few things you may consider updating in your application using dagger latest version

Md Ali Hossain
2 min readJan 23, 2020

--

Note: All this changes based on dagger `2.26` version. You need to update your dagger with this(latest) version to use all these features.

Performance is an important thing when your application getting bigger due to implementation lots of feature day by day. Most of us using the dagger library to handle the dependency stuffs specially when we have lots of module in our application. Those are using the dagger for handling dependency you may consider refactoring with these things by updating your dagger to latest version. Which will increase your build speed as well as get rid of some boilerplate and generated code.

# Qualifiers for field attributes

When an annotation is placed on a property in Kotlin, it’s not clear whether Java will see that annotation on the field of the property or the method for that property. Setting the field: prefix on the annotation ensures that the qualifier ends up in the right place.

Currently the way to apply qualifiers on an injected field is like:

@Inject@field:Named("name")lateinit var someName: InterfaceName

Forgetting to add field could lead to injecting the wrong object if there’s an unqualified instance of that type available in the Dagger graph.

From this version, We don’t need to add field annotation anymore with qualifier

@Inject@Named("name")lateinit var someName: InterfaceName

# @JvmStatic with @Provides functions optimization

Dagger’s generated code will be more performant if @Provides methods are static. To achieve this in Kotlin, We use a Kotlin object instead of a class and annotate our methods with @JvmStatic which we don’t need anymore from latest version.

See the code below:

@Moduleobject FeatureModule {@Provides
// No more @JvmStatic annotation
fun provideSomething() = SomeClass()}

Also for companion object you don’t need to add @Module and @JvmStatic annotation anymore form version 2.26

// @Module is no longer needed here!
companion object {
// @JvmStatic is no longer needed here!
@Provides fun provideSomething() = SomeClass()
}

# @Component.Factory instead of @Component.Builder

Dagger introduced another amazing annotation named @Component.Factory to fix the problems were introduced as the result of using @Component.Builder. You can check this link for more details: https://android.jlelse.eu/dagger2-component-factory-and-subcomponent-factory-b181ec96b213

Future improvements coming…

@EntryPoint another interesting things still not added in this version (Will be reduced the pain to handle the AndroidInjection dependency in fragment and activity). Also probably @JvmSuppressWildcards will be removed from generics which may be available in next versions.

You can also checked the build performance after changing all this locally for debug build using command: ./gradlew — profile — offline — rerun-tasks assembleDevDebug

Happy coding with dagger stuffs :)

--

--

Md Ali Hossain
Md Ali Hossain

Written by Md Ali Hossain

Senior Android Engineer @Delivery Hero | Android developer | Kotlin lover | Flutter explorer | Problem Solver

No responses yet