Groovy on Android (as of 2016)

28
Groovy on Android (as of 2016)

Transcript of Groovy on Android (as of 2016)

Groovy on Android(as of 2016)

About Me

Kevin H.A. Tan

Solutions Evangelist

Polyglot Software Dev

AWPL.co

My Tech Stacks

• Android, Spring Boot, AngularJS, Java in general, Groovy related frameworks like Gaelyk and Grails, etc.

About Android

• mobile operating system based on Dalvik and ART, coded in Java

About Groovy

• JVM programming language that has been existent since 2004

About Groovy

Why Groovy?

• Simplicity compared to Java Syntax

• Reactive (sorta)

• Interoperability with Java code

Differences between yesteryear and 2016

• groovy-all and grooid

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’

Usage

dependencies {

compile ‘org.codehaus.groovy:groovy:2.4.5:grooid’

}

Lazybones

• Project bootstrap

http://mariogarcia.github.io/grooid-templates/

lazybones create grooid-new-project newandroidproject

Lazybones

Project Structure

• src/main/groovy

• src/test/groovy

• src/androidTest/groovy

Source setsandroidGroovy {

sourceSets {

main {

groovy {

srcDirs += ‘src/main/java’

}

}

}

}

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

Domain Classes

CollectionsuserList.findAll { User user ->

user.isCompanyTeam

}.sort { User user ->

user.fullName

}

Prevent NPE

• ? operator

• * operator

responseList?.add(…)

Sample Activity

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(){

}

SwissKnife

SwissKnife.restoreState(saveInstanceState)

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)

Other than that…

• Almost everything works… almost

DEMO

References

Introduction to Groovy

• http://www.slideshare.net/L0rDKadaj/introduction-to-groovy-39150312

• Contact me

http://about.me/kevintanhongann