Skip to content
dexafree edited this page Sep 2, 2014 · 10 revisions

Okay, so you've seen this project and wondered WTF is Groovy. You've made some research, and seen how awesome it is.

So, do you want to use it on your Android application development? Here's how.

ALERT

If you try to start some Activities which aren't annotated with @CompileStaticyou will probably get some StackOverflow errors as Groovy support for Android is still on a beta stage. Please add this annotation to all your Activities like this:

@CompileStatic
public class MyActivity extends Activity {
...

Android Studio

First of all, Android Studio has native Groovy support, so here you don't really have to do anything. You'll only need to create your new Class files as a .groovy file, instead of a Java class (you could define your own template).

Gradle

In order to make Android Studio compile your Android Groovy code, you will need a plugin which is under development. You will have to make a few changes at your module's build.gradle:

build.gradle

buildscript {
    repositories {
        mavenLocal()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.12.+'
        classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.0'
    }
}

apply plugin: 'com.android.application'
apply plugin: 'me.champeau.gradle.groovy-android'

android {
    ...
    packagingOptions {
        // workaround for http://stackoverflow.com/questions/20673625/android-gradle-plugin-0-7-0-duplicate-files-during-packaging-of-apk
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/groovy-release-info.properties'
    }
}

dependencies{
    compile 'org.codehaus.groovy:groovy:2.4.0-beta-3:grooid'
}

After this little edits, you're ready to start using Groovy in your Android development.

References