cordova :transformClassesWithDexForReleaseDex: Error converting bytecode to dex

Viewed 353

I have been battling with this issue the past couple of days, and tried several suggested fixes. One of them, to use Jack, works but

  1. doubles the length of time and memory required to build
  2. Jack is deprecated

I also tried updating the Java version specified in CordovaLib/build.gradle but that doesn't seem to solve the problem either.

I am running cordova 7.1.0, upgrading is likely not a trivial option. And java 1.8.0

Observations

The following command fails to build:

cordova --release build android --device

with

:transformClassesWithDexForReleaseDex: Error converting bytecode to dex:
Cause: Dex cannot parse version 52 byte code.
This is caused by library dependencies that have been compiled using 
Java 8 or above.
If you are using the 'java' gradle plugin in a library submodule add 
targetCompatibility = '1.7'
sourceCompatibility = '1.7'
to that submodule's build.gradle file.
    UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
    at com.android.dx.command.dexer.Main.processClass(Main.java:775)
    at com.android.dx.command.dexer.Main.processFileBytes(Main.java:741)
    at com.android.dx.command.dexer.Main.access$1200(Main.java:88)
    at com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1683)
    at com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
    at com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
    at com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
    at com.android.dx.command.dexer.Main.processOne(Main.java:695)
    at com.android.dx.command.dexer.Main.processAllFiles(Main.java:592)
    at com.android.dx.command.dexer.Main.runMonoDex(Main.java:321)
    at com.android.dx.command.dexer.Main.run(Main.java:292)
    at com.android.builder.internal.compiler.DexWrapper.run(DexWrapper.java:54)
    at com.android.builder.core.DexByteCodeConverter.lambda$dexInProcess$0(DexByteCodeConverter.java:173)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:748)
Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
    at com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:476)
    at com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
    at com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
    at com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
    at com.android.dx.command.dexer.Main.parseClass(Main.java:787)
    at com.android.dx.command.dexer.Main.access$1600(Main.java:88)
    at com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1722)
    at com.android.dx.command.dexer.Main.processClass(Main.java:773)
    ... 16 more

But, the following does build:

( cd platforms/android ; ./gradlew cdvBuildRelease -b build.gradle )

After that, the following also builds (even though previously it didnt)

cordova --release build android --device

Lastly, if I clean the build, I get back to the state where it doesn't build.

( cd platforms/android ; ./gradlew clean )
cordova --release build android --device

Not sure what to do next to get to the bottom of the issue.

1 Answers

Did you try changing this in your Gradle build like so? targetCompatibility = '1.7' sourceCompatibility = '1.8'

If you do a CLEAN and blow away all of your .class files so they re-compile to a 1.7 format you should be good to go.

Just make sure you do a clean AFTER changing the values in your Gradle build like shown above.

Related