TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Dependency injection on Android with dagger-android and Kotlin

32 点作者 albertgao大约 7 年前

6 条评论

parmesan大约 7 年前
Dagger2 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&#x27;ve never seen an unsolvable situation this far.<p>Kapt does bump the <i>clean</i> build time to about 2min, incremental builds take between 10-60s - although actually launching the app on an emulator&#x2F;device adds another 20s.<p>The cons of Dagger2 that I&#x27;ve experienced since it&#x27;s launch are; The documentation and support is useless. You&#x27;re on your own of you don&#x27;t use a 3rd party sample project as a template. No one understands scopes and subscopes, subcomponents etc. The new Android dagger api is arcane and weird, no one wants to use it.<p>The Dagger2 team should (if they aren&#x27;t already) create a Kotlin extension for it, I believe there are some syntactic optimizations to be offered.
评论 #16865194 未加载
评论 #16865765 未加载
评论 #16902044 未加载
christop大约 7 年前
Unfortunately, using Dagger for DI with Kotlin requires using the Kotlin annotation processor, kapt, which is still incredibly slow, and works non-incrementally.<p>If you value your build times or your sanity in the slightest, I&#x27;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.
评论 #16864361 未加载
评论 #16902061 未加载
the_grue大约 7 年前
I tried to learn Dagger for a recent project in a similar setting (Dagger+Android+Kotlin). Spent some 8 hours, couldn&#x27;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.
评论 #16865689 未加载
seviu大约 7 年前
No need to use kapt to kill compilation times. Data Binding is good enough. We started using it a year ago and since then instant run does not work and the debugger takes three minutes to start. Sometimes it mixes generated classes and we have to do a full clean &#x2F; rebuild.<p>Since then we have introduced Kotlin and Dagger and we are super happy with it.<p>As with Dagger, the learning curve is pretty steep. Having it working with modules + activities + fragments + architecture components has been a real challenge but we love it.<p>For those who are struggling with Dagger: version 2.10 introduced major changes for Android, for good reasons, but it made much harder to use and understand. Many tutorials focus on Dagger &lt; 2.10. You probably should ignore those.<p>And for those who are going crazy with compilation times, Jrebel for Android is a huge time saver. It has an incremental compiler which works with annotation processors. They will stop supporting it in one year, but even then I think it is worth using it. Who knows: if enough people buy licenses maybe they reconsider killing it.
aristidb大约 7 年前
I&#x27;ve never quite understood the appeal of Dependency Injection. What&#x27;s wrong with just using constructors?
评论 #16865658 未加载
评论 #16865377 未加载
评论 #16866630 未加载
mad_tortoise大约 7 年前
You should look at kodein, a DI library for kotlin. Works brilliantly.
评论 #16865620 未加载