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.
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>mBy 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:
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:
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

No comments:
Post a Comment