#GroovyAndroid @CedricChampeau 4/47 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 • Multi-faceted language • OO, Imperative, functional, scripting, dynamic, static, … • Straightforward integration with Java
#GroovyAndroid @CedricChampeau 14/47 Groovy on Android: the problems • Groovy is primarily a dynamic language • Not everything done at compile time • Intensive use of reflection • Potentially slow invocation pathes • Battery? • Bytecode is different • Classes at runtime?
#GroovyAndroid @CedricChampeau 15/47 • Not all classes are available • java.bean.xxx very problematic • Multiple runtimes • Dalvik • ART • Behavior not the same as the standard JVM Groovy on Android: the problems
#GroovyAndroid @CedricChampeau 16/47 Groovy on Android: dex files • Dalvik VM = alternative bytecode • Groovy generates JVM bytecode • Translation done through dex • No native support for generating classes at runtime
#GroovyAndroid @CedricChampeau 20/47 Classes at runtime?! • Works, but very slow • Lots of I/O involved • What about ASMDex? • Same approach used by Ruboto • Nice proof of concept
#GroovyAndroid @CedricChampeau 22/47 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 • No performance issue!
#GroovyAndroid @CedricChampeau 23/47 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!
#GroovyAndroid @CedricChampeau 26/47 Requirements • Gradle • Android Studio • Or your favorite editor... • Groovy 2.4.0 (“grooid”) • A good tutorial on Android...
#GroovyAndroid @CedricChampeau 27/47 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
#GroovyAndroid @CedricChampeau 28/47 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 use a plugin: apply plugin: 'groovyx.grooid.groovy-android' • Supports both the application and library plugins
#GroovyAndroid @CedricChampeau 30/47 Then code! @CompileStatic @ToString(includeNames = true) @EqualsAndHashCode class Session { Long id Long speakerId Slot slot String title String summary List tags }
#GroovyAndroid @CedricChampeau 34/47 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)
#GroovyAndroid @CedricChampeau 37/47 SwissKnife • Combines ideas from Android Annotations and ButterKnife • Based on AST transformations • View injection • Threading model • Bundles management • Works with annotations to generate code
#GroovyAndroid @CedricChampeau 38/47 SwissKnife class MyActivity extends Activity { @ViewById(R.id.myField) TextField mTextField @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) contentView = R.layout.activity_main // This must be called for injection of views and callbacks to take place SwissKnife.inject(this) } }
#GroovyAndroid @CedricChampeau 39/47 SwissKnife @SaveInstance private int myInt // You can also set a custom tag to your variable @SaveInstance("MYSTRING") private String myString @Override void onCreate(Bundle savedInstanceState){ // Your previous code SwissKnife.restoreState(this, savedInstanceState) }
#GroovyAndroid @CedricChampeau 43/47 Potential issues • Performance of dynamic Groovy on low end-devices • Use @CompileStatic whenever possible • The infamous 64k method count • Use ProGuard and multidex support • Tooling support • Groovy not fully supported by Android Studio • Google support • Android Gradle plugin updates are very frequent
#GroovyAndroid @CedricChampeau 44/47 “Best of all, I expect to try to update Android Studio right before the talk, so I have the latest possible version in the socalled Canary channel. What could possibly go wrong?” Ken Kousen, September 10th, 2014
#GroovyAndroid @CedricChampeau 45/47 Other ideas • Dagger-like dependency injection framework? • Dagger 2 in the works... • Data binding APIs • Improved reactive APIs • You can already use Reactor or RxJava