Finally found a solution for this by adding this line to gradle.properties. org.gradle.jvmargs=-Xmx4608m