Xtending Android development

Android is an amazing operating system that combines technical innovation with high popularity among the end-consumers. In spite of fragmentation, there is a large amount of developers across the world who enjoy developing applications for Android. Today, most Android applications are written in Java. Unfortunately, Java is known to be deficient in some modern language concepts and idioms like conciseness, declarativity, modularity mechanisms, the map/reduce pattern or immutable data. Wouldn’t it be great if there was a more advanced programming language for Android development? Luckily, there is one and it integrates seamlessly with Java. This language is Xtend.

Xtend is a statically-typed programming language that compiles to Java. Xtend has a clear and concise syntax. Although lambda expressions will be finally introduced in Java 8, Xtend’s syntax for them is significantly more readable. Another feature that makes Xtend a functional language is the absence of statements. Code written in Xtend is executed by evaluating expressions. Operator overloading in form of an operator-to-method mapping makes possible to fully customize operator semantics. Extensions methods allow for enhancing closed types with new functionality. Although the language is statically typed, its polymorphic method invocation combined with the use of extension methods brings duck typing to Xtend. Other powerful features like the type-based switch expression, shorthand property access and local type inference make programming in Xtend a real fun. But what probably even more important than the language features is the full, bidirectional interoperability with Java, a low learning curve for Java developers, comprehensible and efficient generated Java source code and a great IDE support. All this makes Xtend ready for immediate use in every environment where Java and Eclipse are used for development. If you are new to Xtend, check out its documentation.

In this tutorial, we will write an Android application in Xtend, build an APK and run it on the Android emulator. We will implement a simple application called Is Following? which let us check if a Twitter user follows another Twitter user.

To be able to follow the tutorial, you need to be acquainted with Android development and the Eclipse IDE.

Setting up the IDE

First, we’ll install and configure the IDE and create a project. You can skip the first three steps, if you already have an Eclipse installation that you use for Android development.

Create a new Android Application Project with only one blank activity MainActivity.

The compiler compliance level of the project needs to be set to 1.6.

Creating a layout

Create a simple layout that consists of:

an editable field for each of both Twitter names,

a button to start the request and

a text view that will show a result.

Writing the application

Our application will have an activity to interact with the user and a communicator which will send requests to Twitter and process its responses. The communication with Twitter will be performed asynchronously. Let’s implement these components.

Delete the class MainActivity.java that was automatically generated through the project creation.

To create a new Xtend class, select New -> Xtend Class from the project context menu in the Package Explorer.

On the first page of the creation wizard, type MainActivity in the Name field and select android.app.Activity as a superclass.

Click Finish.

The class we’ve just created will show an error:

Mandatory library bundle 'org.eclipse.xtext.xbase.lib' 2.3.0 or higher not found on the classpath.

Use the available quick fix to add Xtend libraries to the classpath.

MailActivity will have two methods: onCreate(Bundle) and showResult(Object). The first method onCreate(Bundle) prepares the content view and registers the click listener callback of the Check button. In the callback, we initialize the communicator, use its method isNetworkAvailable(Context) to test if network is available and execute the communicator asynchronously. The second method showResult(Object) updates the UI with the response received from the communicator by setting the text value in the result text view.

Create a new Xtend class TwitterCommunicator as a subclass of android.os.AsyncTask. The subclass overrides two methods: doInBackground(Params...) and onPostExecute(Result). In doInBackground(Params…) method, we create a new Twitter URL from both Twitter names passed in as parameters. After that, we open an HTTP connection and read the input stream from it. The second method passes the data read from the input stream to the main activity.

To avoid tight coupling between the activity and the communicator, we will make use of the delegate pattern. Create a new Xtend class AsyncDelegate. This time it will be an interface, so replace the keyword class with a keyword interface in the newly created Xtend class. The interface declares only one method showResult(Object). We will call this method from the post execute callback of TwitterCommunicator.

Now, when we are done with writing source code, add permissions android.permission.INTERNET and android.permission.ACCESS_NETWORK_STATE to the Android manifest and let’s build an APK and run it on an AVD.

We need to tell ADT how to export the Xtend libraries. Choose Properties -> Java Build Path from the project context menu and go to the Order and Export tab. Select the checkbox beside Xtend library.

After closing the Properties window, you can launch the application on an existing AVD or a device.

Where to go from here

Now that you know how to use Xtend for Android development, go and create awesome Android apps in this sophisticated programming language!

Don’t hesitate to let me know if anything of the above is confusing to you or if you’d like me to go into more detail.

Support my work

Very interesting. Thanks. However, the twitter returns the following message:
{“errors”:[{“message”:”The Twitter REST API v1 is no longer active. Please migrate to API v1.1. https://dev.twitter.com/docs/api/1.1/overview.”,”code”:64}]}