(Pending title)
Anas Ambri
Android Montreal Meetup - September 2014
Candidate title: Groovy on Android
$ whoami
Mobile dev at Guestful
At Guestful
We make reservations for restaurants
- On a restaurant's website
- On a restaurant's FB page
- On our own website
- Through the mobile apps
Disclaimer
This talk is based on code used in the Android project
So, if you ever spot a bug, don't hesitate to let me know. Our users will thank you. KTHXBYE
Title ideas
-
Add some Groove to your app
-
Groovy App Development
-
Android got the grooves
(too long)
(too simple)
(too cheesy)
There is an app for that
Basically, we are gonna write an Android app in Groovy to find a title for a talk about writing Android apps in Groovy
What is Groovy?
Dynamic optionally-typed
def obj = someFunctionToTachQuack(zebra)
obj.quack()
Why Groovy?
Nice substitute to Java's verbosity and excessive OOP-ness
Hello World
println "Hello, world"
Groovy
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Java
Another Example
elements = restaurants.sort({ Object a, b ->
int distanceFromFirst =
DistanceCalculator.getDistanceInMeters(
location,
new Coordinate(a.address.location.coordinates
)
)
int distanceFromSecond =
DistanceCalculator.getDistanceInMeters(
location,
new Coordinate(b.address.location.coordinates
)
)
return distanceFromFirst <=> distanceFromSecond
})
Groovy
Another Example (2)
public class RestaurantDistanceComparator implements Comparator<JsonObject> {
private Coordinate location;
public RestaurantDistanceComparator(Coordinate currentLocation) {
this.location = currentLocation;
}
@Override
public int compare(JsonObject jsonObject, JsonObject jsonObject2) {
int distanceFromFirst = DistanceCalculator.getDistanceInMeters(
new Coordinate(jsonObject.getJsonObject("address")
.getJsonArray("coordinates")),
location
);
int distanceFromSecond = DistanceCalculator.getDistanceInMeters(
new Coordinate(jsonObject2.getJsonObject("address")
.getJsonArray("coordinates")),
location
);
return distanceFromFirst < distanceFromSecond? -1 : 1;
}
}
// FINALLY
Collections.sort(restaurants,
new RestaurantDistanceComparator(location)
);
Java
Json Parsing
def response =
new JsonSlurper().parse(bytes, "utf-8")
callback.onSuccess(response.results)
Groovy
Java
0) Figure out whether you need to
need models or not
If yes ==>
1) Choose among these libraries:
- Gson
- Jackson
2) Create a lot of files,
with models for each object you
will ever exchange with your backend.
3) Give up on programming
If no ==>
1) Consider using Java's JSONObject.
2) Find out it has the worst
implementation ever
because of its constant try-catch.
3) Try javax.json
and Discover its getJsonObject syntax
4) Give up on programming
Demo
Probably the first time someone does a code demo on one leg
Summary
Step 0: Install groovy-2.4.0
//On OSX/Linux
$ curl -s get.gvmtool.net | bash
$ gvm install groovy 2.4.0-beta-3
//On Windows
Switch to Linux
Or
Go here: http://groovy.codehaus.org/Download
Then switch to Linux
Summary
buildscript {
repositories {
mavenCentral()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.12.2'
//Add plugin
classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.0'
}
}
apply plugin: 'android'
//Apply it
apply plugin: 'me.champeau.gradle.groovy-android'
Step 1: Add groovy plugin
Summary
dependencies {
compile 'org.codehaus.groovy:groovy:2.4.0-beta-3:grooid'
//Only needed if using Groovy's Json
compile ('org.codehaus.groovy:groovy-json:2.4.0-beta-3') {transitive = false}
}
Step 2: Add groovy compiler as dependency
When can I use this?
It depends...
Short Answer
For your boss' project, NO
For your own project, go crazy!
Shortcomings
Loose typing = higher method count
- In debug, about 20k methods
- In release, you can use Proguard
Shortcomings
Still in beta = some bugs
For example, can't include the public interface defined in a base class in a derived class
Shortcomings
No joint compilation (yet)
If you liked what you saw
Try Kotlin
References
- Slides: http://verybadalloc.com/
- Demo project
- Groovy Gradle Android plugin
- Groovy Gradle Android plugin (with joint compilation)
- Kotlin
Groovy for Android
By Anas Ambri
Groovy for Android
Slides presented at the Android Montreal meetup, on September 25th
- 2,549