Developing a native Android App using Groovy

Many of us Java developers have been curious about being able to develop native android app using groovy for a long time. This topic has been discussed in stackoverflow for years. In GR8Conf Europe this year, Cédric Champeau showed Groovy Support for Android starting Groovy 2.4.
In this post we will build a simple Android app using groovy.

Create a new Android app using Android Studio (0.8.6 was the latest version at the time of writing).
1-create

Edit the “build.gradle” of your “app” module and add the following:

3-build-gradle

You will need to add groovy as a dependency as well.

With these changes you now can start using groovy.
Let’s rename the Activity “.java” to “.groovy” file.
Here is the content of the activity file that is written in groovy

4-Groovy Activity

You can build and deploy the app on your device.

5-screenshot

You can checkout the project in github. https://github.com/manijshrestha/GroovyDroidExample

One thought on “Developing a native Android App using Groovy

  1. Ken says:

    I’ve been wondering about groovy in Android but I’m always curious what the performance implication is.

    1. Hi Ken, As of now it is slow. In the link below you will find technical details of how Groovy runs on Android.

      http://melix.github.io/blog/2014/06/grooid2.html

      Hope this helps.
      Thanks,

      1. Hi,

        I just want to correct the last comment: as of now, it is *not* slow. It runs pretty good actually.

        1. Hi Cédric,
          Thank you for your comment. Groovy on Android does run very good. Thanks for correcting me on that. I experienced some lag while loading the UI for the very first time. Once the layout was inflated, it did run pretty fast.
          I would like to personally thank you for your hard work and bringing groovy on Android.

  2. Edvin says:

    Hello,

    Can Java and Groovy files coexist in a single project (is it possible to mix both languages)?

    Many thanks 😉

  3. beginner says:

    Hey. I’m just curious – it’s just implementation only of Groovy language into Android am I right? We’re not using all architecture of Grails framework right? We don’t need any server neither, right?

    Regards! Thanks for an article and help.

  4. chao li says:

    07-16 17:02:33.518 31074-31074/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.lichao.test.youyuan.com.groovytest/com.lichao.test.youyuan.com.groovytest.MainActivity}: java.lang.ClassNotFoundException: Didn’t find class “com.lichao.test.youyuan.com.groovytest.MainActivity” on path: DexPathList[[zip file “/data/app/com.lichao.test.youyuan.com.groovytest-2.apk”],nativeLibraryDirectories=[/data/app-lib/com.lichao.test.youyuan.com.groovytest-2, /vendor/lib, /system/lib]]
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2282)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
    at android.app.ActivityThread.access$600(ActivityThread.java:162)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:194)
    at android.app.ActivityThread.main(ActivityThread.java:5426)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.ClassNotFoundException: Didn’t find class “com.lichao.test.youyuan.com.groovytest.MainActivity” on path: DexPathList[[zip file “/data/app/com.lichao.test.youyuan.com.groovytest-2.apk”],nativeLibraryDirectories=[/data/app-lib/com.lichao.test.youyuan.com.groovytest-2, /vendor/lib, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2273)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)
                at android.app.ActivityThread.access$600(ActivityThread.java:162)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1364)
                at android.os.Handler.dispatchMessage(Handler.java:107)
                at android.os.Looper.loop(Looper.java:194)
                at android.app.ActivityThread.main(ActivityThread.java:5426)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:525)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
                at dalvik.system.NativeStart.main(Native Method)

  5. Danny says:

    I followed your instructions but got this error: Error:(52, 0) Plugin with id ‘me.champeau.gradle.groovy-android’ not found.

Leave a Reply

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

*

*