3 APISpark — Backend as a Service • API platform for managing, documenting, and hosting APIs • Data stores • built-in entity store • Parse, Firebase & SQL wrappers • Google Sheets wrapper! • File stores • built-in file store • Github file store • S3 file store
3 APISpark — Backend as a Service • API platform for managing, documenting, and hosting APIs • Data stores • built-in entity store • Parse, Firebase & SQL wrappers • Google Sheets wrapper! • File stores • built-in file store • Github file store • S3 file store Well suited for your Backend-as-a-Service needs!
10 What does NYT likes about Groovy on Android? • No Java 8, no lambda on Android… Func0 func = new Func0() { @Override public String call() { return "my content"; } }; Async.start(func);
12 What does NYT likes about Groovy on Android? • Groovy code more concise and more readable • but just as type-safe as needed! (with @TypeChecked) • but just as fast as needed! (with @CompileStatic)
28 Java to Groovy… • Rename your activity from .java to .groovy • Remove… • public and return keywords • some parentheses • use the property notation • getMenuInflater() becomes menuInflater • use interpolated strings • and more.
40 From Java to Groovy… public class Greeter { private String owner; public String getOwner() { return owner; } public void setOwner(String owner) { this.owner = owner; } public String greet(String name) { return "Hello " + name + ", I am " + owner; } } Greeter greeter = new Greeter(); greeter.setOwner("Guillaume"); System.out.println(greeter.greet("Marion"));
41 Special Groovy syntax sugar — special operators // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } // Elvis def name = person.name ?: "unknown" // save navigation order?.lineItem?.item?.name
41 Special Groovy syntax sugar — special operators // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } // Elvis def name = person.name ?: "unknown" // save navigation order?.lineItem?.item?.name if (person.name != null && person.name.length() > 0)
41 Special Groovy syntax sugar — special operators // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } // Elvis def name = person.name ?: "unknown" // save navigation order?.lineItem?.item?.name Copied by Swift, C# and CoffeeScript!
41 Special Groovy syntax sugar — special operators // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } // Elvis def name = person.name ?: "unknown" // save navigation order?.lineItem?.item?.name Anything null in the chain? Null
41 Special Groovy syntax sugar — special operators // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } // Elvis def name = person.name ?: "unknown" // save navigation order?.lineItem?.item?.name Better NPEs: Cannot get property name on null object
45 HTTP GET and JSON parsing in 4 lines import groovy.json.* 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' An HTTP GET in a one-liner!
50 AST transformations — @Immutable Implement immutability by the book • final class • tuple-style constructor • private final backing fields • defensive copying of collections • equals() and hashCode() methods • toString() method • ...
51 AST transformations — @Immutable • A person class • a String name • an int age 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; } public int hashCode() { return age + 31 * name.hashCode(); } 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.getName()) { return false; } if (age != otherPerson.getAge()) { return false; } return true; } public String toString() { return "Person(" + name + ", " + age + ")"; } }
52 AST transformations — @Immutable • A person class • a String name • an int age import groovy.transform.* @Immutable class Person { String name int age }
54 Traits • Like interfaces, but with method bodies – similar to Java 8 interface default methods • Elegant way to compose behavior – multiple inheritance without the « diamond » problem • Traits can also be stateful – traits can have properties like normal classes • Compatible with static typing and static compilation – class methods from traits also visible from Java classes • Also possible to implement traits at runtime
64 What else? • Projects with dedicated Groovy support • Swiss Knife • similar to Butter Knife & Android Annotations • Grooid Playground • a builder for creating views in a hierarchical & visual manner
68 Swiss Knife — extension methods • Thanks to Groovy’s method extension mechanism, can enrich any type with your own methods • DSL methods for • views • context • fragment • event • bundle
68 Swiss Knife — extension methods • Thanks to Groovy’s method extension mechanism, can enrich any type with your own methods • DSL methods for • views • context • fragment • event • bundle Adds missing methods you wish the Android SDK had!
82 Programming your Android applications in Groovy • You can use Groovy to code Android apps! • use Groovy 2.4.3 • prefer @CompileStatic • Two great posts to get started: • http://bit.ly/grooid-1 • http://bit.ly/grooid-2 • Gradle plugin support: • http://bit.ly/grooid-gradle