Develop Android applications using Groovy

This article aims to guide developers to develop Android applications using Groovy.
IntroductionGlossaryGroovy
Groovy is a JVM-based agile development language. It integrates many powerful features of Python, Ruby and Smalltalk. Groovy code can be well incorporated with Java code and used to expand existing code. Since it runs on JVM, Groovy can use the databases compiled in other Java languages.
Official website: http://www.groovy-lang.org/Github repository: https://github.com/apache/groovyGradle
Gradle is an open-source automated build tool mainly used for continuous delivery.
Gradle is primarily developed based on Java and Groovy and used to support automated builds of multiple development languages and platforms, including Java, Scala, Android, C/C++ and Groovy. It can be seamlessly integrated into Eclipse, IntelliJ and Jenkins among other development tools and continuous integration services.
Official website: https://gradle.org/Github repository: https://github.com/gradle/gradleGroovy with Android
The groovy-android-gradle-plug-in is launched by Groovy and used to support Android application development using Groovy.
Github repository: groovy-android-gradle-plugin
Demo:
https://github.com/snowdream/test/tree/master/android/groovy/HelloWorldTutorialThis section introduces how to develop Android applications using Groovy. There are two types of development:
One is development using Groovy only, and the other is development using both Groovy and Java.
The syntax and tutorial on Groovy language is not discussed in this article. If you are interested, you can refer to the documents on its official website and development tutorials online.
Several key notes:
1. All the Groovy class files have a suffix of .groovy.
2. Add an annotation of @CompileStatic for any newly created Groovy class in the class header. The reason for doing this can be found at: Melix’s blog and here for more technical details

3. The Groovy source code directory rule is the same with the default. They are located in src/main/groovy, src/test/groovy, src/androidTest/groovy and any src/${buildVariant}/groovy.
4. Groovy source code directory can be customized in the build.gradle file and the definition rule is as follows:
androidGroovy {
sourceSets {
main {
groovy {
srcDirs += 'src/main/java'
}
}
}
}

To enable Android Studio to recognize the customized directories as the source code directories, you may also need to add them again in the sourceSets of Android plug-ins.
GroovyThe steps for developing Android applications using Groovy only are comparatively easy.
1. Add the following code to the build.gradle file in the project root directory:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.1.2'
classpath 'org.codehaus.groovy:groovy-android-gradle-plugin:1.0.0'
}
}

3. After configuration is complete, you can use Groovy to write Android applications happily in the src/main/groovy directory.
Demo: https://github.com/groovy/groovy-android-gradle-plugin/tree/master/groovy-android-sample-appGroovy and JavaThe steps for developing Android applications using both Groovy and Java are also easy.
1. This step is the same with the above.
2. Add the following code to the build.gradle file in the module directory:
apply plugin: 'groovyx.android'

3. After configuration is complete, you can either write Groovy classes in the src/main/groovy directory, or write Java classes in the src/main/java directory.
Demo: https://github.com/snowdream/test/tree/master/android/groovy/HelloWorldNotes1. When referencing a lib developed using Groovy, you need to exclude the Groovy jar package.
For example, to reference the groovy-xml library, the operations are as follows:
compile ('org.codehaus.groovy:groovy-xml:2.4.3') {
exclude group: 'org.codehaus.groovy'
}

2. The Groovy compilation options are found in the options block under the androidGroovy block.
androidGroovy {
options {
configure(groovyOptions) {
encoding = 'UTF-8'
forkOptions.jvmArgs = ['-noverify'] // maybe necessary if you use Google Play Services
}
sourceCompatibility = '1.7' // as of 0.3.9 these are automatically set based off the android plugin's
targetCompatibility = '1.7'
}
}

3. If you want to reference content of a Groovy file in the Java file, you need to compile all the source code files in Groovyc instead of javac.
androidGroovy {
skipJavaC = true
}

4. If annotations are required, you also need to make the following settings:
androidGroovy {
options {
configure(groovyOptions) {
javaAnnotationProcessing = true
}
}
}

5. If Data Binding is required, a plug-in will be needed: https://bitbucket.org/hvisser/android-aptFor more configurations, see: groovy-android-gradle-pluginSummaryBased on my development practices, I will summarize some advantages and disadvantages of using Groovy to develop Android applications:
Advantages1. Introducing many features of Groovy, including closures, functional programming, static compilation, and DSL.
2. Seamless integration with Java for smooth transition.
Disadvantages1. It will split the jar packages supporting Groovy and pack them into APKs. This part of content will increase the size of the APK by 2M to 3M. This is a mishap for the time being and hopefully the size can be reduced in the future.
2. It will pack some License files into the APKs. This can be filtered by the packagingOptions of Android development plug-ins and is not a big problem.
3. Compared with Java, there are too few developers using Groovy.
4. There are not many documents in Chinese and most of the official documents are in English.
Conclusion1. Android applications can be developed using Groovy.
2. Before applications are launched for production, more evaluations are required including stability, operation efficiency, power consumption, compatibility, and R&D acceptance.