Slide 1

Slide 1 text

© 2014 SpringOne 2GX. All rights reserved. Do not distribute without permission. Groovy and Android, a winning pair? By Cédric Champeau

Slide 2

Slide 2 text

#s2gx #groovylang @CedricChampeau About me Pivotal employee Core Groovy committer Compilation configuration Static type checking & Static compilation Traits, new template engine, ... Groovy in Action 2 co-author Misc OSS contribs (Gradle plugins, deck2pdf, jlangdetect, ...) 2

Slide 3

Slide 3 text

#s2gx #groovylang @CedricChampeau Social (or why to use #groovylang) 3

Slide 4

Slide 4 text

#s2gx #groovylang @CedricChampeau Groovy is Open-Source • Licensed under APL2 • 100+ contributors • 10000+ commits • 1.7+ million downloads in 2013 • On GitHub since summer 2011 • Dependency of 25000+ OSS projects 4

Slide 5

Slide 5 text

5

Slide 6

Slide 6 text

#s2gx #groovylang @CedricChampeau Why Android? • Uses a JVM • SDK is free • Tooling also freely available (Android Studio) • I don't own a Mac ;) • Swift anyone? 6

Slide 7

Slide 7 text

#s2gx #groovylang @CedricChampeau Why Groovy? • Built on top of the shoulders of a Giant (Java) • Runs a JVM • Android developers shouldn't be suffering • Java on Android is very verbose • And the main development language on the platform 7

Slide 8

Slide 8 text

#s2gx #groovylang @CedricChampeau Groovy on Android: the problems • Groovy is a dynamic language • Not everything done at compile time • Intensive use of reflection • Potentially slow invocation pathes • Battery? • Bytecode is different • Classes at runtime? 8

Slide 9

Slide 9 text

#s2gx #groovylang @CedricChampeau Groovy on Android: the problems • Not all classes are available • java.bean.xxx very problematic • Multiple runtimes • Dalvik • ART • Behavior not the same as the standard JVM 9

Slide 10

Slide 10 text

#s2gx #groovylang @CedricChampeau Groovy on Android: discobot • Early days • Written in 2011 • Fork of Groovy 1.7 • Capable of running scripts at runtime • but slow... 10

Slide 11

Slide 11 text

#s2gx #groovylang @CedricChampeau Groovy on Android: dex files • Dalvik VM = new bytecode format • Groovy generates JVM bytecode • Translation done through dex • No native support for generating classes at runtime 11

Slide 12

Slide 12 text

#s2gx #groovylang @CedricChampeau Compiling an Android application • Classic process 12

Slide 13

Slide 13 text

#s2gx #groovylang @CedricChampeau Compiling an Android application • Classic process for a Groovy application 13

Slide 14

Slide 14 text

#s2gx #groovylang @CedricChampeau Discobot process • Write Groovy bytes to a file • Package those into a jar • Use a special classloader to load the class • Enjoy! 14

Slide 15

Slide 15 text

#s2gx #groovylang @CedricChampeau Compiling an Android application • Runtime generation of classes 15

Slide 16

Slide 16 text

#s2gx #groovylang @CedricChampeau Discobot process • Works, but very slow • Lots of I/O involved • What about ASMDex? • Same approach used by Ruboto • Nice proof of concept 16

Slide 17

Slide 17 text

#s2gx #groovylang @CedricChampeau Redefinining objectives

Slide 18

Slide 18 text

#s2gx #groovylang @CedricChampeau Groovy 2.4: Objectives for Android • Supporting Android in the standard distribution • Building a full Android application in Groovy • Main focus on @CompileStatic • Optional use of dynamic Groovy 18

Slide 19

Slide 19 text

#s2gx #groovylang @CedricChampeau Groovy 2.4: Objectives for community • Community is a major strenght of Groovy • We need you for Android too! • Bring the goodness of Groovy to Android • Invent new frameworks! 19

Slide 20

Slide 20 text

#s2gx #groovylang @CedricChampeau Groovy for Android

Slide 21

Slide 21 text

#s2gx #groovylang @CedricChampeau It already works! 21

Slide 22

Slide 22 text

#s2gx #groovylang @CedricChampeau Requirements • Gradle • Android Studio • Or your favorite editor... • Groovy 2.4.0-beta-3 • A good tutorial on Android... 22

Slide 23

Slide 23 text

#s2gx #groovylang @CedricChampeau Groovy 2.4 Android Support • Must use a specific Android jar • Use of the grooid classifier • Replaces java.beans use with openbeans • Workarounds for Android specific behavior • Reduced number of methods in bytecode • Important for the 64k limit of dex files 23

Slide 24

Slide 24 text

#s2gx #groovylang @CedricChampeau Gradle plugin • Gradle is the new default build system for Android • apply plugin: 'com.android.application' • Uses a non standard compilation process • Without Groovy specific plugin, lots of trickery involved • Thus apply plugin: apply plugin: 'me.champeau.gradle.groovy-android' • Supports both the application and library plugins 24

Slide 25

Slide 25 text

#s2gx #groovylang @CedricChampeau Gradle plugin buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:0.12.2' classpath 'me.champeau.gradle:gradle-groovy-android-plugin:0.3.0' } } apply plugin: 'me.champeau.gradle.groovy-android' dependencies { compile 'org.codehaus.groovy:groovy:2.4.0-beta-3:grooid' } 25

Slide 26

Slide 26 text

#s2gx #groovylang @CedricChampeau Then code! @CompileStatic @ToString(includeNames = true) @EqualsAndHashCode class Session { Long id Long speakerId Slot slot String title String summary List tags } 26

Slide 27

Slide 27 text

#s2gx #groovylang @CedricChampeau Demo

Slide 28

Slide 28 text

#s2gx #groovylang @CedricChampeau Groovifying Android APIs 28 class FeedTask extends AsyncTask { protected String doInBackground(String... params) { // on next slide... } @Override protected void onPostExecute(String s) { mTextView.setText(s); } }

Slide 29

Slide 29 text

#s2gx #groovylang @CedricChampeau Groovifying Android APIs 29 Fluent.async { def json = new JsonSlurper().parse([:], new URL('http://path/to/feed'), 'utf-8') json.speakers.join(' ') } then { mTextView.setText(it) }

Slide 30

Slide 30 text

#s2gx #groovylang @CedricChampeau Performance?

Slide 31

Slide 31 text

#s2gx #groovylang @CedricChampeau System resources • Example of the GR8Conf Agenda application • Groovy jar: 4.5MB • Application size: 2MB! • After ProGuard: only 1MB! • ~8.2MB of RAM! (but lots of images) 31

Slide 32

Slide 32 text

#s2gx #groovylang @CedricChampeau Community

Slide 33

Slide 33 text

#s2gx #groovylang @CedricChampeau Community projects • Community is more important than the language • New frameworks to invent • Some already did! 33

Slide 34

Slide 34 text

#s2gx #groovylang @CedricChampeau SwissKnife • Similar to Android Annotations and ButterKnife • Based on AST transformations • View injection • Threading model • Works with annotations to generate code 34

Slide 35

Slide 35 text

#s2gx #groovylang @CedricChampeau SwissKnife 35 class MyActivity extends Activity { @OnClick(R.id.button) void onButtonClicked(Button button) { Toast.makeText(this, "Button clicked", Toast.LENGTH_SHOT).show() } @OnBackground void doSomeProcessing(URL url) { // Contents will be executed on background ... } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) // This must be called for injection of views and callbacks to take place SwissKnife.inject(this) } }

Slide 36

Slide 36 text

#s2gx #groovylang @CedricChampeau Grooid Tools 36 View view = new AndroidBuilder().build(this) { relativeLayout(width: MATCH_PARENT, height: MATCH_PARENT, padding: [dp(64), dp(16)]) { textView(width: MATCH_PARENT, height: dp(20), text: R.string.hello_world) } } • Builders for views • Experimental • https://github.com/karfunkel/grooid-tools

Slide 37

Slide 37 text

#s2gx #groovylang @CedricChampeau Potential issues 37 • Performance of dynamic Groovy on low end-devices • Use @CompileStatic whenever possible • The infamous 64k method count • Use ProGuard! • Tooling support • Groovy not fully supported by Android Studio • Google support • Android Gradle plugin updates are very frequent

Slide 38

Slide 38 text

#s2gx #groovylang @CedricChampeau “Best of all, I expect to try to update Android Studio right before the talk, so I have the latest possible version in the so­called Canary channel. What could possibly go wrong?” Ken Kousen, September 10th, 2014

Slide 39

Slide 39 text

#s2gx #groovylang @CedricChampeau Other ideas 39 • Dagger-like dependency injection framework? • Data binding APIs • Improved reactive APIs • You can already use Reactor or RxJava

Slide 40

Slide 40 text

#s2gx #groovylang @CedricChampeau Future is now! 40 • New York Times next app will be written in Groovy!

Slide 41

Slide 41 text

41 @CedricChampeau melix http://melix.github.io/blog