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.