CALL +44 (0)20 7183 3893
agile, geeky, and eccentric Dev Blog

Wednesday, January 18, 2012

Website Relaunch

After three months, approximately 100 litres of various caffeinated beverages and about 5000 lines of code, our new baby has been born! Rebuilding an existing website, particularly in the midst of working diligently on innumerable client projects, is never an easy task. The new website has been a labour of love for the development team, and we are as pleased as proud parents to be able to show it off at last.

The new site has a new design and layout that will make it easier for you to find the information you are looking for. We have also written brand new content to let you know more about us and what we can do for your business.

This release also marks the development team handing over the keys to the site to the relevant business owners. Integrating with Google’s authentication mechanism, the content management system we’ve produced will allow the rest of Cloudreach to update their specific areas of the business. No longer will everyone need to grab one of the developers every time a change is needed. To enable this, we have built the new site on top of a customised version of Django CMS. As always, we demonstrate our confidence in the cloud by hosting the site in Amazon Web Services.

We would like to thank the hard work of everyone who has contributed their time and writing to the new site, but in particular we’d like to thank Carol Rashti — for her great work organising the process and editing the content — and Antoine Collet — for all the awesome effort he has put into development for the project. Their endeavours have put us on course for the future, and we have a lot more planned.

Tuesday, January 10, 2012

Dealing with OutOfMemory Errors when using Maven and Android

When adding new dependencies to an Android project managed by Maven you may sometimes come across Out of Memory (java.lang.OutOfMemoryError) errors of the sort shown below:
UNEXPECTED TOP-LEVEL ERROR:
java.lang.OutOfMemoryError: Java heap space

For instance, I got the same error when I introduced OrmLite, a lightweight open-source ORM,  as a dependency in my Android project.

If you were to, at that point, read the error logs carefully, you will notice that such an error occurs in the DEX phase, i.e., the stage during the building a packaged Android application at which the Android Maven Plugin converts compiled Java classes into the Android Dalvik Executable (dex) format. During the course of this, the Java Virtual Machine (JVM) runs out of heap memory, which results in the aforementioned error that can flummox the best of us.
Typically, when running a Java application, the heap memory can be increased by passing the following arguments to the JVM:
-Xms<starting heap size>m
-Xmx<maxium heap size>m

By default, the maximum heap size is 128M; this can be insufficient for some projects. In order to view the maximum heap size, just run the following code:
public class JavaHeapSize {
    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime());
    }
}
However, things aren’t quite so simple when using Maven because you do not directly invoke the JVM.

While there were several answers floating around the Internet on how to pass arguments to the JVM via Maven, none of them worked for me, until I recieved an answer for my question from Roberto Tyley on StackOverflow.

I have reproduced the gist of his answer here: you can pass any argument to JVM using the Android Maven Plugin if you add the following lines to the configuration subsection of the Android Maven Plugin section of your pom.xml file:
<dex>
    <jvmArguments>
        <jvmArgument>-Xms256m</jvmArgument>
        <jvmArgument>-Xmx512m</jvmArgument>
    </jvmArguments>
</dex>

In the example above, we pass the necessary arguments to the JVM that results in an increase in the heap size.

However, here’s a gotcha! In order to do this, you have to use version 3.0.0 of the Android Maven Plugin because versions prior to this do not support the <dex> tag.

Another important point to note at this juncture is, from this version on, the plugin was renamed from maven-android-plugin to android-maven-plugin. Thus, you must have the following in your pom.xml:
<plugin>
    <groupId>com.jayway.maven.plugins.android.generation2</groupId>
    <artifactId>android-maven-plugin</artifactId>
    <version>3.0.0</version>
<configuration>
    <dex>
        <jvmArguments>
            <jvmArgument>-Xms256m</jvmArgument>
            <jvmArgument>-Xmx512m</jvmArgument>
        </jvmArguments>
    </dex>
    <!-- some other parameters -->
</configuration>
<!-- some other parameters -->
</plugin>

Mike Borozdin
Software Developer, Cloudreach Limited
Pontus is ready and waiting to answer your questions