SlideShare a Scribd company logo
1 of 42
Download to read offline
android.talks@infinum.hr
Getting
on Android
Ivan Kušt
Java on Android
• no Java 8 and lambda expressions
• writing a lot of boilerplate code
Groovy
• optionally typed
• dynamic language
• compiles into Java VM bytecode
Groovy
Thread thread = new Thread(new Runnable() {



@Override

void run() {

//do stuff in background

}

});
• example (executing code in a new thread) on Java:
Groovy
• same thing in Groovy, using closures:
Thread.start { //do stuff in background }
Groovy
• more concise code
• more readable code
• still type safe
• still fast
Benefits of Groovy
• semi colons are optional
• parentheses are optional
• dynamic typing
• return keyword is optional
• public keyword is optional
• all Java is valid Groovy
vs
Default imports
• java.io.*
• java.lang.*
• java.math.BigDecimal
• java.math.BigInteger
• java.net.*
• java.util.*
• groovy.lang.*
• groovy.util.*
Special operators
• Elvis operator

• ?. operator
def name = person.name ?: "unknown"
def name = person?.name
Initializers
• Java:
• Groovy:
int[] array = { 1, 2, 3}
int[] array = [1,2,3]
• { … } block reserved for closures
Initializers
def list = [1, 2, 3, 4 ,5]



def map = [a: 1, b: 2, c:3]



def regex = ~/.*foo.*/



def range = 128..255



def closure = {a, b -> a + b}
• other available initializers:
== operator
• == in groovy translates to:

a.compareTo(b) == 0 if they are Comparable

a.equals(b) otherwise
Groovy truth
• in Java:

• in Groovy:

if(s)
if(s != null && s.length() > 0)
Package scope
• Java: package private field
• Groovy: property
• package private field in groovy:
class Person {
String name
}
class Person {
@PackageScope String name
}
Multi-methods
• java: result = 2 (method chosen at compile time)
• groovy: result = 1 (method chosen at run time)
int method(String arg) {
return 1;
}
int method(Object arg) {
return 2;
}
Object o = "Object";
int result = method(o);
Properties
public class Pokemon {



private String name;



public String getName() {

return name;

}



public void setName(String name) {

this.name = name;

}

}
Pokemon pokemon = new Pokemon();

pokemon.setName("Pikachu");
public class Pokemon {



String name;

}
Pokemon pokemon =
new Pokemon(name: "Pikachu")

pokemon.setName("Raichu")
Annotation processing
• no annotation processing in Groovy
• AST transformations
• example: @Immutable - implements immutable “by
the book”
AST transformations example
public final class Person {



private final String name;

private final int age;



public Person(String name, int age) {

this.name = name;

this.age = age;

}



public String getName() {

return name;

}



public int getAge() {

return age;

}



@Override

public int hashCode() {

return age + 31 * name.hashCode();

}



@Override

public boolean equals(Object other) {

if(other == null) {

return false;

}



if(this == other) {

return true;

}



if(Person.class != other.getClass()) {

return false;

}



Person otherPerson = (Person) other;

if(!name.equals(otherPerson.name)) {

return false;

}



if(age != otherPerson.age) {

return false;

}



return true;

}



@Override

public String toString() {

return "Person(" + name + ", " + age + ")";

}

}
import groovy.transform.Immutable



@Immutable

public class Person {

String name;

int age;

}
with {} method
view = new TextView(context);

view.setName(name);

view.setTextSize(16f);

view.setTextColor(Color.WHITE);
view = new TextView(context);
view.with {

text = name

textSize = 16f

textColor = Color.WHITE

}
Resource handling
File f = new File("/sdcard/dir/f.txt");

if(f.exists() && f.canRead()) {

FileInputStream fis = null;

try {

fis = new FileInputStream(f);

byte[] bytes = new byte[fis.available()];

while(fis.read(bytes) != -1) {};

textView.setText(new String(bytes));

} catch(IOException e) {

//handle

} finally {

if(fis != null) {

try {

fis.close();

} catch(IOException e) {

//ignore

}

}

}

} else {

//handle

}
def f = new File("/sdcard/dir/f.txt");

if(f.exists() && f.canRead()) {

f.withInputStream { fis ->

def bytes = new byte[fis.available()]

while(fis.read(bytes) != -1) {}

textView.setText(new String(bytes))

}

}
HTTP GET example
def url = "https://api.github.com/repos" +

"groovy/groovy-core/commits"

def commits = new JsonSlurper().parseText(url.toURL().text)



assert commits[0].commit.author.name == "Cedric Champeau"
• easy to parse JSON REST API:
But there is more
• all the differences are described:

http://groovy-lang.org/differences.html
Groovy project setup
Groovy project setup
Groovy project setup
Groovy project setup
Groovy project setup
Groovy project setup
Modify build.gradle
buildscript {

repositories {

jcenter()

}

dependencies {

classpath 'com.android.tools.build:gradle:1.0.1'

classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.0'

}

}



apply plugin: 'com.android.application'

apply plugin: 'me.champeau.gradle.groovy-android'



android {

…

}



dependencies {

compile fileTree(dir: 'libs', include: ['*.jar'])



compile ‘org.codehaus.groovy:groovy-all:2.4.3'



…

}
Add groovy code
• rename class files from .java to .groovy
• Android Studio has autocomplete support!
Proguard
-dontobfuscate

-keep class org.codehaus.groovy.reflection.** { *; }

-keep class org.codehaus.groovy.vmplugin.** { *; }

-keep class org.codehaus.groovy.runtime.dgm* { *; }

-keepclassmembernames class org.codehaus.groovy.runtime.dgm* {

*;

}

-keepclassmembernames class ** implements

org.codehaus.groovy.runtime.GeneratedClosure {

*;

}

-dontwarn org.codehaus.groovy.**

-dontwarn groovy**
Groovy overhead
Java Groovy
Without
Proguard
947 kB 2.9 MB
With
Proguard
719 kB 1.6 MB
Android
Groovy libraries
Libraries
• all Android libraries can be used with Groovy
• caveat: annotation processing won’t be run 

(except for .java files)
Swiss knife
• Butterknife for Groovy
• same annotations as Butterknife
• in onCreate() add:
SwissKnife.inject(this);

SwissKnife.restoreState(this, savedInstanceState);
compile ‘com.arasthel:swissknife:1.2.3'
• in build.gradle add:
The good
• less boilerplate and more concise code
• closures
• neat Exception stacktraces for closures 

(vs Java 8 lambdas)
The bad
• a little bit slower than Java Android project
• larger .apk
• no annotation processing (APT)
Conclusion
• good for smaller projects
• faster to code
• lots of syntax sugar
• add some overhead to performance and .apk size
Resources
• Groovy presentation by Guillaume Laforge: 

https://speakerdeck.com/glaforge/groovy-on-
android-droidcon-paris-2014
• Groovy official site:

http://groovy-lang.org/
• groovy project example:

https://github.com/ikust/groovy-pokemons

More Related Content

What's hot

What's hot (20)

Building GUI App with Electron and Lisp
Building GUI App with Electron and LispBuilding GUI App with Electron and Lisp
Building GUI App with Electron and Lisp
 
Better DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-EclipseBetter DSL Support for Groovy-Eclipse
Better DSL Support for Groovy-Eclipse
 
Groovy
GroovyGroovy
Groovy
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Tdd With Groovy
Tdd With GroovyTdd With Groovy
Tdd With Groovy
 
The Ring programming language version 1.5.4 book - Part 180 of 185
The Ring programming language version 1.5.4 book - Part 180 of 185The Ring programming language version 1.5.4 book - Part 180 of 185
The Ring programming language version 1.5.4 book - Part 180 of 185
 
Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1Golang basics for Java developers - Part 1
Golang basics for Java developers - Part 1
 
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
Asynchronous IO in Rust - Enrico Risa - Codemotion Rome 2017
 
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017Briefly Rust - Daniele Esposti - Codemotion Rome 2017
Briefly Rust - Daniele Esposti - Codemotion Rome 2017
 
Dexador Rises
Dexador RisesDexador Rises
Dexador Rises
 
Exploring Clojurescript
Exploring ClojurescriptExploring Clojurescript
Exploring Clojurescript
 
Kotlin - A very quick introduction
Kotlin - A very quick introductionKotlin - A very quick introduction
Kotlin - A very quick introduction
 
DaNode - A home made web server in D
DaNode - A home made web server in DDaNode - A home made web server in D
DaNode - A home made web server in D
 
Atlassian Groovy Plugins
Atlassian Groovy PluginsAtlassian Groovy Plugins
Atlassian Groovy Plugins
 
用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構用 Go 語言打造多台機器 Scale 架構
用 Go 語言打造多台機器 Scale 架構
 
High Performance Python Microservice Communication
High Performance Python Microservice CommunicationHigh Performance Python Microservice Communication
High Performance Python Microservice Communication
 
Node ppt
Node pptNode ppt
Node ppt
 
Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...Asynchronous single page applications without a line of HTML or Javascript, o...
Asynchronous single page applications without a line of HTML or Javascript, o...
 
Introduction to TPL
Introduction to TPLIntroduction to TPL
Introduction to TPL
 
Inroduction to golang
Inroduction to golangInroduction to golang
Inroduction to golang
 

Similar to Infinum android talks_10_getting groovy on android

Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
James Williams
 
Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)
Jim Driscoll
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
Andres Almiray
 

Similar to Infinum android talks_10_getting groovy on android (20)

Boosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with GroovyBoosting Your Testing Productivity with Groovy
Boosting Your Testing Productivity with Groovy
 
Javaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 GroovytestingJavaone2008 Bof 5101 Groovytesting
Javaone2008 Bof 5101 Groovytesting
 
Apache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and YouApache Groovy's Metaprogramming Options and You
Apache Groovy's Metaprogramming Options and You
 
Groovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web ApplicationsGroovy & Grails: Scripting for Modern Web Applications
Groovy & Grails: Scripting for Modern Web Applications
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Groovy for java developers
Groovy for java developersGroovy for java developers
Groovy for java developers
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
An Introduction to Gradle for Java Developers
An Introduction to Gradle for Java DevelopersAn Introduction to Gradle for Java Developers
An Introduction to Gradle for Java Developers
 
Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)Groovy DSLs (JavaOne Presentation)
Groovy DSLs (JavaOne Presentation)
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
 
Android Bootstrap
Android BootstrapAndroid Bootstrap
Android Bootstrap
 
Having Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo SurabayaHaving Fun with Kotlin Android - DILo Surabaya
Having Fun with Kotlin Android - DILo Surabaya
 
Greach - The Groovy Ecosystem
Greach - The Groovy EcosystemGreach - The Groovy Ecosystem
Greach - The Groovy Ecosystem
 
From Java to Python
From Java to PythonFrom Java to Python
From Java to Python
 
Code Generation with Groovy, Lombok, AutoValue and Immutables - Ted's Tool Time
Code Generation with Groovy, Lombok, AutoValue and Immutables - Ted's Tool TimeCode Generation with Groovy, Lombok, AutoValue and Immutables - Ted's Tool Time
Code Generation with Groovy, Lombok, AutoValue and Immutables - Ted's Tool Time
 
Dsl로 만나는 groovy
Dsl로 만나는 groovyDsl로 만나는 groovy
Dsl로 만나는 groovy
 
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
Contract First Development with Microsoft Code Contracts and Microsoft Pex at...
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Grooscript greach 2015
Grooscript greach 2015Grooscript greach 2015
Grooscript greach 2015
 
Using the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM DevelopmentUsing the Groovy Ecosystem for Rapid JVM Development
Using the Groovy Ecosystem for Rapid JVM Development
 

More from Infinum

More from Infinum (20)

Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
Infinum Android Talks #20 - Making your Android apps fast like Blue Runner an...
 
Infinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtilInfinum Android Talks #20 - DiffUtil
Infinum Android Talks #20 - DiffUtil
 
Infinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using KotlinInfinum Android Talks #20 - Benefits of using Kotlin
Infinum Android Talks #20 - Benefits of using Kotlin
 
Infinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactiveInfinum iOS Talks #4 - Making our VIPER more reactive
Infinum iOS Talks #4 - Making our VIPER more reactive
 
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
Infinum iOS Talks #4 - Making your Swift networking code more awesome with Re...
 
Infinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelperInfinum Android Talks #13 - Using ViewDragHelper
Infinum Android Talks #13 - Using ViewDragHelper
 
Infinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4jInfinum Android Talks #14 - Log4j
Infinum Android Talks #14 - Log4j
 
Infinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-awareInfinum Android Talks #9 - Making your app location-aware
Infinum Android Talks #9 - Making your app location-aware
 
Infinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle pluginsInfinum Android Talks #14 - Gradle plugins
Infinum Android Talks #14 - Gradle plugins
 
Infinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android APIInfinum Android Talks #14 - Facebook for Android API
Infinum Android Talks #14 - Facebook for Android API
 
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
Infinum Android Talks #19 - Stop wasting time fixing bugs with TDD by Domagoj...
 
Infinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan MarićInfinum Android Talks #18 - Create fun lists by Ivan Marić
Infinum Android Talks #18 - Create fun lists by Ivan Marić
 
Infinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan MarićInfinum Android Talks #18 - In-app billing by Ivan Marić
Infinum Android Talks #18 - In-app billing by Ivan Marić
 
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko PlesacInfinum Android Talks #18 - How to cache like a boss by Željko Plesac
Infinum Android Talks #18 - How to cache like a boss by Željko Plesac
 
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan VujaklijaInfinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
Infinum iOS Talks #2 - VIPER for everybody by Damjan Vujaklija
 
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan ĐikićInfinum iOS Talks #2 - Xamarin by Ivan Đikić
Infinum iOS Talks #2 - Xamarin by Ivan Đikić
 
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho PolutaInfinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
Infinum iOS Talks #1 - Swift under the hood: Method Dispatching by Vlaho Poluta
 
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan DikicInfinum iOS Talks #1 - Swift done right by Ivan Dikic
Infinum iOS Talks #1 - Swift done right by Ivan Dikic
 
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran BurojevicInfinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
Infinum iOS Talks #1 - Becoming an iOS developer swiftly by Vedran Burojevic
 
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan KustInfinum Android Talks #17 - Testing your Android applications by Ivan Kust
Infinum Android Talks #17 - Testing your Android applications by Ivan Kust
 

Recently uploaded

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
masabamasaba
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
masabamasaba
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 

Recently uploaded (20)

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
WSO2CON 2024 - Building the API First Enterprise – Running an API Program, fr...
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Toronto Psychic Readings, Attraction spells,Brin...
 
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
WSO2CON 2024 - API Management Usage at La Poste and Its Impact on Business an...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open SourceWSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
WSO2CON 2024 - Freedom First—Unleashing Developer Potential with Open Source
 
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
%+27788225528 love spells in Atlanta Psychic Readings, Attraction spells,Brin...
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
WSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security ProgramWSO2CON 2024 - How to Run a Security Program
WSO2CON 2024 - How to Run a Security Program
 
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
WSO2CON 2024 - Navigating API Complexity: REST, GraphQL, gRPC, Websocket, Web...
 
tonesoftg
tonesoftgtonesoftg
tonesoftg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 

Infinum android talks_10_getting groovy on android

  • 3. Java on Android • no Java 8 and lambda expressions • writing a lot of boilerplate code
  • 4.
  • 5. Groovy • optionally typed • dynamic language • compiles into Java VM bytecode
  • 6. Groovy Thread thread = new Thread(new Runnable() {
 
 @Override
 void run() {
 //do stuff in background
 }
 }); • example (executing code in a new thread) on Java:
  • 7. Groovy • same thing in Groovy, using closures: Thread.start { //do stuff in background }
  • 8. Groovy • more concise code • more readable code • still type safe • still fast
  • 9. Benefits of Groovy • semi colons are optional • parentheses are optional • dynamic typing • return keyword is optional • public keyword is optional • all Java is valid Groovy
  • 10. vs
  • 11. Default imports • java.io.* • java.lang.* • java.math.BigDecimal • java.math.BigInteger • java.net.* • java.util.* • groovy.lang.* • groovy.util.*
  • 12. Special operators • Elvis operator
 • ?. operator def name = person.name ?: "unknown" def name = person?.name
  • 13. Initializers • Java: • Groovy: int[] array = { 1, 2, 3} int[] array = [1,2,3] • { … } block reserved for closures
  • 14. Initializers def list = [1, 2, 3, 4 ,5]
 
 def map = [a: 1, b: 2, c:3]
 
 def regex = ~/.*foo.*/
 
 def range = 128..255
 
 def closure = {a, b -> a + b} • other available initializers:
  • 15. == operator • == in groovy translates to:
 a.compareTo(b) == 0 if they are Comparable
 a.equals(b) otherwise
  • 16. Groovy truth • in Java:
 • in Groovy:
 if(s) if(s != null && s.length() > 0)
  • 17. Package scope • Java: package private field • Groovy: property • package private field in groovy: class Person { String name } class Person { @PackageScope String name }
  • 18. Multi-methods • java: result = 2 (method chosen at compile time) • groovy: result = 1 (method chosen at run time) int method(String arg) { return 1; } int method(Object arg) { return 2; } Object o = "Object"; int result = method(o);
  • 19. Properties public class Pokemon {
 
 private String name;
 
 public String getName() {
 return name;
 }
 
 public void setName(String name) {
 this.name = name;
 }
 } Pokemon pokemon = new Pokemon();
 pokemon.setName("Pikachu"); public class Pokemon {
 
 String name;
 } Pokemon pokemon = new Pokemon(name: "Pikachu")
 pokemon.setName("Raichu")
  • 20. Annotation processing • no annotation processing in Groovy • AST transformations • example: @Immutable - implements immutable “by the book”
  • 21. AST transformations example public final class Person {
 
 private final String name;
 private final int age;
 
 public Person(String name, int age) {
 this.name = name;
 this.age = age;
 }
 
 public String getName() {
 return name;
 }
 
 public int getAge() {
 return age;
 }
 
 @Override
 public int hashCode() {
 return age + 31 * name.hashCode();
 }
 
 @Override
 public boolean equals(Object other) {
 if(other == null) {
 return false;
 }
 
 if(this == other) {
 return true;
 }
 
 if(Person.class != other.getClass()) {
 return false;
 }
 
 Person otherPerson = (Person) other;
 if(!name.equals(otherPerson.name)) {
 return false;
 }
 
 if(age != otherPerson.age) {
 return false;
 }
 
 return true;
 }
 
 @Override
 public String toString() {
 return "Person(" + name + ", " + age + ")";
 }
 } import groovy.transform.Immutable
 
 @Immutable
 public class Person {
 String name;
 int age;
 }
  • 22. with {} method view = new TextView(context);
 view.setName(name);
 view.setTextSize(16f);
 view.setTextColor(Color.WHITE); view = new TextView(context); view.with {
 text = name
 textSize = 16f
 textColor = Color.WHITE
 }
  • 23. Resource handling File f = new File("/sdcard/dir/f.txt");
 if(f.exists() && f.canRead()) {
 FileInputStream fis = null;
 try {
 fis = new FileInputStream(f);
 byte[] bytes = new byte[fis.available()];
 while(fis.read(bytes) != -1) {};
 textView.setText(new String(bytes));
 } catch(IOException e) {
 //handle
 } finally {
 if(fis != null) {
 try {
 fis.close();
 } catch(IOException e) {
 //ignore
 }
 }
 }
 } else {
 //handle
 } def f = new File("/sdcard/dir/f.txt");
 if(f.exists() && f.canRead()) {
 f.withInputStream { fis ->
 def bytes = new byte[fis.available()]
 while(fis.read(bytes) != -1) {}
 textView.setText(new String(bytes))
 }
 }
  • 24. HTTP GET example def url = "https://api.github.com/repos" +
 "groovy/groovy-core/commits"
 def commits = new JsonSlurper().parseText(url.toURL().text)
 
 assert commits[0].commit.author.name == "Cedric Champeau" • easy to parse JSON REST API:
  • 25. But there is more • all the differences are described:
 http://groovy-lang.org/differences.html
  • 32. Modify build.gradle buildscript {
 repositories {
 jcenter()
 }
 dependencies {
 classpath 'com.android.tools.build:gradle:1.0.1'
 classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.0'
 }
 }
 
 apply plugin: 'com.android.application'
 apply plugin: 'me.champeau.gradle.groovy-android'
 
 android {
 …
 }
 
 dependencies {
 compile fileTree(dir: 'libs', include: ['*.jar'])
 
 compile ‘org.codehaus.groovy:groovy-all:2.4.3'
 
 …
 }
  • 33. Add groovy code • rename class files from .java to .groovy • Android Studio has autocomplete support!
  • 34. Proguard -dontobfuscate
 -keep class org.codehaus.groovy.reflection.** { *; }
 -keep class org.codehaus.groovy.vmplugin.** { *; }
 -keep class org.codehaus.groovy.runtime.dgm* { *; }
 -keepclassmembernames class org.codehaus.groovy.runtime.dgm* {
 *;
 }
 -keepclassmembernames class ** implements
 org.codehaus.groovy.runtime.GeneratedClosure {
 *;
 }
 -dontwarn org.codehaus.groovy.**
 -dontwarn groovy**
  • 35. Groovy overhead Java Groovy Without Proguard 947 kB 2.9 MB With Proguard 719 kB 1.6 MB
  • 37. Libraries • all Android libraries can be used with Groovy • caveat: annotation processing won’t be run 
 (except for .java files)
  • 38. Swiss knife • Butterknife for Groovy • same annotations as Butterknife • in onCreate() add: SwissKnife.inject(this);
 SwissKnife.restoreState(this, savedInstanceState); compile ‘com.arasthel:swissknife:1.2.3' • in build.gradle add:
  • 39. The good • less boilerplate and more concise code • closures • neat Exception stacktraces for closures 
 (vs Java 8 lambdas)
  • 40. The bad • a little bit slower than Java Android project • larger .apk • no annotation processing (APT)
  • 41. Conclusion • good for smaller projects • faster to code • lots of syntax sugar • add some overhead to performance and .apk size
  • 42. Resources • Groovy presentation by Guillaume Laforge: 
 https://speakerdeck.com/glaforge/groovy-on- android-droidcon-paris-2014 • Groovy official site:
 http://groovy-lang.org/ • groovy project example:
 https://github.com/ikust/groovy-pokemons