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
Up to Spec: JavaScript Numeric Separators
Let's take a look at the proposal to add Numeric Separators to the JavaScript specification.
Using Conftest to Validate Configuration Files
Conftest is a utility within the Open Policy Agent ecosystem that helps simplify writing validation tests against configuration files. In a previous blog post, I wrote about using the Open Policy Agent utility directly to […]
SwiftGen with Image & Color Asset Catalogs
You might remember back in 2015 when iOS 9 was introduced, and we were finally given a way to manage all of our assets in one place with Asset Catalogs. A few years later, support […]
Tracking Original URL Through Authentication
If you read my other post about refreshing AWS tokens, then you probably have a use case for keeping track of the original requested resource while the user goes through authentication so you can route […]