Groovy 2.4+ is providing support for developing Android applications in Groovy. In this presentation, we see how Groovy can help streamline the development of Android apps with the Groovy language.
What does NYT likes about Groovy on Android? • No Java 8, no lambda on Android… 6 Func0 func = new Func0() { @Override public String call() { return "my content"; } }; Async.start(func);
What does NYT likes about Groovy on Android? • No Java 8, no lambda on Android… 7 ! ! ! ! ! ! Async.start { "my content" } Good bye annonymous inner classes!
Special Groovy syntax sugar — special operators 37 // Groovy truth // if (s != null && s.length() > 0) {...} if (s) { ... } ! // Elvis def name = person.name ?: "unknown" ! // save navigation order?.lineItem?.item?.name
Special Groovy syntax sugar — special operators 37 // 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)
Special Groovy syntax sugar — special operators 37 // 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!
Special Groovy syntax sugar — special operators 37 // 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
Special Groovy syntax sugar — special operators 37 // 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
HTTP GET and JSON parsing in 4 lines 41 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!
• an int age 45 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 + ")"; } }