0

I have an written a small example Android app that is the "out of the box" single empty activity app. When I try to add powermock2 via build.gradle, it will not run. Below is my entire build.gradle file.

Note: Using compile because we're going to override getSystemService(String service) and embed some mocks within it to tweak the return(s) for ConnectivityManger and NetworkType.

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"

    defaultConfig {
        applicationId "com.example.exampleapp"
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"

        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

def powerMockVersion =  '1.7.0'

dependencies {
    repositories {
        mavenCentral()
    }

    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile ("org.powermock:powermock-module-junit4:$powerMockVersion") {
        exclude group: 'org.objenesis', module: 'objenesis'
    }

    compile ('org.powermock:powermock-api-mockito2:1.7.0') {
        exclude group: 'org.mockito', module: 'hamcrest-core'
        exclude group: 'org.hamcrest', module: 'hamcrest-core'
        exclude group: 'junit' exclude group: 'org.mockito'
        exclude group: 'org.objenesis', module: 'objenesis'
    }

//    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
//        exclude group: 'com.android.support', module: 'support-annotations'
//    })
//    testCompile 'junit:junit:4.12'
}

To reproduce:

In your android project,
1) create a new Android Module for an empty activity and call it exampleapp.
2) edit your build.gradle file for it. copy/paste the above.
3) Gradle sync & Run it.
Fails with the dexing error.

Error:Execution failed for task ':exampleapp:transformClassesWithDexForDebug'.
> com.android.build.api.transform.TransformException: com.android.ide.common.process.ProcessException: java.util.concurrent.ExecutionException: com.android.dex.DexException: Multiple dex files define Lorg/powermock/api/mockito/expectation/WithExpectedArguments;

As a workaround, if I use mockito (as opposed to mockito2) the dex problem goes away, but I need mockito2.

compile ('org.powermock:powermock-api-mockito2:1.7.0') {...}

This MAY be a defect in Powermock. Hence, I also posted an issue there... https://github.com/powermock/powermock/issues/821

The example Android project: http://23.79.234.224/Anaina/cache/TestData2/ficticious_project.tar

Edit: Adding the screenshot that shows that it's actually only referenced in one package. enter image description here

Michael Lupo
  • 326
  • 3
  • 17
  • 1
    Click on **Build** then **Clean Project** and wait for the process to finish. After that, click again on **Build** and then **Rebuild Project**. – Animesh Patra Jul 19 '17 at 12:03

3 Answers3

1

from your dependencies comment out junit as shown below

dependencies {
    repositories {
        mavenCentral()
    }
    testCompile 'junit:junit:4.12'
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile ("org.powermock:powermock-module-junit4:$powerMockVersion") {
        exclude group: 'org.objenesis', module: 'objenesis'
    }

    compile ('org.powermock:powermock-api-mockito2:1.7.0') {
        exclude group: 'org.mockito', module: 'hamcrest-core'
        exclude group: 'org.hamcrest', module: 'hamcrest-core'
        exclude group: 'junit' exclude group: 'org.mockito'
        exclude group: 'org.objenesis', module: 'objenesis'
    }

//    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
//        exclude group: 'com.android.support', module: 'support-annotations'
//    })

}

for more check this link

Omkar
  • 3,040
  • 1
  • 22
  • 42
  • Hi Omi, thanks for your suggestion. However, the same problem persists. I also had to add back in the espresso stuff as the android.support.test.InstrumentationRegistry and .AndroidJuni4 stuff couldn't be found till I did. Cleaned, rebuilt, still same out come. – Michael Lupo Jul 19 '17 at 12:38
  • Did you just remove a commented-out line of code or am I missing something? – Morphing Coffee Jan 07 '19 at 13:08
0

So it seems that the first problem can be overcome by adding this to the android element of the build.gradle file

        dexOptions {
            jumboMode true
            multiDexEnabled true
        }

Once I do that I no longer encounter the issue described in this post. Instead I encounter an issue with java.beans.

java.lang.IllegalStateException: Could not initialize plugin: interface org.mockito.plugins.MockMaker
at org.mockito.internal.configuration.plugins.PluginLoader$1.invoke(PluginLoader.java:66)
at java.lang.reflect.Proxy.invoke(Proxy.java:393)
at $Proxy0.isTypeMockable(Unknown Source)
at org.mockito.internal.util.MockUtil.typeMockabilityOf(MockUtil.java:29)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:22)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:186)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:180)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:62)
at org.mockito.Mockito.spy(Mockito.java:1812)
at com.akamai.android.sdk.internal.MapClientTestMainApp.getSystemService(MapClientTestMainApp.java:85)
at com.akamai.android.sdk.net.VocAccelerator.init(VocAccelerator.java:96)
at com.akamai.android.sdk.VocService.createVocService(VocService.java:143)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.setVocService(VocSdkLiveTest_BaseServiceTest.java:217)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.unRegisterIfNecessary(VocSdkLiveTest_BaseServiceTest.java:166)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.waitForSyncToFinishAndThenDeregister(VocSdkLiveTest_BaseServiceTest.java:211)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.unRegisterIfNecessary(VocSdkLiveTest_BaseServiceTest.java:151)
at com.akamai.vocsdkliveapitestlib.VocSdkLiveTest_BaseServiceTest.setUp(VocSdkLiveTest_BaseServiceTest.java:41)
at com.akamai.android.sdk.internal.MapLT_AICTests.setUp(MapLT_AICTests.java:64)
at junit.framework.TestCase.runBare(TestCase.java:132)
at junit.framework.TestResult$1.protect(TestResult.java:115)
at android.support.test.internal.runner.junit3.AndroidTestResult.runProtected(AndroidTestResult.java:77)
at junit.framework.TestResult.run(TestResult.java:118)
at android.support.test.internal.runner.junit3.AndroidTestResult.run(AndroidTestResult.java:55)
at junit.framework.TestCase.run(TestCase.java:124)
at android.support.test.internal.runner.junit3.NonLeakyTestSuite$NonLeakyTest.run(NonLeakyTestSuite.java:63)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at android.support.test.internal.runner.junit3.DelegatingTestSuite.run(DelegatingTestSuite.java:103)
at android.support.test.internal.runner.junit3.AndroidTestSuite.run(AndroidTestSuite.java:69)
at android.support.test.internal.runner.junit3.JUnit38ClassRunner.run(JUnit38ClassRunner.java:103)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:59)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:262)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1970)
Caused by: java.lang.NoClassDefFoundError: Failed resolution of: Ljava/beans/Introspector;
at org.powermock.configuration.support.ConfigurationMapper.map(ConfigurationMapper.java:45)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.mapConfiguration(ConfigurationReaderBuilder.java:89)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.createConfiguration(ConfigurationReaderBuilder.java:80)
at org.powermock.configuration.support.ConfigurationReaderBuilder$ConfigurationReaderImpl.read(ConfigurationReaderBuilder.java:71)
at org.powermock.configuration.support.ConfigurationFactoryImpl.readDefault(ConfigurationFactoryImpl.java:42)
at org.powermock.configuration.support.ConfigurationFactoryImpl.create(ConfigurationFactoryImpl.java:33)
at org.powermock.configuration.GlobalConfiguration.createConfig(GlobalConfiguration.java:75)
at org.powermock.configuration.GlobalConfiguration.<init>(GlobalConfiguration.java:59)
at org.powermock.configuration.GlobalConfiguration.mockitoConfiguration(GlobalConfiguration.java:40)
at org.powermock.api.mockito.mockmaker.PowerMockMaker.<init>(PowerMockMaker.java:45)
at java.lang.Class.newInstance(Native Method)
at org.mockito.internal.configuration.plugins.PluginLoader.loadImpl(PluginLoader.java:96)
at org.mockito.internal.configuration.plugins.PluginLoader.loadPlugin(PluginLoader.java:45)
at org.mockito.internal.configuration.plugins.PluginRegistry.<init>(PluginRegistry.java:18)
at org.mockito.internal.configuration.plugins.Plugins.<clinit>(Plugins.java:17)
at org.mockito.internal.configuration.plugins.Plugins.getMockMaker(Plugins.java:33)
at org.mockito.internal.util.MockUtil.<clinit>(MockUtil.java:24)
... 39 more
Caused by: java.lang.ClassNotFoundException: Didn't find class "java.beans.Introspector" on path: DexPathList[[zip file "/system/framework/android.test.runner.jar", zip file "/data/app/com.akamai.android.sdk.internal.test-1/base.apk", zip file "/data/app/com.akamai.android.sdk.internal-1/base.apk"],nativeLibraryDirectories=[/data/app/com.akamai.android.sdk.internal.test-1/lib/arm, /data/app/com.akamai.android.sdk.internal-1/lib/arm, /data/app/com.akamai.android.sdk.internal.test-1/base.apk!/lib/armeabi-v7a, /data/app/com.akamai.android.sdk.internal-1/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
... 56 more
Suppressed: java.lang.ClassNotFoundException: java.beans.Introspector
at java.lang.Class.classForName(Native Method)
at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
... 57 more
Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack trace available

Based on this, I think this Stack Overflow Post may indeed sum up why PowerMock isn't working in its current state on Android.

Maybe this can help someone....

Michael Lupo
  • 326
  • 3
  • 17
0

apparently powermock's powermock-api-mockito2 module and its direct dependency org.powermock:powermock-api-mockito-common (both version 1.7.4) declare .class files for the classes:

  1. org.powermock.api.mockito.expectation.WithoutExpectedArguments
  2. org.powermock.api.mockito.expectation.WithExpectedArguments

I was able to resolve the build error by adding the following exclude:

androidTestImplementation('org.powermock:powermock-api-mockito2:1.7.4') {
    exclude group: 'org.powermock', module: 'powermock-api-mockito-common'
}

I'm not sure if powermock's mockito API will work after this, however, since removing the whole module is way overkill... I'd really like to be able to remove just the dup classes, but I couldn't find a way to do so in modern gradle.

CCJ
  • 1,619
  • 26
  • 41