Better Android applications with Groovy and fastsnail

Gilles Grousset
Hack Like a Pirate
Published in
3 min readNov 3, 2014

After a long sleep… I’m back 🙂

Actually, I was not sleeping at all. Let’s say I was busy doing things… One of them was actually thinking how I could write better Android applications.

And I came up with that…

The beginning

I wrote my first real Android application 3 years ago, and while I started learning Android, the first thing I thought was : welcome back to 2002 !

Do you remember Swing with Java ? Many, many lines of code to display a simple text view in a basic window ? I felt exactly like I was writing a Swing application with Android, and I got really frustrated because of that.

To sum up:

  • No suitable build tool (outside Eclipse which is not a build tool, and maven with it’s crapy apkLib dependencies)
  • Every single boring stuff is made by hand: layout loading, view binding… And my favorite : Parcelable !
  • Unit test writing hell

Gradle lead the way…

Then one day, a miracle append: Google decided to support Gradle, a pretty cool build tool.

It was now possible to build an Android application with a single command! That really changed my mind…

But I still felt inconfortable with all the Java lines I had to write, until this summer.

A Groovier world

This summer was the Groovier ever for me.

First, Cédric Champeau from Pivotal wrote a Gradle plugin to present the official Android support in Groovy coming with version 2.4.

Then, I read this New York Time blog post where a developer explains how their Android app is being re-written from scratch using Groovy + Dagger + rxJava + OkHttp.

If you don’t know those libraries:

  • Dagger is a dependency injection library
  • rxJava is a Reactive Extensions Java implementation library (particulary useful for dealing with async programming)
  • OkHttp is a fast HTTP client library

This post really inspired me, so I decided to test the setup described in it… And it didn’t work. Actually, it is impossible to make it work because Dagger uses apt to generate source code at compile time, and that does not work with Groovy code.

I was so disappointed that I decided to write my own library to fix what was not working.

fastsnail

The library I wrote is named fastsnail.

It is designed to work with Groovy and Gradle (of course !).

Here are the features (for the moment):

  • Dependency injection: as Dagger was not working with Groovy, I wrote my own (but not as sophisticated as Dagger) dependency injection mechanism. Instead of using apt, I used Groovy AST Transformationsto avoid runtime overheads.
  • View and layout injection with annotations on Activities and Fragments.
  • Ansynchronous manager to help dealing with background processing / UI Thread.
  • A list Adapter working with Groovy closures.

Some code snippets for a better understanding:

// Dependency injection@CompileStatic 
class HomeActivity extends Activity {
@InjectComponent MyManager myManager
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
myManager.sayHello()
}
}
// View and layout injection@CompileStatic
@InjectLayout(R.layout.activity_home)
class HomeActivity extends Activity {
@InjectView(R.id.hello_text_view) TextView helloTextView
protected void onCreate(Bundle savedInstanceState) {
// At this stage layout is set and views are injected
helloTextView.setText('Hello world from an Activity !!')
}
}
// Ansynchronous programming@CompileStatic
class HomeActivity extends Activity {
@InjectComponent AsyncManager asyncManager
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) asyncManager.runAsync {
// Long processing here ...
return Result }.subscribe({Result result -> println "Result =
$result" }, {Throwable e -> println "Something failed...
$e.message" })
}
}
// List adapterFSEfficientAdapter adapter = new FSEfficientAdapter(activity, R.layout.item_menu)
adapter.addAll([new MenuItem('HOME', 'Home'), new MenuItem('REPO', 'Repository info')])
adapter.onBindViewHolder = {FSEfficientAdapter.ViewHolder holder, MenuItem item, position -> TextView textView = holder.itemView as TextView textView.setText(item.label) }

Want to get more information ? Then check out sources and documentation at https://github.com/zippy1978/fastsnail

Any contribution is welcomed !

Originally published at https://blog.grousset.fr on November 3, 2014.

--

--