Roy Clarkson

Roy Clarkson

Recent Blog posts by Roy Clarkson

Updated Maven Support for Android Projects

Engineering | November 07, 2011 | ...

It has been a while since my initial posts on using Maven with Spring Android, and the state of the tools has changed somewhat. With the recent release of the SDK for Android 4.0 and revision 14 of the Android Development Tools (ADT), Google made several changes that impacted the third party Maven plugin support for building Android projects. Fortunately, the teams behind the Android Maven Plugin, and the Android Configurator for M2E have been hard at work to support these latest changes in the Android tools. If you have tried to run the Spring Android Samples recently, then you probably noticed the projects would not build. In this post, I will illustrate the configuration changes necessary to utilize the latest Maven plugins and Eclipse support in the Spring Android Showcase project.

Android Maven Plugin

As of version 3.0.0-alpha-11, The Maven Android Plugin now has a new name, and is now called the Android Maven Plugin. The latest version, 3.0.0-alpha-13 fixes the build issue with Android SDK r14.

Update the Plugin

To update your plugin version, make the following changes to the artifactId and version.


<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>3.0.0-alpha-13</version>
    <configuration>
        <sdk>
            <platform>${android-platform}</platform>
        </sdk>
        <deleteConflictingFiles>true</deleteConflictingFiles>
        <undeployBeforeDeploy>true</undeployBeforeDeploy>
    </configuration>
    <extensions>true</extensions>
</plugin>

Once modified, you can now run maven from the command line to build your project.

mvn clean install

Potential Build Errors

It is possible that your project will receive a few build errors when you try to build with the new plugin.

java.lang.OutOfMemoryError

When I updated the Spring Android Showcase client, the build failed with the following:

[INFO] UNEXPECTED TOP-LEVEL ERROR:
[INFO] java.lang.OutOfMemoryError: Java heap space
[INFO] 	at com.android.dx.ssa…

Clean Code in Android Applications

Engineering | August 26, 2011 | ...

Let's say you wake up one morning, and think, "Hey, I'm going to build an Android app today." First off, good choice! As of the end of June, 500,000 Android devices were being activated every day, outpacing even the iPhone. That means there is a large, potential audience for your app. Additionally, Android is built with Java. This may not seem like a big deal, but I have worked in Objective-C on the iOS platform for a few years, and while I am now quite comfortable with it, the iOS SDK offered a steeper learning curve than I experienced with Android. Android just felt more accessible when I first started working with the Android SDK. That said, there are some clear differences from any other Java application you have built in the past, and I'll go over some of those in the first section.

So moving forward in time, you have completed your first app, and have submitted it to the Android Market. Congratulations are in order, as your friends are all downloading your app and tweeting about it. Now it is time to start on your second app. You spend a few days, and suddenly realize that you are starting to reuse code from your first app, which in itself is not a bad thing. Code reuse can be valuable. But you notice there is a lot of boilerplate code that tends to be repeated often, and that can be distracting from focusing on your business logic. Fortunately, there are some ways to improve upon this.

In this blog post, I will provide an overview of Android and the application lifecycle, and discuss some of the limitations imposed by the framework. I will also review a few of the techniques and third party projects that can help you clean up your Android code, and focus on what you want to achieve with your app.

Android Overview

Let's begin with a brief overview of how Android works. Android applications (apps) are built using Java, and compiled to class files. The class files are then compiled into the Dalvik Executable (DEX) format, so they may run on the Dalvik virtual machine used by Android. After conversion to DEX format, the class files are zipped to an Android Package (APK) for distribution to devices. Because of the use of the DEX format, the Dalvik VM is not a true Java Virtual Machine, since it does not operate on Java byte code. Additionally, the Dalvik VM is based on a subset of the Apache Harmony project for its core class library. This means that many of the classes and methods to which you are accustomed in Java SE are available, but certainly not all. I have found the API reference on the Android developer web site to be an invaluable resource for reviewing these differences.

By default, each Android application is assigned a unique Linux user ID by the Android operating system. When started by the system, an application runs in its own Linux process, within its own virtual machine (VM). The system manages the starting and shutting down of this process when needed. As you can guess, this means that each application runs in isolation from the other running applications. When installed, an app can request permission to access hardware features or interact with other applications. The user elects to grant these permissions to the app or to not install it. The permissions required or requested by an app are defined in each app's Android Manifest file. This is an XML file that lists all the components of the app, and any settings for those components. The four types of application components are activities, services, content providers, and broadcast receivers. For the purposes of this post, I will be focusing on activities.

Activities basically represent a single screen of an Android application. For example, a Twitter app may have a login screen, a screen with a list of tweets, and a screen for authoring a new tweet. Each of these screens represent different activities within the application. As a developer you never instantiate an activity object yourself. Activities are activated by sending an asynchronous message called an Intent, as seen in the example below.


startActivity(new Intent(context, HomeActivity.class));

When startActivity(Intent intent) is called, the system either creates a new instance or reuses an existing one in order to display the activity to the user. The important point is that the system controls the starting and stopping, and creation and destroying of the application and each activity. If you want to interact with this process, then the application and activity classes provide methods for different lifecycle events that you can override in a subclass.

Dependency Injection

The Spring Android project recently reached its fourth milestone release. With that release we have continued to improve upon the RestTemplate and Spring Social support for Android, which simplifies the process of making RESTful HTTP requests and accessing REST APIs secured by OAuth. And while we believe these are valuable additions for Android development, some developers have asked questions regarding the possibility of dependency injection support in Spring Android, because as you are probably aware, the Spring Framework already provides a popular Inversion of Control (IOC) container for enabling dependency injection in enterprise Java applications. Early in the Spring Android planning stages, dependency injection support was identified as a potential candidate for inclusion in the project. At that point, it was unclear what that support would entail, and how it would be implemented. Because of this, I began the process of researching and investigating the possible methods available for, and limitations of, performing dependency injection in Android.

Well, what is dependency injection? If you ask two different developers, you may get two different answers. You might hear about IOC, XML files, annotations, or some other implementation detail. In reality, dependency injection is simply a technique to reduce coupling by handing an object what it needs to work, rather than having the object reach out into its environment. That sounds easy enough, and you might be thinking to yourself you can already get this with class constructors and setter methods, which is completely true. However, recall from the overview section above, the Android system drives the application lifecycle, so the way we can do this is limited.

The Android Way

Without using any third party libraries, it is rather easy to pass a dependency to an Activity. As discussed earlier, the system creates the application instance. So by extending application, you can effectively create a singleton dependency instance, which can then be accessed by any of the activities in the app.


public class MainApplication extends Application  {

    private MyService service;

    @Override
    public void onCreate() {
        super.onCreate();
        service = new MyServiceImpl();
    }

    public MyService getMyService() {
        return this.service;
    }
}

The activity class has a method called getApplication() which returns a reference to the application object that owns the activity. We simply cast it to MainApplication, and we can access the getter method for the MyService. Of course, the activity now has to "know" about the application, which might seem like a disadvantage. But remember, the activity already knows about its application. The method is built in.


public class MainActivity extends Activity  {

    private MyService service;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MainApplication app = (MainApplication…

Spring Android 1.0.0.M4 Released

Releases | July 21, 2011 | ...

Dear Spring Community,

We are pleased to announce that the fourth milestone release of the Spring Android project is now available!

Spring Android supports usage of the Spring Framework in a Android environment. The 1.0.0.M4 release focuses on updating support for the latest Spring Social release in native Android applications, as well as providing enhancements to Rest Template. This includes:

  • Support for Spring Social 1.0.0.RC1, and Spring Security 3.1.0.RC2 through the Spring Android Auth module, which includes a SQLite datastore for persisting OAuth API connections.
  • Updated RestTemplate (client) support, now at the level of Spring Framework 3.1.0.M2.
  • Added gzip compression support in RestTemplate
  • Added support for Google's Gson JSON parsing library. The Gson library is smaller than Jackson, however Jackson has faster performance.

Spring Android is supported in Android version 2.1 (API Level 7) and higher.

To get the software, download the release distribution, or simply add the maven artifacts to your project. To see the features live, check out the spring-android-showcase (updated for 1.0.0.M4).

In addition to the reference guide, Roy Clarkson has authored two blog posts to help you get started developing Android applications:

If you're building native Android applications, we invite you to collaborate with us on the Spring Android project!

Spring Android 1.0.0.M3 Released

Releases | May 10, 2011 | ...

Dear Spring Community,

We are pleased to announce that the third milestone release of the Spring Android project is now available!

Spring Android supports usage of the Spring Framework in a Android environment. The 1.0.0.M3 release focuses on extending the use of Spring Social in native Android applications. This includes:

  • Support for Spring Social 1.0.0.M3 through the new Spring Android Auth module, which includes a SQLite datastore for persisting API connections. Spring Social is currently supported in Android version 2.3.1 (API Level 9) and higher.
  • Updated RestTemplate (client) support, now at the level of Spring Framework 3.1.0.M1.

To get the software, download the release distribution, or simply add the maven artifacts to your project. To see the features live, check out the spring-android-showcase (updated for 1.0.0.M3):
git clone git://github.com/SpringSource/spring-android-samples.git; cd samples/spring-android-showcase; more README

In addition to the reference guide, Roy Clarkson has authored two blog posts to help you get started developing Android applications:

If you're building native Android applications, we invite you to collaborate with us on the Spring Android project!

Spring Android 1.0.0.M2 Released

Releases | February 09, 2011 | ...

Dear Spring Community,

We are pleased to announce that the second milestone release of the Spring Android project is now available!

Spring Android supports usage of the Spring Framework in a Android environment. The 1.0.0.M2 release focuses on extending the use of RestTemplate in native Android applications:

  • We now use the HttpComponents HttpClient 4 by default for all REST calls, which is a native HTTP client on the Android platform. This replaces the use of the Commons HttpClient 3 from the first milestone.
  • We have added Object-to-XML marshaling support through the use of the Simple XML serialization library. Simple has a small footprint and is compatible with Android.
  • RSS and Atom Feeds are now supported through the Android Rome Feed Reader. Android Rome is a port of the popular Rome library that is compatible with Android.
  • Integration with the Jackson JSON Processor continues to provide first-class Object-to-JSON marshaling support.

Spring Android is available for download. If you are utilizing Maven with your Android project, simply add the following dependency:

<dependency>
    <groupId>org.springframework.android</groupId>
    <artifactId>spring-android-rest-template</artifactId>
    <version>1.0.0.M2</version>
</dependency>

<repository>
    <id>org.springframework.maven.milestone</id>
    <name>Spring Maven Milestone Repository</name>
    <url>http://maven.springframework.org/milestone</url>
</repository>

In addition to the reference guide, Roy Clarkson has authored two blog posts to help you get started developing Android applications:

A sample app with a README is available at github.com/SpringSource/spring-android-samples: git clone git://github.com/SpringSource/spring-android-samples.git

Work continues on the next Spring Android milestone, where we will we be integrating OAuth support for Android applications. If you're building native Android applications, we invite you to collaborate with us on the Spring Android project.

Spring Android and Maven (Part 2)

Engineering | February 09, 2011 | ...

In Spring Android and Maven (Part 1), I described how to build an Android application from the command-line using Maven. In this post, I will show you how to build an Android application with Maven dependency management from the Eclipse IDE. The application will also showcase the latest features in Spring Android 1.0.0.M2, which was released this week.

Overview

The Maven Android Plugin lets you build your Android applications with Maven and benefit from dependency management. Google's Android Development Tools (ADT) plugin allows you to develop and build Android applications within the Eclipse IDE. To get Maven dependency management within Eclipse, the Maven Integration for Android Development Tools plugin is required, which integrates m2eclipse, the ADT Plugin, and the Maven Android Plugin

Spring Android and Maven (Part 1)

Engineering | December 17, 2010 | ...

We recently announced the M1 release of Spring Android, and with that release some questions have arisen around how to build an Android application utilizing the Spring Android Rest Template and Spring Android Commons Logging libraries. Google provides several methods for compiling an Android application, including SDK command line tools, and the ADT (Android Development Tools) Plugin for Eclipse. Unfortunately, neither of these methods includes integrated dependency management support.

Overview

As Java developers we have come to appreciate tools such as Maven and Gradle for managing external dependencies. While traditional Java applications run in a JVM, Android applications run on the Dalvik virtual machine.  The Dalvik VM executes files in the Dalvik Executable (.dex) format.  It runs classes compiled by a Java language compiler that have been transformed into the .dex format.  A build tool will need to support this process if it is going to be able to compile a compatible Android application with dependencies.

There are basically two options for including external libraries in your Android application. The first is to manually copy the jars into the libs directory within your project and update the classpath within Eclipse. This is the simplest solution, and the one most supported by the ADT plugin. The disadvantage is that you have to manage the dependencies manually. Alternatively, a third party plugin such as the Maven Android Plugin can be utilized to automatically include the dependencies from a Maven repository.

In this post I will walk through the process of using the Android command line tools, Maven, the Maven Android Plugin, and Android Maven artifacts to compile a sample application that utilizes the Spring Android libraries, and deploy it to the Android emulator. After you have configured Maven, it is easy to create a build, deploy it to the emulator, run tests, and package the app for deployment to the Android Market. Before running the sample code, we will first highlight the configuration settings necessary in the pom.xml. The components used in this example are listed below.

Maven Configuration

This section covers the parts of a pom.xml that are required for developing with Spring Android and the Maven Android Plugin.

Maven Android Plugin

In order to use Maven to build an Android application, you will need to configure the Maven Android Plugin within your pom.xml file. Android applications are deployed to the device as an apk file, not a jar. You must specify this in the the packaging configuration.


<packaging>apk</packaging>

To configure the Maven Android and Maven Compiler Plugins in the build task, set the sdk platform to the desired level. In this example it is set to 9, which corresponds to Android version 2.3.1. The emulator avd value is the name of the AVD (Android Virtual Device) you defined in the AVD Manager. In this case, an AVD with the name "9", but the AVD can be named whatever you like, as long as it matches the name you specified when creating the AVD. This is a basic configuration for the plugin that is needed to build and run an Android application…

Spring Android 1.0.0.M1 Released

Releases | November 20, 2010 | ...

Dear Spring Community,

We are pleased to announce that the first milestone release of the Spring Android project is now available!

Spring Android supports usage of the Spring Framework in a Android environment. The defining feature of the 1.0.0.M1 release is RestTemplate support that provides a robust REST client to use in native Android applications.

To get you started, Keith has posted a Spring into Mobile Application Development blog, which provides insight into the project.

This milestone is the first in an exciting road ahead. If you're building native Android applications that communicate with Spring web apps, we invite you to join us in the development of the Spring Android project!

Spring Mobile 1.0.0.M1 Released

Releases | November 19, 2010 | ...

Dear Spring Community,

We are pleased to announce that the first milestone release of the Spring Mobile project is now available!

Spring Mobile provides extensions to Spring MVC that aid in the development of cross-platform mobile web applications. The defining feature of the 1.0.0.M1 release is support for server-side device detection.

To get you started, Keith has posted a Spring into Mobile Application Development blog, which provides insight into the project.

This milestone is the first in an exciting road ahead. We invite you to get involved in Spring Mobile development and look forward to your feedback!

Get ahead

VMware offers training and certification to turbo-charge your progress.

Learn more

Get support

Tanzu Spring offers support and binaries for OpenJDK™, Spring, and Apache Tomcat® in one simple subscription.

Learn more

Upcoming events

Check out all the upcoming events in the Spring community.

View all