5. Groovy is inside the Gradle
You probably know Groovy through Gradle already.
Groovy is a superset of Java.
It contains lots of cool stuff that makes Java fun!
34. 8) Builders import groovy.json.*
def json = new JsonBuilder()
json.conference {
name 'SZJUG'
subject 'Groovy on Android'
date 2016
time ['13:00', '16:00']
address {
place 'GRAPE 联合创业空间'
street '布吉街道吉华路247号下水径商业大厦三层 3/f'
district '龙岗区'
city '深圳市'
country '中国'
}
}
37. 7) Immutability
// Create immutable instance of User.
def paul = new User('PaulVI', 'paul.verest@live.com')
// will throw an exception on
// paul.email = 'pupkin@mail.com'
def tomasz = mrhaki.copyWith(username: 'Tomasz')
39. // Java
TextView view =
new TextView(context);
view.setText(name);
view.setTextSize(16f);
view.setTextColor(Color.WHITE);
TextView view =
new TextView(context)
view.with {
text = name
textSize = 16f
textColor = Color.WHITE
}
->
40. How to read text file from SD card?
def f = new FIle("/sdcard/dir/f.txt")
if (f.exists() && f.canRead()) {
view.text = f.text
}
41. You have to write a lot of anonymous classes EVERYWHERE
// Java
button.setOnClickListener(new View.OnClickListener() {
@Override
void onClick(View v) {
startActivity(intent);
}
})
button.onClickListener = { startActivity(intent) }
->
42. No native support for generating classes
at runtime
Focus on @CompileStatic
Problems
Generate bytecode, that runs
at the same speed as java files