Kotlin+Dagger best practices/documentation/pain points
See original GitHub issueOpening this as a tracking bug for all kotlin related documentation that we should be add/best practices that we should call out to make using Dagger w/ Kotlin easier.
One example: How to achieve the effect of static @Provides
in Kotlin.
Feel free to comment new ideas, but don’t make “me too” or “i agree with XYZ” comments.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:77
- Comments:53
Top Results From Across the Web
Dagger in Kotlin: gotchas and Optimizations - Medium
This post was inspired by some of the suggestions in this Dagger issue that goes through best practices and pain points of Dagger...
Read more >Dagger 2 Modules and generic parameters in Kotlin
1 Answer 1 ; Dagger in Kotlin: gotchas and Optimizations (Manuel Vivo, July 2019) ; google/dagger#900: Kotlin+Dagger best practices/documentation/ ...
Read more >تويتر \ Tahsin Dane على تويتر: "#AskAndroid Will Google's ...
#AskAndroid Will Google's Dagger ever be Kotlin or Android Architecture Components ... Kotlin+Dagger best practices/documentation/pain points · Issue #900 ...
Read more >Manual dependency injection - Android Developers
When your application gets larger, you will start seeing that you write a lot of boilerplate code (such as factories), which can be...
Read more >Dagger by Tutorials, Chapter 8: Working With Modules - Kodeco
According to the definition in the @Module documentation, a @Module annotates a class that contributes to the object graph. Note: It's easy to...
Read more >Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
Top Related Hashnode Post
No results found
Top GitHub Comments
Edit: This resolved in Dagger 2.25+ by https://github.com/google/dagger/commit/646e0336cdbe454baa5fe73c0af11f211a92deeb
If you have injected properties (as “fields”), qualifiers must have
field:
designation.Good
Bad
Forgetting this can lead to subtle bugs if an unqualified instance of that type also exists on the graph at that scope, as that’s the one that will end up being injected. This would make a great lint as well
Edit: Objects are handled in Dagger 2.25+. Companion objects are handled in Dagger 2.26+.
Static provides can be achieved via
@JvmStatic
. There are two scenarios I see this come up:top-level
object
sIf you have an existing class module, things get a bit weirder
The way this works is as follows:
companion object
must also be annotated as@Module
DataModule
class. Dagger will see those and treat them like regular static fields. Dagger will also see them in the companion object, but that “module” will get code gen from dagger but be marked as “unused”. The IDE will mark this as such, as theprovideDiskCache
method will be marked as unused. You can tell IntelliJ to ignore this for annotations annotated with@Provides
via quickfixI sent Ron a braindump once of how dagger could better leverage this, i.e. no requirement for JvmStatic and just call through to the generated
Companion
class, but I think that’s out of the scope of this issue 😃. I’ve been meaning to write up a more concrete proposal for how that would work.