vendredi 29 avril 2016

How to setup PowerMock for Android instrumentation tests (ART)

I want to use PowerMock in my instrumentations tests but I run into several problems.

I first tried to include de dependencies in the build.gradle

androidTestCompile ("org.powermock:powermock-api-mockito:$POWERMOCK_VERSION") {
    exclude module: 'hamcrest-core'
}
androidTestCompile "com.crittercism.dexmaker:dexmaker:$DEXMAKER_VERSION"
androidTestCompile "com.crittercism.dexmaker:dexmaker-dx:$DEXMAKER_VERSION"
androidTestCompile ("com.crittercism.dexmaker:dexmaker-mockito:$DEXMAKER_VERSION") {exclude group: 'junit' exclude group: 'org.hamcrest', module: 'hamcrest-core' }

And I got the following error:

> com.android.build.api.transform.TransformException: com.android.builder.packaging.DuplicateFileException: Duplicate files copied in APK mockito-extensions/org.mockito.plugins.MockMaker
    File1: /Users/nzylber1/.gradle/caches/modules-2/files-2.1/org.powermock/powermock-api-mockito/1.6.4/fe12509b7e9e49d25131f4155145748a31e42e40/powermock-api-mockito-1.6.4.jar
    File2: /Users/nzylber1/.gradle/caches/modules-2/files-2.1/com.crittercism.dexmaker/dexmaker-mockito/1.4/70892a94894462c1b35df3c8a77d21b7e843550b/dexmaker-mockito-1.4.jar

A google search led me to this post where the accepted answer doesn't provide a solution but mentions PowerMock can't work with Dalvik. But since recent devices and emulators run on ART, it doesn't seem relevant anymore. So I tried the method proposed in the second and unaccepted answer, which was to exclude manually the duplicate files from the PowerMock jar

AGPBI: {"kind":"error","text":"Error converting bytecode to dex:
Cause: java.lang.RuntimeException: Exception parsing classes","sources":[{}],"original":"UNEXPECTED TOP-LEVEL EXCEPTION:
java.lang.RuntimeException: Exception parsing classes
com.android.dx.command.dexer.Main.processClass(Main.java:752)
com.android.dx.command.dexer.Main.processFileBytes(Main.java:718)
com.android.dx.command.dexer.Main.access$1200(Main.java:85)
com.android.dx.command.dexer.Main$FileBytesConsumer.processFileBytes(Main.java:1645)
com.android.dx.cf.direct.ClassPathOpener.processArchive(ClassPathOpener.java:284)
com.android.dx.cf.direct.ClassPathOpener.processOne(ClassPathOpener.java:166)
com.android.dx.cf.direct.ClassPathOpener.process(ClassPathOpener.java:144)
com.android.dx.command.dexer.Main.processOne(Main.java:672)
com.android.dx.command.dexer.Main.processAllFiles(Main.java:574)
com.android.dx.command.dexer.Main.runMonoDex(Main.java:311)
 com.android.dx.command.dexer.Main.run(Main.java:277)
com.android.dx.command.dexer.Main.main(Main.java:245)
 com.android.dx.command.Main.main(Main.java:106)
Caused by: com.android.dx.cf.iface.ParseException: bad class file magic (00051607) or version (0000.0002)
com.android.dx.cf.direct.DirectClassFile.parse0(DirectClassFile.java:472)
com.android.dx.cf.direct.DirectClassFile.parse(DirectClassFile.java:406)
com.android.dx.cf.direct.DirectClassFile.parseToInterfacesIfNecessary(DirectClassFile.java:388)
com.android.dx.cf.direct.DirectClassFile.getMagic(DirectClassFile.java:251)
com.android.dx.command.dexer.Main.parseClass(Main.java:764)
com.android.dx.command.dexer.Main.access$1500(Main.java:85)
com.android.dx.command.dexer.Main$ClassParserTask.call(Main.java:1684)
com.android.dx.command.dexer.Main.processClass(Main.java:749)
... 12 more\n","tool":"Dex"}

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:transformClassesWithDexForDebugAndroidTest'.
> com.android.build.api.transform.TransformException: java.lang.RuntimeException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.ide.common.process.ProcessException: org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_77.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

where the most important line seems to be:

Caused by: com.android.dx.cf.iface.ParseException: bad class file magic 

Again, some research led me to this other post suggesting that the problem is related to incompatibility in java version. My project's compilation target is 1.8 because I'm using retrolambda, si setting it back to 1.7 is not an option.

And here I am (stuck). Any idea is warmly welcome!

Aucun commentaire:

Enregistrer un commentaire