question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Kotlin+Dagger best practices/documentation/pain points

See original GitHub issue

Opening 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:open
  • Created 6 years ago
  • Reactions:77
  • Comments:53

github_iconTop GitHub Comments

52reactions
ZacSweerscommented, Jan 10, 2020

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

@Inject @field:ForApi lateinit var gson: Gson

Bad

@Inject @ForApi lateinit var gson: Gson // @ForApi is ignored!

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

49reactions
ZacSweerscommented, Feb 12, 2020

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 objects

@Module
object DataModule {
  @JvmStatic @Provides fun provideDiskCache() = DiskCache()
}

If you have an existing class module, things get a bit weirder

@Module
abstract class DataModule {
  @Binds abstract fun provideCache(diskCache: DiskCache): Cache

  @Module
  companion object {
    @JvmStatic @Provides fun provideDiskCache() = DiskCache()
  }
}

The way this works is as follows:

  • the companion object must also be annotated as @Module
  • under the hood, the kotlin compiler will duplicate those static provides methods into the 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 the provideDiskCache method will be marked as unused. You can tell IntelliJ to ignore this for annotations annotated with @Provides via quickfix

I 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Hashnode Post

No results found