Home » Android » java – How to get Powermock to work with Dexmaker

java – How to get Powermock to work with Dexmaker

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am trying to incorporate Powermock as a dependency for my Android tests using the following build.gradle configuration:

dependencies{
    compile 'com.android.support:appcompat-v7:21.0.+'
    androidTestCompile('org.mockito:mockito-core:1.9.5')
    androidTestCompile('com.google.dexmaker:dexmaker:1.2')
    androidTestCompile('com.google.dexmaker:dexmaker-mockito:1.2')
    androidTestCompile('org.powermock:powermock-module-junit4:1.5.5') {
        exclude module: 'junit'
    }
    androidTestCompile('org.powermock:powermock-api-mockito:1.5.5') {
        exclude module: 'mockito-all'
    }
}

However, the compiler is complaining that

Error:Gradle: Execution failed for task ':app:packageDebugTest'.
> Duplicate files copied in APK mockito-extensions/org.mockito.plugins.MockMaker
    File 1: ~/.gradle/caches/modules-2/files-2.1/com.google.dexmaker/dexmaker-mockito/1.2/b99884a4c6ef6335ba376f79aa79632b2421c17c/dexmaker-mockito-1.2.jar
    File 2: ~/.gradle/caches/modules-2/files-2.1/com.google.dexmaker/dexmaker-mockito/1.2/b99884a4c6ef6335ba376f79aa79632b2421c17c/dexmaker-mockito-1.2.jar

Looking into the jar structure, I noticed that both Dexmaker and Powermock declare a MockMaker in mockito-extensions

img

What is a MockMaker? How do they differ?
And the most important question: Is it possible to get Powermock to work nicely with Dexmaker?

Thanks in advance. Any help would be greatly appreciated.

How to&Answers:

MockMaker is a glue module that integrates dexmaker with Mockito. It does what’s necessary for Mockito to generate concrete classes with Dalvik .dex files instead of JVM .class files.

It’s possible that Powermock will work with Dexmaker, but it’s unlikely advanced Powermock features will work. In particular, Powermock advertises this:

PowerMock uses a custom classloader and bytecode manipulation to enable mocking of static methods, constructors, final classes and methods, private methods, removal of static initializers and more.

That custom class loader is unlikely to work on dalvikvm.

Answer:

you can try to put this in your build.gradle, it solved to me the same problem

android{
...

packagingOptions{
    exclude 'mockito-extensions/org.mockito.plugins.MockMaker'
}

...

}

Answer:

I had the same problem and i just found the solution here. It involves a bit of manual work and you will have to modify the jar file yourself.

So what nparihar suggests is the following.

  1. Make backup copy of powermock-api-mockito-1.5.5.jar
  2. Rename powermock-api-mockito-1.5.5.jar to powermock-api-mockito-1.5.5.zip
  3. Unzip powermock-api-mockito-1.5.5.zip
  4. cd powermock-api-mockito-1.5.5/
  5. rm -rf mockito-extensions
  6. jar cf powermock-api-mockito-1.5.5.jar META-INF/ org/
  7. put the new jar in your libs foler.

This solution worked for me. Let me know if it works for you as well.

Also, i can see that we are using the same dependencies. In my case i had to remove manually the hamcrest.jar and the objenesis.jar as there were confilcts.

Hope that helps.