Groovy and Dagger 2 Android Example

I decided to use the Groovy language for an Android app I was working on. Luckily it was fairly straightforward, but I noticed that the number of blogs and examples available to demonstrate using Groovy for Android development are far fewer than Java ones. Not only that, but many of them were out of date.

This is particularly true if you want to use libraries that are not straightforward coding, such as Dagger 2.

I was intending to write a simple proof-of-concept app to start with, to verify that I could use Dagger 2 and Groovy together. Then I found this example app on Github.

However this simple Dagger 2 example did now show how to use the @Inject annotation (which is my preferred way to do Dependency Injection into activities), so I created a fork that did.

Note that you will need to use an up-to-date version of Groovy (2.4.x) to get Groovy and Dagger 2 working on Android.

Code Changes

I only had to make a few code changes to use @Inject, you can check the commits in the project to see what they were.

Firstly in the component interface (in the example, it is called demo.simplegroovyapp.component.VehicleComponent), I added a statement that would inject the dependency objects into the activity.

void inject(MainActivity mainActivity)

Then in the activity class (demo.simplegroovyapp.MainActivity) add the @Inject annotation to the field that was to be injected. The other important change I made was to add the public access modifier, I’ll explain later why this is necessary.

In other words, from this:

Vehicle vehicle

to this:

public Vehicle vehicle

In the onCreate() method, this line was added to inject the dependencies into the activity.


I also commented out the statement that was previously used to manually retrieve the Vehicle object, as it was no longer necessary.

//vehicle = vehicleComponent.provideVehicle()

That’s it!

Property or Field?

In Groovy it is quite common to see data in a class being defined without an access modifier (public, protected, private).

Vehicle vehicle

Likewise in many Dagger 2 Java examples I’ve seen, the @Inject annotation is used on fields without the access modifier.


MyPresenter mypresenter;

However when this is done in Groovy, you are specifying a property, not a field.

Hence Dagger can’t inject the Vehicle object into the activity, even with the @Inject annotation. Once the public access modifier is added, then Dagger works as expected.


2 thoughts on “Groovy and Dagger 2 Android Example”

  1. Hello,
    Great article, I’ve updated this example to use new versions, some improvements to code: remove unnecessary dependency: dexcount, javax.annotation…), and use “annotationProcessor” instead of “provided” to avoid ProGuard issues, add @CompileStatic for all classes, and now we can use @Canonical on domains, Delegates on domain properties… and other useful AST transformations.
    This example can be improved to grails like MVC architecture.

  2. Hi, thanks for your update. To be honest I haven’t done much Groovy development in Android lately as I have been looking at Kotlin as an alternative.

Leave a Reply

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