Anas Ambri
Android Montreal Meetup - September 2014
Candidate title: Groovy on Android
So, if you ever spot a bug, don't hesitate to let me know. Our users will thank you. KTHXBYE
(too long)
(too simple)
(too cheesy)
def obj = someFunctionToTachQuack(zebra)
obj.quack()
println "Hello, world"
Groovy
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World");
}
}
Java
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
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
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
Probably the first time someone does a code demo on one leg
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
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
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
For your own project, go crazy!
For example, can't include the public interface defined in a base class in a derived class