Groovy is a dynamic language similar to Python, Ruby, Pearl and Smalltalk. Since it compiles to Java bytecode it can be used to write Android applications as well. We’ll demonstrate how to setup a Groovy Android application project, show the advantages, disadvantages and provide some code examples.
https://www.youtube.com/watch?v=9yxj9bxQ9H4
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 }
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
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
}
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