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:
Android- Error:Execution failed for task ':app:transformClassesWithDexForRelease'