55

Dagger 2 is around the corner but the available examples wouldn't even compile right off the box, and the documentation is a copy-paste-replace from Dagger 1.

Does anyone have an example of a proper application working on Google's Dagger 2?

tomrozb
  • 25,773
  • 31
  • 101
  • 122
MLProgrammer-CiM
  • 17,231
  • 5
  • 42
  • 75

4 Answers4

27

I've just published sample app based on Gradle which integrates Dagger2, retrolambda, butterknife and lombok. You can find it here: https://github.com/mgrzechocinski/dagger2-example.

Hope it would help :)

  • 1
    Nice! Just beware of Lombok and general annotation processors on Multidex applications, they're known to have major bugs in Dalvik. – MLProgrammer-CiM Dec 12 '14 at 11:41
  • 3
    It would be really great if you could write up a blog post about your Demo. For newbies, it is really hard to related most of the implementations with the explanation from http://google.github.io/dagger/. – Aung Pyae Feb 09 '15 at 16:13
  • Thanks for the sample you shared. got me going. Much better than the sample that google folks shared. – nemo Feb 28 '15 at 22:48
  • 1
    The example has a lot of extra frameworks/libraries that make the code more verbose and confuse those who are just trying to pickup dagger 2. – frankelot Oct 01 '15 at 18:08
  • What's up with have a separate .Activity and .Application? I've never seen that practice before. Could be important to learn. – Ethan_AI Feb 19 '16 at 23:59
20

I've just ported u2020-mvp app to Dagger 2. We use it as our sandbox app. Dagger 2 has nice implementation of scopes by the way. Components are really big deal. And it is based on u2021 made by Christian Gruber. You can check that as well.

Hope it helps :)

Kirill Boyarshinov
  • 6,143
  • 4
  • 33
  • 29
  • Why do you have two repositories{} sections in your build.gradle file? – IgorGanapolsky Feb 09 '15 at 21:24
  • @IgorGanapolsky, one is buildscript repositories for apt gradle plugin, the second is for project dependencies. They differ from the root one, so they has to overriden. – Kirill Boyarshinov Feb 10 '15 at 02:20
  • Quite confusing to have multiple ones, violates the DRY principle. – IgorGanapolsky Feb 10 '15 at 13:28
  • 2
    It does. Do not like it myself. The main reason of separating repositories was the case when you have multiple subprojects (e.g. libraries, data layer). Considering them full repositories list in root file is redundant. However, for example purposes better to keep them all in root file. Will fix that when I have time. Thanks for suggestion! – Kirill Boyarshinov Feb 10 '15 at 13:53
19

For anyone looking for a simple and straightforward way to just build a Dagger 2 project on Gradle/Android Studio, check out https://github.com/bytehala/dagger2-gradle-quickstart

In the commit messages, I also enumerate the steps to convert a fully functioning project to Dagger 2.

The target audience for this project is anyone who has no experience with dependency injection.

No other library besides Butterknife is used.

WARNING The aim of this sample project is simplicity, not completeness. Only the following features were used:

  • @Module
  • @Component
  • @Injects
  • @Provides
  • @Singleton

Topics such as scopes, submodules, etc are left as an exercise.

bytehala
  • 665
  • 1
  • 10
  • 25
  • 2
    I'd recommend to remove all IntelliJ dependencies like .iml files – MLProgrammer-CiM May 13 '15 at 14:39
  • Really? I didn't know that had to be done. I figured the .gitignore given by AS was enough. LOL I'll do as you recommend in the morning, I promise. Any other files I should remove from git? – bytehala May 13 '15 at 14:47
  • Everything else is okay. .iml files and .idea folder tend to have local paths plus some naughty stuff with gradle versioning if you're using wildcard :+ version numbers. – MLProgrammer-CiM May 13 '15 at 15:17
3

CoffeeMaker sample without android: https://github.com/yongjhih/dagger2-sample

Steps:

git clone https://github.com/yongjhih/dagger2-sample
cd dagger2-sample
./gradlew execute
Andrew Chen
  • 447
  • 7
  • 14
  • 7
    Great finding, although I never found the Coffee Maker sample enlightening. – MLProgrammer-CiM Mar 19 '15 at 02:53
  • You should add arbitrary injection somewhere, the example is incomplete without it. – MLProgrammer-CiM Mar 19 '15 at 02:56
  • Thanks. The sample is imported from official https://github.com/google/dagger/tree/master/examples/simple/src/main/java/coffee . You can see injections in Thermosiphon and CoffeeMaker. – Andrew Chen Mar 20 '15 at 16:00
  • Personally, I found Coffe Maker example confusing. It lacks a lot of information, i.e.: there is no need of using modules in simple cases (when You can provide class objects by yourself jest annotating class constructor with `@Inject` annotation). – Tomasz Dzieniak Mar 10 '16 at 22:48