Top Banner
Groovy on Android (as of 2016)
28

Groovy on Android (as of 2016)

Apr 12, 2017

Download

Technology

Kevin H.A. Tan
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Groovy on Android (as of 2016)

Groovy on Android(as of 2016)

Page 2: Groovy on Android (as of 2016)

About Me

Kevin H.A. Tan

Solutions Evangelist

Polyglot Software Dev

AWPL.co

Page 3: Groovy on Android (as of 2016)

My Tech Stacks

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

Page 4: Groovy on Android (as of 2016)

About Android

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

Page 5: Groovy on Android (as of 2016)

About Groovy

• JVM programming language that has been existent since 2004

Page 6: Groovy on Android (as of 2016)

About Groovy

Page 7: Groovy on Android (as of 2016)

Why Groovy?

• Simplicity compared to Java Syntax

• Reactive (sorta)

• Interoperability with Java code

Page 8: Groovy on Android (as of 2016)

Differences between yesteryear and 2016

• groovy-all and grooid

Page 9: Groovy on Android (as of 2016)

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’

Page 10: Groovy on Android (as of 2016)

Usage

dependencies {

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

}

Page 11: Groovy on Android (as of 2016)

Lazybones

• Project bootstrap

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

lazybones create grooid-new-project newandroidproject

Page 12: Groovy on Android (as of 2016)

Lazybones

Page 13: Groovy on Android (as of 2016)

Project Structure

• src/main/groovy

• src/test/groovy

• src/androidTest/groovy

Page 14: Groovy on Android (as of 2016)

Source setsandroidGroovy {

sourceSets {

main {

groovy {

srcDirs += ‘src/main/java’

}

}

}

}

Page 15: Groovy on Android (as of 2016)

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'

}

}

Page 16: Groovy on Android (as of 2016)

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

Page 17: Groovy on Android (as of 2016)

Domain Classes

Page 18: Groovy on Android (as of 2016)

CollectionsuserList.findAll { User user ->

user.isCompanyTeam

}.sort { User user ->

user.fullName

}

Page 19: Groovy on Android (as of 2016)

Prevent NPE

• ? operator

• * operator

responseList?.add(…)

Page 20: Groovy on Android (as of 2016)

Sample Activity

Page 21: Groovy on Android (as of 2016)

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

Page 22: Groovy on Android (as of 2016)

SwissKnifehttps://github.com/Arasthel/SwissKnife

DSLs for example:

@OnUiThread

void doSomething(){

}

Page 23: Groovy on Android (as of 2016)

SwissKnife

SwissKnife.restoreState(saveInstanceState)

Page 24: Groovy on Android (as of 2016)

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)

Page 25: Groovy on Android (as of 2016)

Other than that…

• Almost everything works… almost

Page 26: Groovy on Android (as of 2016)

DEMO

Page 27: Groovy on Android (as of 2016)

References

Introduction to Groovy

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

Page 28: Groovy on Android (as of 2016)

• Contact me

http://about.me/kevintanhongann