You should look at kodein, a DI library for kotlin. Works brilliantly.
Dependency injection on Android with dagger-android and Kotlin
11–19 of 19 posts
Re: Dependency injection on Android with dagger-android and Kotlin
#12I've never quite understood the appeal of Dependency Injection. What's wrong with just using constructors?
* monstrously big constructors (for carrying transitive dependencies)
* lots of @VisibleForTesting code to handle manually injecting various dependencies only for the sake of testing (poor man's DI and generally bad practice)
* a lot of factories (service locator or poor man's DI, essentially)
* code that's hard to unit test due to dependencies being hardcoded.
In other words, you'll either reinvent DI poorly, or give up on testability.
Re: Dependency injection on Android with dagger-android and Kotlin
#13I tried to learn Dagger for a recent project in a similar setting (Dagger+Android+Kotlin). Spent some 8 hours, couldn't make it work and gave up. I suppose, as with most systems, one needs to build up a list of recipes for solving common problems. But with Dagger I found that notoriously hard for some reason. Perhaps because it tends to fail silently at runtime or with non-descriptive errors.
Re: Dependency injection on Android with dagger-android and Kotlin
#14Dagger2 is great when you have it setup, generating the dependency graph during compilation makes it lightning fast during execution. I also like the error messages, as long as you can read Java stack traces all the info for solving errors are in there - I've never seen an unsolvable situation this far. Kapt does bump the clean build time to about 2min, incremental builds take between 10-60s - although actually launc…
I think there are two reasons for this. First, is how scopes interact with Android component lifecycles, which make everything harder (like RxJava, etc) since they are a complexity multiplier.
Second, there are seemingly bizarre design decisions. For example, if you use dependent components (aka component dependencies), the syntax is pretty straightforward:
@Component { }
interface AppComponent { X,Y,Z }
@Component { dependencies = AppComponent.class }
interface LoginComponent { }
So the natural order of things is, components use modules, and components can depend on other components. Pretty simple.Subcomponents turns this relatively simple schema around. The documentation for subcomponents say:
> "To add a subcomponent to a parent component, add the subcomponent class to the subcomponents attribute of a @Module that the parent component installs."
I find is truly strange, now we have modules depending on (sub)components which is the inverse relationship as above.
Re: Dependency injection on Android with dagger-android and Kotlin
#15I've never quite understood the appeal of Dependency Injection. What's wrong with just using constructors?
Then you have a mess without DI.
Re: Dependency injection on Android with dagger-android and Kotlin
#16I tried to learn Dagger for a recent project in a similar setting (Dagger+Android+Kotlin). Spent some 8 hours, couldn't make it work and gave up. I suppose, as with most systems, one needs to build up a list of recipes for solving common problems. But with Dagger I found that notoriously hard for some reason. Perhaps because it tends to fail silently at runtime or with non-descriptive errors.
It really helps to adopt a pattern from another project rather than trying to build one from scratch if you're new to Dagger. But it's definitely worth it, I wouldn't do an Android project nowadays without dependency injections.
Re: Dependency injection on Android with dagger-android and Kotlin
#17Dagger2 is great when you have it setup, generating the dependency graph during compilation makes it lightning fast during execution. I also like the error messages, as long as you can read Java stack traces all the info for solving errors are in there - I've never seen an unsolvable situation this far. Kapt does bump the clean build time to about 2min, incremental builds take between 10-60s - although actually launc…
Re: Dependency injection on Android with dagger-android and Kotlin
#18Dagger2 is great when you have it setup, generating the dependency graph during compilation makes it lightning fast during execution. I also like the error messages, as long as you can read Java stack traces all the info for solving errors are in there - I've never seen an unsolvable situation this far. Kapt does bump the clean build time to about 2min, incremental builds take between 10-60s - although actually launc…
I wrote a semantics doc to try to help on the documentation front: https://google.github.io/dagger/semantics/ It's not intended as a tutorial, so perhaps it won't quite meet your needs, but if you're curious how all the pieces fit together, it may help.
Re: Dependency injection on Android with dagger-android and Kotlin
#19Unfortunately, using Dagger for DI with Kotlin requires using the Kotlin annotation processor, kapt, which is still incredibly slow, and works non-incrementally. If you value your build times or your sanity in the slightest, I'd recommend either severely limiting use of Dagger, e.g. by only using it in certain, small Gradle modules, or using a different DI solution.