Clean Code in Android Applications

Engineering | Roy Clarkson | 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…

This week in Spring: August 23rd, 2011

Engineering | Josh Long | August 24, 2011 | ...

Welcome to another edition of "This Week in Spring" Things are moving fast and furious as we near next week's VMworld 2011. I want to invite any attendees to visit your expert technologists at the VMWorld Spring booth. Let me know if you read this weekly roundup. Lots to talk about this week, so let's get to it!

    <li>The preliminary session schedule has been published for <a href="http://www.springone2gx.com">SpringOne 2GX 2011</a>. This year's show is going to be another fantastic mix of deep technical content, cutting edge development and the absolute best place to learn about everything in the Spring universe. Be sure to <a href="http://springone2gx.com/conference/chicago/2011/10/register">register now</a>!</li>
    
    <LI> <a href="http://static.springsource.org/spring/docs/3.0.6.RELEASE/changelog.txt">Spring 3.0.6's was just released!</a>   		 
    	 This release addresses over 50 minor issues and includes…

Micro Cloud Foundry for Spring Developers

Engineering | Josh Long | August 24, 2011 | ...

Today VMware team released Micro Cloud Foundry, a complete, local version of the popular, open source Platform as a Service that lets developers run a full featured cloud on their Mac or PC. Using Micro Cloud Foundry developers can build end-to-end cloud applications locally, without the hassles of configuring middleware while preserving the choice of where to deploy and the ability to scale their applications without changing a line of code.

Micro Cloud Foundry supports Spring and Java, of course, but also provides runtime environments for Scala, Node.js, and Ruby so that you can release your inner polyglot programmer! Micro Cloud Foundry also provides many services like MongoDB, MySQL, and Redis with come ready to use immediately without having to do extensive installation and configuration. With built-in dynamic DNS support, developers can run their Micro Cloud Foundry wherever they happen to be working – whether at home, office or coffee shop – without any reconfiguration required. After creating and testing your application on Micro Cloud Foundry, you can easily deploy your…

Using Micro Cloud Foundry from Grails

Engineering | Peter Ledbrook | August 24, 2011 | ...

Back in April, VMware introduced Cloud Foundry to the world and with it came super-simple application deployment for Grails developers. Fast forward several months and now another piece of the jigsaw is in place: Micro Cloud Foundry. You can now have your own Cloud Foundry instance for testing or any other use case. And of course, it's incredibly easy to use from Grails.

So what is Micro Cloud Foundry? The following screencast gives you a brief overview of the product and then takes you through the process of downloading, installing and configuring it. At the end, you get to see how you can…

Countdown to Grails 2.0: Database Migrations

Engineering | Peter Ledbrook | August 17, 2011 | ...

One of the many nice features of Grails is the way it will automatically create your database schema for you from your domain model. Admittedly it's a feature of Hibernate that Grails uses, but still, it helps you get started very quickly with database-driven web applications without having to worry about the database schema.

What happens once your application moves to production? During development, losing the data in between server runs isn't a big issue. But you can't just drop the database in production. So that rules out the "create" and "create-drop" values for the dbCreate data source…

Chatting in the Cloud: Part 1

Engineering | Mark Fisher | August 16, 2011 | ...

Last week the availability of RabbitMQ as a service on Cloud Foundry was announced. Any application running on Cloud Foundry may now send and receive messages via a RabbitMQ broker that can be provisioned as a service with a single command (e.g. 'vmc create-service rabbitmq'). Instances of the messaging service may be shared between applications, and since RabbitMQ is a protocol-based broker, those applications may even be written in different languages. So, this is an exciting announcement for those interested in modular, polyglot, event-driven applications running in the cloud. I will be…

Configuring Spring and JTA without full Java EE

Engineering | Josh Long | August 15, 2011 | ...

Spring has rich support for transaction management through its PlatformTransactionManager interface and the hierarchy of implementations. Spring's transaction support provides a consistent interface for the transactional semantics of numerous APIs. Broadly, transactions can be split into two categories: local transactions and global transactions. Local transactions are those that affect only one transaction resource. Most often, these resources have their own transactional APIs, even if the notion of a transaction is not explicitly surfaced. Often it's surfaced as the concept of a session, a…

Beyond the FactoryBean

Engineering | Josh Long | August 10, 2011 | ...

I looked at what a basic FactoryBean is in my previous post. While FactoryBeans are important - and knowing what they do can help you navigate the framework more effectively - they're by and large no longer the recommended approach to the task as of Spring 3.0 and the imminent Spring 3.1.

The whole point of a FactoryBean is to hide the construction of an object - either because it's very complex or because it can't simply be instantiated using the typical constructor-centric approach used by the Spring container (maybe it needs to be looked up? Maybe it needs a static registry method?) Spring has also supported the factory-method attribute in the XML format. The Java configuration approach offers a conceptually similar (in practice, the result is the same) alternative, but features a more concise, type-safe alternative.

Spring 3.0 saw the introduction of Java configuration which lets you define beans using Java. For instance, to register a regular javax.sql.DataSource with Spring in XML, you will more than likely delegate to a properties file for the sensitive configuration information (like a database password) and use Spring to instantiate the javax.sql.DataSource, like this:


<beans ...>
	<context:property-placeholder location = "ds.properties" />

	<bean id = "ds" class = "a.b.c.MySqlDataSource">
	  <property name = "user" value = "${ds.user}"/>
	  <property name = "password" value = "${ds.password}"/>
	</bean>
</beans>

This is a simple bean, and translates naturally into Java configuration. It would look like this:

 
import a.b.c.* ;
	
@Configuration 
@PropertySource("ds.properties") 
public class MyConfiguration { 
    @Inject private Environment env ; 
	
    @Bean public MySqlDataSource ds(){ 
        MySqlDataSource ds = new MySqlDataSource () ; 
        ds.setUser( env.getProperty("ds.user") );
        ds.setPassword( env.getProperty("ds.password…

What's a FactoryBean?

Engineering | Josh Long | August 09, 2011 | ...

In this post, I'll look at Spring's org.springframework.beans.factory.FactoryBean<T> interface. The definition of this interface is:


public interface FactoryBean<T> {
  T getObject() throws Exception;
  Class<T> getObjectType();
  boolean isSingleton();
}

A FactoryBean is a pattern to encapsulate interesting object construction logic in a class. It might be used, for example, to encode the construction of a complex object graph in a reusable way. Often this is used to construct complex objects that have many dependencies. It might also be used when the construction logic itself is highly volatile and depends on the configuration. A FactoryBean is also useful to help Spring construct objects that it couldn't easily construct itself. For example, in order to inject a reference to a bean that was obtained from JNDI, the reference must first be obtained. You can use the JndiFactoryBean to obtain this reference in a consistent way. You may inject the result of a FactoryBean's getObject() method into any other property.

Suppose you have a Person class whose definition is thus:


public class Person { 
 private Car car ;
 private void setCar(Car car){ this.car = car;  }	
}

and a FactoryBean whose definition is thus:


public class MyCarFactoryBean implements FactoryBean<Car>{
  private String make; 
  private int year ;

  public void setMake(String m){ this.make =m ; }

  public void setYear(int y){ this.year = y; }

  public Car getObject(){ 
    // wouldn't be a very useful FactoryBean 
    // if we could simply instantiate the object…

This week in Spring: August 2nd, 2011

Engineering | Josh Long | August 03, 2011 | ...

Welcome to another edition of "This Week in Spring." August is well underway and soon, at the end of August, VMworld 2011 will be upon us. Shortly thereafter, SpringOne will be here. It's going to get hot and heavy very quickly, so get ready! This week's "This Week in Spring" has a lot of interesting content from Gordon Dickens, of Chariot Solutions. Thanks Gordon for all the good reading!

  1. Rod Johnson - Spring's founder and thought leader - did a keynote at TheServerSide earlier this year. This post relays some of the content of that keynote, including his thoughts on cloud computing, SOA, and more. Check it out.
  2. <LI> 
    	The video of the recent webinar, "<A href="http://www.springsource.org/node/3194">What's New in Apache Tomcat 7</a>," is now available on the <a href="http://www.youtube.com/SpringSourceDev">SpringSourceDev YouTube channel</a>.   
    </LI> 
    <LI>Luke Taylor has some great content on how to <a href="http://blog.springsource.com/2011/08/01/spring-security-configuration-with-scala/">configure Spring Security with the Scala DSL</a> he's been developing. Check it out! 
    </LI> 
    <LI> 
    	<a href= "http://www.springsource.org/node/3192">Spring Data JDBC Extensions with Oracle Database Support</a>…

Get the Spring newsletter

Stay connected with the Spring newsletter

Subscribe

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