groovy on android (as of 2016)
TRANSCRIPT
My Tech Stacks
• Android, Spring Boot, AngularJS, Java in general, Groovy related frameworks like Gaelyk and Grails, etc.
Why Groovy?
• Simplicity compared to Java Syntax
• Reactive (sorta)
• Interoperability with Java code
Usagebuildscript{
repositories {
jcenter()
}
dependencies {
classpath ‘com.android.tools.build:gradle:1.5.0’
classpath ‘org.codehaus.groovy:gradle-groovy-android-plugin:0.3.8’
}
}
apply plugin: ‘groovyx.grooid.groovy-android’
Lazybones
• Project bootstrap
http://mariogarcia.github.io/grooid-templates/
lazybones create grooid-new-project newandroidproject
Compilation OptionsandroidGroovy {
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'
}
}
Domain Classes• Groovy AST Transformation (AST means Abstract Syntax Tree,
which is basically an abstract representation of code / any syntactic structure. A transformation is an action modifying this tree)
@AutoExternalize
@ToString
@Immutable
@InheritConstructors
Libraries• Fluent API
• JsonSlurper and XMLSlurper
• Groovy AST Transformation
• SwissKnife (similar with Butterknife by Jake Wharton)
More info: http://groovy-almanac.org/ and
http://melix.github.io/blog/2014/06/grooid.html
SwissKnifehttps://github.com/Arasthel/SwissKnife
DSLs for example:
@OnUiThread
void doSomething(){
}
Downsides• Doesn’t fully work with Android Studio in terms of
Goto File implementation (workaround - double tap Shift to search and go to file)
• Right click on package, select New > Create Groovy Class doesn’t appear.
• Packaged APK file is huge (due to the hug grooid jar size)
References
Introduction to Groovy
• http://www.slideshare.net/L0rDKadaj/introduction-to-groovy-39150312