Search code examples
androidapache-commons-mathjack-compiler

How can I get around Android's jack compiler giving Java heap space exceptions?


I recently tried to add the Apache Commons' Math library, but ran into the following exception during compilation:

Error:Execution failed for task ':app:transformClassesWithPreJackPackagedLibrariesForDebug'. com.android.sched.scheduler.RunnerProcessException: Error during 'CodeItemBuilder' runner on 'static void org.apache.commons.math3.util.FastMathLiteralArrays.() (FastMathLiteralArrays.java:28-5102)': Java heap space

I think the problem is similar to the problems experienced in these questions:

Error:Execution failed for task ':app:transformClassesWithMultidexlistForDebug'. > java.lang.UnsupportedOperationException (no error message)

Android- Error:Execution failed for task ':app:transformClassesWithDexForRelease'


Solution

  • As the exception states, there's a problem with the available heap space. To increase the heap space size allocated to the Jack compiler, you have to run in out of process and then apply the appropriate dex option:

    defaultConfig {
        jackOptions {
            enabled true
            jackInProcess false
        }
        dexOptions {
            javaMaxHeapSize "2048M"
        }
    }
    

    With these, I was able to get past the compilation error.