Creating Android Application with Groovy

23 / Jul / 2015 by Simranjit Kour 2 comments

Groovy 2.4 was released in January 2015, with native support for Android development. It allows to write Android applications fully using Groovy.

Running Groovy on Android

Step 1: Create a new Android project in Android Studio

Step 2: Open this build.gradle (Module: app) file:
Insert the following code before the first line:

[java]buildscript {
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:1.1.0’
classpath ‘org.codehaus.groovy:gradle-groovy-android-plugin:0.3.6’
}
}[/java]

Insert the following code after the “apply plugin:’com.android.application'” line:

[java]apply plugin: ‘groovyx.grooid.groovy-android'[/java]

Finally, near the end of the gradle script, insert a line in dependencies:

[java]compile ‘org.codehaus.groovy:groovy:2.4.1:grooid'[/java]

After changing the gradle file, gradle needs to be “sync”ed.

Step 3: You will now create an additional “Java Folder” below main. Use New->Folder->Java Folder instead of New->Directory
The name in the entry field is src/main/groovy.
Note: Groovy code files need to be placed in src/main/groovy instead of src/main/java. Adding Groovy files to src/main/java will not work!

Step 4: Create a new, MainActivity, with the context menu of the groovy folder, and enter the name of this class fully qualified with the package name.

Step 5: MainActivity class can be migrated from .java to .groovy by choosing Refactor->Rename File from the context menu. The file extension .java is simply replaced with a .groovy extension.

Step 6: Give the “Hello World” text view an id in the res/layout/activity_main.xml file, like this:

[java]<TextView
android:id="@+id/textview_hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world"/>
[/java]

Step 7: In MainActivity.groovy, programmatically change the text shown by this view using groovy’s string interpolation:

[java]public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)
int id = R.id.textview_hello
findViewById(id).text = "Hello Groovy"
}
}[/java]

Step 8: Build and execute the application.

It is very easy to run Groovy on Android. Because of Groovy’s interoperability with Java, it is also very easy to mix Groovy and Java in the same application.

Happy groovy’ing!

FOUND THIS USEFUL? SHARE IT

comments (2)

  1. IbrahimH.

    Hi, just for update to the last version of groovy android gradle plugin (1.1.0), here are the steps:
    1- add to the dependencies of the project the following: classpath ‘org.codehaus.groovy:groovy-android-gradle-plugin:1.1.0’
    2- include groovy plugin into your build.gradle of your app module:
    apply plugin: ‘groovyx.android’
    3- include grooid as compile dependency into your dependencies of your app module:
    compile ‘org.codehaus.groovy:groovy:2.4.4:grooid’
    Synchronize and you’re all set, you can now rename your activities (such as: MainActivity.java to MainActivity.groovy) and other classes to groovy files, you should also use @CompileStatic in every class for better performance, then don’t forget to take advantages of groovy language: Groovy JDK, AST Tranformation, DSL, Traits… and take a look at related projects: SwissKnife, Dagger2.

    Reply

Leave a Reply to Araglon Cancel reply

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