Groovy on Android
-
Upload
mario-garcia -
Category
Technology
-
view
2.374 -
download
3
description
Transcript of Groovy on Android
![Page 1: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/1.jpg)
1
Android and Groovy
![Page 2: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/2.jpg)
2
Mario Garcia
Software EngineerHappily Working for @marioggar
![Page 3: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/3.jpg)
3
Let me tell you a story...
![Page 4: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/4.jpg)
4
Long Long time ago....
There was a back-end developer ...
![Page 5: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/5.jpg)
5
Long Long time ago....
He used to code using Groovy
![Page 6: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/6.jpg)
6
Long Long time ago....
But not all was happiness...
At the beginning Groovy couldn'tWork on Android
![Page 7: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/7.jpg)
7
But finally that day came....
During the GR8Conf...
![Page 8: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/8.jpg)
8
Yes now you can!!!Released Groovy 2.4 :grooid
Thanks Cedric!!!!
Cedric Champeau core developer
![Page 9: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/9.jpg)
9
![Page 10: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/10.jpg)
10
He took his mobile and....
Every seemed to be awesome but then ...
![Page 11: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/11.jpg)
11
Light became darkness because....
![Page 12: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/12.jpg)
12
Darkest hour
No background on Android development
Activity
Fragment
Layouts
Adapters
UIThread
Bla bla...
Bla bla...Bla bla...
![Page 13: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/13.jpg)
13
Darkest hour
Almost no docs about Groovy on AndroidCedric Champeau
Blog http://melix.github.io/blog/
Github https://github.com/melix
![Page 14: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/14.jpg)
14
Create the first project....
Tools, tools everywhere
...
![Page 15: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/15.jpg)
15
Two paths
%
![Page 16: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/16.jpg)
16
The Gradle Path
Grab some of Cedric's projects
Add what you need and build it
Import to AS
![Page 17: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/17.jpg)
17
Gradle
Hey is Groovy!!Less verbose than MavenIs code not XMLPolyglot
Same build tool for different languages
At some point it should set you free from any IDE
![Page 18: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/18.jpg)
18
Gradle
Plugins
![Page 19: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/19.jpg)
19
Gradle
Android SDKs version
![Page 20: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/20.jpg)
20
Gradle
Dependencies
![Page 21: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/21.jpg)
21
Android Studio
Import your Gradle app
![Page 22: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/22.jpg)
22
Android Studio
Project structure
![Page 23: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/23.jpg)
23
Java Path (Android Studio)
Ask AS to create a Java project
Add Cedric plugin
Keep working
![Page 24: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/24.jpg)
24
Android Studio
Or create a new Android/Gradle project
![Page 25: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/25.jpg)
25
He swore he would never...
How can avoid repeating the whole process again ?
![Page 26: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/26.jpg)
26
Wish list
Basic working Groovy skeletonBasic helpful dependencies to make my life less miserableAdd Asciidoctor :)Add whatever you want...
![Page 27: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/27.jpg)
27
Lazybones
What's lazybonesA simple project creation tool that uses packaged project templates.Created by: Peter Leedbrook
https://github.com/pledbrook/lazybones
![Page 28: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/28.jpg)
28
Lazybones
With lazy bones he could:Basic working Groovy skeleton...Basic dependencies to make my life less miserable...Add Asciidoctor...Add whatever you want...
DONE
DONE
DONE
![Page 29: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/29.jpg)
29
SHOW ME THE CODE
How Groovy can help you ?
![Page 30: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/30.jpg)
30
Groovy by default
At the beginning everything looked like Java:
![Page 31: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/31.jpg)
31
Groovy by default
But even then if you looked a little bit closer...Some Groovy basic AST transformations saved precious lines
Get rid of accessors
![Page 32: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/32.jpg)
32
Groovy by default
Bean construction:Using map constructor:
![Page 33: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/33.jpg)
33
Groovy collections and more...
Collections were more funFiltering and mapping (find/collect)
![Page 34: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/34.jpg)
34
Closures
Turn functional interfaces to functions (I):Whenever you have to implement an interface with just one method you can use a closure
![Page 35: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/35.jpg)
35
Closures
Turn functional interfaces to functions (II):Whenever you have to implement an interface with just one method you can use a closure
![Page 36: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/36.jpg)
36
Closures - Fn
Currying and MethodClosure:Plain Java methods can become high order functions
![Page 37: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/37.jpg)
37
Closures
Functional Interface coertion:
![Page 38: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/38.jpg)
38
Traits
Traits:Sometimes you have to extend different clases but still want to share some common behavior. (Activities/ListActivity)
There are red lines because Android Studio doesn't recognize Traits...yet
![Page 39: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/39.jpg)
39
AST Transformations
ASTs:Anytime you fill there is boilerplate code there is work for an AST to do
You can create your own
@ASTTest@AnnotationCollector@AutoClone@AutoExternalize@Canonical@ConditionalInterrupt@EqualsAndHashcode@Field@Immutable@IndexedProperty@InheritConstructors@Memoized
@Sortable@TailRecursive@ThreadInterrupt@Trait@ToString@TupleConstructor@TypeChecked@Log@Log4j
Still a few more
![Page 40: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/40.jpg)
40
SwissKnife
A multi-purpose Groovy library containing view injection and threading for Android using annotations. It's based on both ButterKnife and AndroidAnnotations
![Page 41: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/41.jpg)
41
SwissKnife
Injecting Views. Forget about findViewById
![Page 42: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/42.jpg)
42
SwissKnife
@OnBackground and @UIThread
@OnBackground
Creates an async task
@UIThread
It gets a handler instance to run the body in the UI thread
![Page 43: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/43.jpg)
43
SwissKnife
@OnBackground and @UIThread
![Page 44: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/44.jpg)
44
SwissKnife
@OnClick, @OnItemSelected...
There are several other annotations for annotating methods you want to be triggered when an event occurs
![Page 45: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/45.jpg)
45
Grooid-tools
A DSL for generating views with Groovyhttps://github.com/karfunkel/grooid-tools
For example create a TextView
s
![Page 46: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/46.jpg)
46
What about performance ?
Faster, cleaner, cheaper...
![Page 47: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/47.jpg)
47
Performance
Before blaming the language you may want to check if the problem is elsewhere ;)
![Page 48: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/48.jpg)
48
Performance
@CompileStatic:It optimizes bytecode applying static check compilationSorry, I don't have benchmarks on AndroidIn the JDK we're doing well with InvokeDynamicOn Android is advisable to use it...what please don't freak out !
![Page 49: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/49.jpg)
49
Performance
http://open.blogs.nytimes.com/2014/08/?_r=0
![Page 50: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/50.jpg)
50
What's next ?
The future
![Page 52: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/52.jpg)
52
Who knows ?
Groovy Groovy everywhere
![Page 53: Groovy on Android](https://reader033.fdocuments.us/reader033/viewer/2022052910/559c54a71a28abb01c8b4635/html5/thumbnails/53.jpg)
53
Thank you …
Q & A