When using multi-dex, gradle will spawn a number of additional dexing tasks in parallel and this makes your build OOM since it requires more than 4G RAM in total.
To stop this happening, you need to pass a few args to gradle build.
./gradlew build -PpreDexEnable=false -Pcom.android.build.threadPoolSize=1 -Dorg.gradle.parallel=false -Dorg.gradle.jvmargs="-Xms512m -Xmx512m"