简体   繁体   中英

Building an Android multidex application using gradle without Android Studio (on a build server)

I'm in the process of setting up a CI server running TeamCity.

The server is running openjdk8, I'm using build tools 23.0.2

I have a multidex Android app that I am able to build locally using assembleMyFlavorRelease. This same build fails on my TeamCity agent on app:transformClassesWithDexForMyFlavorRelease

The stacktrace shows java exiting with 1 though I can't seem to find why

[Gradle failure report] Execution failed for task ':app:transformClassesWithDexForMyFlavorRelease'.
[Gradle failure report] >
com.android.build.api.transform.TransformException:
com.android.ide.common.process.ProcessException:
java.util.concurrent.ExecutionException:
com.android.ide.common.process.ProcessException: 
org.gradle.process.internal.ExecException: 
Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' finished with non-zero exit value 1

Exception is:
[Gradle failure report] org.gradle.api.tasks.TaskExecutionException:
Execution failed for task ':app:transformClassesWithDexForMyFlavorRelease'.
....
Caused by: 
java.lang.RuntimeException: com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' finished with non-zero exit value 1
....
Caused by: 
com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/usr/lib/jvm/java-8-openjdk-amd64/bin/java'' finished with non-zero exit value 1

My app.gradle looks like this

productFlavors {

    final def MIN_SDK = 19
    final def TARGET_SDK = 23

    myFlavor {
        minSdkVersion MIN_SDK
        targetSdkVersion TARGET_SDK
        multiDexEnabled true
    }

With these dex options

dexOptions {
    javaMaxHeapSize "4g"
    preDexLibraries false
 }

Compiling with

compileSdkVersion 23
buildToolsVersion '23.0.2'

using multidex 1.0.1

dependencies {
  compile 'com.android.support:multidex:1.0.1'
}

The build agent completes both

:app:collectMyFlavorReleaseMultiDexComponents
:app:transformClassesWithMultidexlistForMyFlavorRelease

Before failing on

app:transformClassesWithDexForMyFlavorRelease

Running with info reveals this before exiting

[org.gradle.launcher.daemon.client.DaemonClient] Received result Failure[value=org.gradle.initialization.ReportedException:
org.gradle.internal.exceptions.LocationAwareException: Execution failed for task ':app:transformClassesWithDexForMyFlavor'.] 
from daemon DaemonInfo{pid=2200, 
address=[a10b64d0-94c0-40e9-8b5d-b5a5bbb171c4 port:46291, addresses:[/0:0:0:0:0:0:0:1%lo, /127.0.0.1]], idle=false,
context=DefaultDaemonContext[uid=968a9ee5-e6d4-4cba-a2a5-ce768ecbfe44,
javaHome=/usr/lib/jvm/java-8-openjdk-amd64,
daemonRegistryDir=/root/.gradle/daemon,pid=2200,idleTimeout=120000,
daemonOpts=-XX:MaxPermSize=512m,-XX:+HeapDumpOnOutOfMemoryError,-Xmx2048m,-Dfile.encoding=UTF-8,
-Duser.country=US,-Duser.language=en,-Duser.variant]

Any help or direction at this point would be greatly appreciated!

Turns out it was a resource issue, I was running my builds on an AWS AMI without no swap and the dexer was running out of ram. It also didn't help that I was setting javaMaxHeapSize to 4g on an AMI with only 2g of ram.

To fix this I first reduced the javaMaxHeapSize to 2g

dexOptions {
    javaMaxHeapSize "2g"
    preDexLibraries false
}

2g seems to be enough mempry for the dexing to avoid throwing a java.lang.OutOfMemoryError: GC overhead limit exceeded in my build. My project includes the Google Analytics library and had issues building with the default dex heap size.

On the AMI side I created a 2G swapfile, I'm running builds on an ubuntu AMI running on a T2.Small.

On my AMI:

sudo fallocate -l 2G /swap
sudo mkswap /swap
sudo swapon /swap

To persist after a reboot I added the following line to /etc/fstab

/swap    none    swap    sw    0    0

After this my T2 is now able to build without running into any errors.

Hope this helps someone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM