Top Banner
1 Android and Groovy
53

Groovy on Android

Jul 08, 2015

Download

Technology

Mario Garcia

Presentation during Codemotion 2014 about using Groovy for Android development.
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Groovy on Android

1

Android and Groovy

Page 2: Groovy on Android

2

Mario Garcia

Software EngineerHappily Working for @marioggar

Page 3: Groovy on Android

3

Let me tell you a story...

Page 4: Groovy on Android

4

Long Long time ago....

There was a back-end developer ...

Page 5: Groovy on Android

5

Long Long time ago....

He used to code using Groovy

Page 6: Groovy on Android

6

Long Long time ago....

But not all was happiness...

At the beginning Groovy couldn'tWork on Android

Page 7: Groovy on Android

7

But finally that day came....

During the GR8Conf...

Page 8: Groovy on Android

8

Yes now you can!!!Released Groovy 2.4 :grooid

Thanks Cedric!!!!

Cedric Champeau core developer

Page 9: Groovy on Android

9

Page 10: Groovy on Android

10

He took his mobile and....

Every seemed to be awesome but then ...

Page 11: Groovy on Android

11

Light became darkness because....

Page 12: Groovy on Android

12

Darkest hour

No background on Android development

Activity

Fragment

Layouts

Adapters

UIThread

Bla bla...

Bla bla...Bla bla...

Page 13: Groovy on Android

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

14

Create the first project....

Tools, tools everywhere

...

Page 15: Groovy on Android

15

Two paths

%

Page 16: Groovy on Android

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

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

18

Gradle

Plugins

Page 19: Groovy on Android

19

Gradle

Android SDKs version

Page 20: Groovy on Android

20

Gradle

Dependencies

Page 21: Groovy on Android

21

Android Studio

Import your Gradle app

Page 22: Groovy on Android

22

Android Studio

Project structure

Page 23: Groovy on Android

23

Java Path (Android Studio)

Ask AS to create a Java project

Add Cedric plugin

Keep working

Page 24: Groovy on Android

24

Android Studio

Or create a new Android/Gradle project

Page 25: Groovy on Android

25

He swore he would never...

How can avoid repeating the whole process again ?

Page 26: Groovy on Android

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

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

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

29

SHOW ME THE CODE

How Groovy can help you ?

Page 30: Groovy on Android

30

Groovy by default

At the beginning everything looked like Java:

Page 31: Groovy on Android

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

32

Groovy by default

Bean construction:Using map constructor:

Page 33: Groovy on Android

33

Groovy collections and more...

Collections were more funFiltering and mapping (find/collect)

Page 34: Groovy on Android

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

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

36

Closures - Fn

Currying and MethodClosure:Plain Java methods can become high order functions

Page 37: Groovy on Android

37

Closures

Functional Interface coertion:

Page 38: Groovy on Android

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

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

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

41

SwissKnife

Injecting Views. Forget about findViewById

Page 42: Groovy on Android

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

43

SwissKnife

@OnBackground and @UIThread

Page 44: Groovy on Android

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

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

46

What about performance ?

Faster, cleaner, cheaper...

Page 47: Groovy on Android

47

Performance

Before blaming the language you may want to check if the problem is elsewhere ;)

Page 48: Groovy on Android

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

49

Performance

http://open.blogs.nytimes.com/2014/08/?_r=0

Page 50: Groovy on Android

50

What's next ?

The future

Page 51: Groovy on Android

51

Android Wear

Stay tuned athttp://melix.github.io/blog/

Page 52: Groovy on Android

52

Who knows ?

Groovy Groovy everywhere

Page 53: Groovy on Android

53

Thank you …

Q & A