Porting existing Java code to Android

by Ivan.Memruk  on  16. July 2010 07:27

One of the major reasons why Java became so popular is the vast amount of existing code such as libraries, examples and open source applications, that is available to Java developers. When you develop in Java, chances are most of the "chore" code that your application needs has already been written, and good chances are that the code that you need is open source and has a flexible license. Thus, a lot of redundant work is skipped and you can focus on the business logic of your specific application.

Apparently, the decision to use Java as the Android platform language was also based on this reason (as well as others). Not only the platform itself includes a number of ported libraries, both Java and non-Java, but you can engage Java code that you and other people wrote even before the platform was born, to benefit your Android app.

There are multiple situations and approaches of using existing Java code in your Android projects. This article is intended to make a short overview of your options, including instructions for configuring the environment and some tips about issues you might run into.

Case #1: Using a utility JAR

A very typical case is when you want to use a 3rd party library that solves a problem you don't want to solve again yourself. For example, let's say we want to include the very popular Apache Commons Lang library that includes many goodies such as StringUtils, WordUtils, ObjectUtils, ArrayUtils and a lot more.

The commons-lang library is distributed as a package that includes a JAR. As long as we are sure the JAR is compiled for Java 5.0 or earlier, we can safely add the JAR to the build path of our Android project in Eclipse - just like to any other Java project:

The Android build tools will silently recompile the JAR code into the Dalvik code and include it into your APK along with your compiled source.

Now we can do something nice with the commons-lang library's help, such as generate a random alphanumeric string of a given length:

public class TestActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        String randomString = RandomStringUtils.randomAlphanumeric(10);
        TextView textView = (TextView) findViewById(R.id.text_view);
        textView.setText("String generated by commons.lang: " + randomString);

And here's what we get:

In general, this is a very effective way to include existing Java libs into your Android project. However, this will not work for every case. Android is still a different platform from those that most frameworks and libraries were meant for. Some libraries might do something under the cover that will not work on Android, such as using the filesystem in a non-portable way or using some Java2D code. In such cases, since you're dealing with precompiled JAR files, you won't be able to fix the rough edges even though you would be able to do that if you had the source. So let's see how you can include existing Java code in its source form.

Case #2: Including source code

Obviously, you can just copy and paste the source code of the library that you need right into your Android project in Eclipse. However, since you're not going to focus on that code, it will just bother you all the time. In addition, it is likely to bring in some warnings that will turn your whole project yellow (do you hate warnings as much as I do?). Anyway, there is a way to separate 3rd party code from your main code tree, and you can also use it to separate some auxiliary, reusable pieces of code that you write yourself.

The trick is to create a new Eclipse project, but not an Android project - just a Java one. Be sure to set the compilation level to 5.0. Put the source into that new project and feel free to modify and configure it. After it compiles, you just add this Java project to the build path of your original Android project. The code will be automatically picked up and compiled to be included into your final APK.

In case you need to use Android API from the Java project, you can just add the android.jar file from the corresponding folder of Android SDK, for example \android-sdk-windows\platforms\android-1.5\android.jar for 1.5, to the build path:

This way you can keep your 3rd party and auxiliary code in a separate project so that it does not mix up with your main app code, and at the same time use the Android API in it.

Things to watch out for

While 80%-90% of all source code you will port into your Android apps will work out of the box, there are several major areas where Android is very different from the platforms most 3rd party code was written for. Be sure to watch out for:

  • Filesystem I/O: While Android is based on Linux, it has a very peculiar permission model that limits each app to its filesystem sandbox. In some cases, such as when you deal with the SD card, you can use the usual File and Directory classes, but when you deal with your application's internal data, you can never do that. This is why you have to watch your 3rd party code for filesystem operations it performs.
  • Swing: There is no Swing on Android. If it is used, or any of its pieces are used, you need to replace them with the corresponding Android APIs, or just remove the features that are based on them.
  • Native code: In many cases, you can port native code, for example common C-based Unix libraries, to Android. Stay tuned for a dedicated article on that subject!
  • Device usage: Stuff that deals with sound or graphics, such as Java 2D, needs to be rewritten too.

It's always reasonable to try to reuse existing, proven and tested code rather than re-invent the wheel. Don't hesitate to try porting existing code to Android before you decide to rewrite it yourself.

Ivan Memruk

Related articles

Porting C code: HQ4X pixel scaling algorithm on Android
In addition to situations where you want to improve performance of critical parts of your code by re...
Moving from Java Web development to Android
If you are a Java developer interested in learning more about Android, or if you're determined you...
Should Android market be more regulated?
Android is undeniably one of the hottest products available to customers living in this era of tec...

blog comments powered by Disqus
� 2010 WiseAndroid lightdir.com