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.

Related Blog Posts
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]