Tutorial: Create android gradle project with dagger

It is tricky to set up multi-module projects with android & gradle, if you are new to it.

It is even trickier to make it working with Dagger. A bit background knowledge: Dagger is an open-source dependency injection framework designed for Android, by Square Inc. Other open-source option is RoboGuice.

Here is the tutorial, which you can use it to quickly kick off your projects. I’d recommend to run it with latest version of Android Studio or gradle or gradle wrapper. I haven’t tested it with other situations yet. Hope you enjoy it!


Important Notes:
1. I learned these pieces of knowledge from multiple places, including Dagger codebase. Please let me know if I miss a link to your shared tips.

2. I don’t use dagger at my everyday job. Therefore, please kindly let me know if any issue or improvement or pull requests. Enjoy!

3. I may add more inlined comments later. Comment here, if you have questions.

This entry was posted in android and tagged , , . Bookmark the permalink.

13 Responses to Tutorial: Create android gradle project with dagger

  1. Pingback: Gradle & Dagger: Add dagger code generation to android library and app | Mobile & March

  2. JRaymond says:

    As I use the Dagger pre-compiler with my Library dependencies, I often get weird syntax errors in my R.java and elsewhere. any thoughts? Directions to pursue?

    Dependency Structure:

    / | \
    LibA | LibB
    \ | /

    • frankdu says:

      Have you tried the gradle 1.10 + gradle android 0.8.+? It added the provided scope finally. If you still see the issue, mind sharing some stacktrace?

      • JRaymond says:

        It turned out to be that I had parallelized builds on. The stack traces were different every time I ran the thing but they were usually something like this:

        /Path/To/Project/LibA/build/source/r/debug/com/example/liba/LibModule.java:3: class, interface, or enum expected
        import com.example.libc.CClass;
        /Path/To/Project/LibA/build/source/r/debug/com/example/liba/R.java:992: expected
        public static int abc_activity_chooser_view_see_all=0x7f0a0004;
        /Path/To/Project/LibA/build/source/r/debug/com/example/liba/R.java:1443: expected
        public static final int ActionBar_subtitleTextStyle = 6;
        3 Errors

        But non deterministic – the error was different every time. Interesting error, might be worth mentioning in your tutorial somewhere.

  3. Jeff says:

    Hi Frank,

    Thanks for this article, i tested your code in IntelliJ 13.0.2 and it works fine using gradle wrapper. I am new to gradle/java in general, (moving from .net) i’m interested in how gradle does the annotation processing because it seems to do it independently of the java compiler settings in intellij, i am having problems (aka pulling my hair out) getting the dagger code generation working though intellij runner on TeamCity so now i have seen gradle work locally i will try it on TeamCity via gradle wrapper, hopefully with more luck, thanks for the inspiration to keep going 😉

    • frankdu says:

      Jeff, thank you for the words! I assume your project is not android project. Otherwise, android studio is the way to go.

      The annotation processing is a hook into java compiling process to run code analysis or generation and so on. You will need to provide the annotation processor (e.g. dagger-compiler.jar in this case) to the java compiler classpath. If you don’t specify -processor argument, java compiler will discovery the processor automatically. For an example implementation, check http://deors.wordpress.com/2011/10/08/annotation-processors/

      • Jeff says:

        Hi Frank,

        Thanks for the extra info on annotation processing – very useful, i got the processors working locally already, but i cant get them to work on my CI environment, using the intellij runner, so i am switching my whole project to gradle so that i can use the gradle runner. This is and Android project so I will take a look at the android studio, thanks for the tip!

  4. Awesome demonstration! This helped me get up and running fast with Dagger.

  5. thanks for this article.this article help me to run fast my device with dagger.
    appreciate it.

  6. Nice tutorial.It helped me a lot.

    Thanks a lot for sharing .Keep it up.

  7. lin1987www says:

    I has a problem, but it is solved now.

    I hope you can modify your android-gradle-dagger-tutorial.

    🙂 Thanks you

    AndroidCommonLib build.gradle

    dependencies {

    provided ‘javax.inject:javax.inject:1’
    provided ‘com.squareup:javawriter:2.5.0’
    provided ‘com.squareup.dagger:dagger-compiler:1.2.1’
    compile ‘com.squareup.dagger:dagger:1.2.1’


    MyCoolApp build.gradle
    dependencies {

    provided ‘javax.inject:javax.inject:1’
    provided ‘com.squareup:javawriter:2.5.0’
    provided ‘com.squareup.dagger:dagger-compiler:1.2.1’



Leave a Reply

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.