Upload
mario-garcia
View
2.374
Download
3
Tags:
Embed Size (px)
DESCRIPTION
Presentation during Codemotion 2014 about using Groovy for Android development.
Citation preview
1
Android and Groovy
2
Mario Garcia
Software EngineerHappily Working for @marioggar
3
Let me tell you a story...
4
Long Long time ago....
There was a back-end developer ...
5
Long Long time ago....
He used to code using Groovy
6
Long Long time ago....
But not all was happiness...
At the beginning Groovy couldn'tWork on Android
7
But finally that day came....
During the GR8Conf...
8
Yes now you can!!!Released Groovy 2.4 :grooid
Thanks Cedric!!!!
Cedric Champeau core developer
9
10
He took his mobile and....
Every seemed to be awesome but then ...
11
Light became darkness because....
12
Darkest hour
No background on Android development
Activity
Fragment
Layouts
Adapters
UIThread
Bla bla...
Bla bla...Bla bla...
13
Darkest hour
Almost no docs about Groovy on AndroidCedric Champeau
Blog http://melix.github.io/blog/
Github https://github.com/melix
14
Create the first project....
Tools, tools everywhere
...
15
Two paths
%
16
The Gradle Path
Grab some of Cedric's projects
Add what you need and build it
Import to AS
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
18
Gradle
Plugins
19
Gradle
Android SDKs version
20
Gradle
Dependencies
21
Android Studio
Import your Gradle app
22
Android Studio
Project structure
23
Java Path (Android Studio)
Ask AS to create a Java project
Add Cedric plugin
Keep working
24
Android Studio
Or create a new Android/Gradle project
25
He swore he would never...
How can avoid repeating the whole process again ?
26
Wish list
Basic working Groovy skeletonBasic helpful dependencies to make my life less miserableAdd Asciidoctor :)Add whatever you want...
27
Lazybones
What's lazybonesA simple project creation tool that uses packaged project templates.Created by: Peter Leedbrook
https://github.com/pledbrook/lazybones
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
29
SHOW ME THE CODE
How Groovy can help you ?
30
Groovy by default
At the beginning everything looked like Java:
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
32
Groovy by default
Bean construction:Using map constructor:
33
Groovy collections and more...
Collections were more funFiltering and mapping (find/collect)
34
Closures
Turn functional interfaces to functions (I):Whenever you have to implement an interface with just one method you can use a closure
35
Closures
Turn functional interfaces to functions (II):Whenever you have to implement an interface with just one method you can use a closure
36
Closures - Fn
Currying and MethodClosure:Plain Java methods can become high order functions
37
Closures
Functional Interface coertion:
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
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
40
SwissKnife
A multi-purpose Groovy library containing view injection and threading for Android using annotations. It's based on both ButterKnife and AndroidAnnotations
41
SwissKnife
Injecting Views. Forget about findViewById
42
SwissKnife
@OnBackground and @UIThread
@OnBackground
Creates an async task
@UIThread
It gets a handler instance to run the body in the UI thread
43
SwissKnife
@OnBackground and @UIThread
44
SwissKnife
@OnClick, @OnItemSelected...
There are several other annotations for annotating methods you want to be triggered when an event occurs
45
Grooid-tools
A DSL for generating views with Groovyhttps://github.com/karfunkel/grooid-tools
For example create a TextView
s
46
What about performance ?
Faster, cleaner, cheaper...
47
Performance
Before blaming the language you may want to check if the problem is elsewhere ;)
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 !
49
Performance
http://open.blogs.nytimes.com/2014/08/?_r=0
50
What's next ?
The future
52
Who knows ?
Groovy Groovy everywhere
53
Thank you …
Q & A