Android Apps with Groovy
About me
● Mario Garcia– Software engineer at Kaleidos
https://github.com/mariogarcia
@marioggar
Before I begin...
● Thanks to Tuenti● Disclaimer
– I'm not an Android developer– Questions welcomed :)
What is this talk about ?
● Summary– Android is NOT ONLY Java– Your first Grooid app– Groovy features– Useful libraries– Reactive– Testing– Q & A
Android is NOT ONLY Java
● Why not Groovy ?– Why not… ?– Chose your poison:
Create your first Android/Groovy app
Create your first Groovy app
● The Android Studio wizard– Meant for Java– If you go this way, then:
● Create a Java app through the wizard● Add Groovy/Gradle plugin● Add needed basic dependencies● …
Create your first Groovy app
● A better way:– Create a basic skeleton with Lazybones– Import as Gradle project to Android Studio– Profit!
Create your first Groovy app
● Lazybones– A simple project creation tool– Templates– Really easy to use and extend
Create your first Groovy app
● Grooid templates– Lazybones templates for creating:
● Android app, Android lib● Coming soon: Google Glass app
Create your first Groovy app
● Gradle– Android Reference build tool– Convention over Configuration– Still thinking of Maven ? check this out:
● https://gradle.org/maven_vs_gradle/
Create your first Groovy app
● Android Studio– Reference IDE based on Intellij IDEA– Really helpful specially for UI– Integrates perfectly with Gradle projects
Create your first Groovy app
● Demo
Groovy featuresout-of-the-box
Groovy features
● Domain classes● Get rid of accessors● No need to override constructors● More AST to do pumbling staf
– @AutoExternalize– @Canonical– @Immutable– @InheritConstructors– …. many more
Groovy features
● Beans
Groovy features
● Constructors
Groovy features
● GDK– Groovy extensions to the JDK
● Groovy collections (collect/inject/find)● Safe methods to deal with JDK Classes
– e.g: take(int) : use it instead of substring
Groovy features
● NPE and Exceptions– NPE
● ? operator● * operator● Elvis :?
– Exceptions● Go functional || Reactive
– FNZ: Try<T>, Maybe<T>– RxJava/RxAndroid: onError()
Groovy features
● Safe operators
Groovy Features
● FNZ
Groovy features
● Closures● No need to create inner classes anymore● Wherever there is a Functional Interface you can
use a Closure● Also useful when creating DSLs
Groovy features
● Closures
Groovy features
● Closures (cont)
Groovy features
● DSLs
Groovy features
● Traits– Have you ever wanted to share some
functionality between an Activity and a ListActivity ?
● Another way to share functionality among class hierarchies
● Used as if it were an interface● IDE friendly
Groovy features
Provides waiting spinner
Provides easier call to Toast.makeXXX
● Traits
Groovy advancedFeatures
Advanced features
● Extension Modules– Add new functionality to existing code– Easy to use and program
● Just classes with static methods● Android Studio recognizes Extension Modules out-of-
the-box
Advanced features
Busted!
● Extension modules
Advanced features
● Custom ASTs– Add new functionality to existing code
● During compilation time● Extremely powerful● No need for an annotation-processor● Harder to code than Extension Modules
Advanced features
● Show a dialog
Advanced features
Advanced features
Advanced features
● Swissknife– Makes easier to deal with view injection and
threading nightmare.● Based on AndroidAnnotations and ButterKnife● Collection of: ASTs, DSLs, Extension Modules
ReactiveProgramming
Reactive Programming
● RxAndroid + Groovy– Less verbose than using Java:
● Closures and Functional Interface coertion● Composable Closures● Method Reference
Code
TestingAnd
Quality
Testing
● Spock– A better way of testing
● DSL to describe your tests in a fluent way● Compatible with JUnit
Testing
● AndroidSpock– Create integration tests
● Under src/androidTest/groovy● Helpers to inject and initialize activities in your tests● Deploys and run tests in the device
Testing
● Robospock– Create unit tests
● Based on roboelectric 3.0● no DEXing or deployment time is wasted to execute
your unit tests.
Other tools
● Quality code– Codenarc
● Static analysis for Groovy● Highly Configurable● Completely integrated with Gradle
References
● Links● Android Studio
– https://developer.android.com/sdk/index.html● Lazybones
– https://github.com/pledbrook/lazybones● Grooid templates
– https://github.com/mariogarcia/grooid-templates● Swissknife
– https://github.com/Arasthel/SwissKnife● AndroidRx
– https://github.com/ReactiveX/RxAndroid
References
● Links● Spock
– http://spockframework.github.io/spock/docs/1.0/index.html● Android Spock
– https://github.com/pieces029/android-spock● Robo Spock
– http://robospock.org/● Gradle
– https://gradle.org/● Groovy Lang
– http://www.groovy-lang.org/
References
● Links● Codenarc
– http://codenarc.sourceforge.net/● FNZ
– http://mariogarcia.github.io/fnz/
Q & A
Common question
● Size of Groovy :grooid ?
Thanks!!