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

About the Author

Object Partners profile.

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 *

Related Blog Posts
Feature Flags in Terraform
Feature flagging any code can be useful to developers but many don’t know how to or even that you can do it in Terraform. Some benefits of Feature Flagging your code You can enable different […]
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]