question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

mockito-android has extremely large method count

See original GitHub issue

I’ve been investigating why my test apk has hit the 65k “multidex” method limit. It seems like mockito-android is by far the dominant contributor to method count.

To help get cleaner data, I created a brand new empty project in Android Studio 3.0-alpha3 and removed all dependencies except the following:

dependencies {
    androidTestImplementation 'org.mockito:mockito-android:2.8.9'
    androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
}

After this, I built the test apk using ./gradlew assembleDebugAndroidTest and used the APK Analyzer tool in Android Studio to examine the APK. Here’s what it reports:

org.mockito --> 2402 referenced methods net.bytebuddy --> 13512 referenced methods com.android.dx --> 4661 referenced methods

This totals to 20575 referenced methods, which is roughly 1/3 of the total allowable referenced method count for an Android test apk. Since test apks don’t support legacy multidex, if a test apk goes over the 65k method limit, they can no longer run their tests on versions of android older than API 21 (Lollipop).

For extra info, here’s the (relevant) output of ./gradlew :app:dependencies for my sample project:

debugAndroidTestCompileClasspath - Resolved configuration for compilation for variant: debugAndroidTest
+--- org.mockito:mockito-android:2.8.9
|    +--- org.mockito:mockito-core:2.8.9
|    |    +--- net.bytebuddy:byte-buddy:1.6.14
|    |    +--- net.bytebuddy:byte-buddy-agent:1.6.14
|    |    \--- org.objenesis:objenesis:2.5
|    \--- net.bytebuddy:byte-buddy-android:1.6.5
|         +--- net.bytebuddy:byte-buddy:1.6.5 -> 1.6.14
|         \--- com.jakewharton.android.repackaged:dalvik-dx:1
|              \--- com.jakewharton.android.repackaged:libcore-dex:2

In my real app, we are already using Proguard on our test APK, and that only strips out ~3k of the 20k methods referenced by mockito-android (so this probably isn’t a viable solution).

Is there anything we can do to reduce the method count pulled in by mockito-android?

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:15 (10 by maintainers)

github_iconTop GitHub Comments

5reactions
jaredsburrowscommented, Jul 16, 2017

@drewhannay Because of this, I continue to use:

  androidTestCompile "com.nhaarman:mockito-kotlin-kt1.1:1.5.0"      , { exclude group: "net.bytebuddy" } // DexMaker has it"s own MockMaker
  androidTestCompile "org.mockito:mockito-core:2.8.47"              , { exclude group: "net.bytebuddy" } // DexMaker has it"s own MockMaker
  androidTestCompile "com.linkedin.dexmaker:dexmaker-mockito:2.2.0" , { exclude group: "net.bytebuddy" } // DexMaker has it"s own MockMaker

See https://github.com/jaredsburrows/android-gif-example/blob/master/build.gradle#L178.

1reaction
tmurakamicommented, Jul 18, 2017

This workaround is effective only with DexMaker. It does not work with mockito-android that indirectly relies on Byte Buddy.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mockito-android has extremely large method count
I've been investigating why my test apk has hit the 65k "multidex" method limit. It seems like mockito-android is by far the dominant ......
Read more >
A Unit Testing Practitioner's Guide to Everyday Mockito - Toptal
Unit testing has become mandatory in the age of Agile, and there are many tools available to help with automated testing. One such...
Read more >
when I run mockito test occurs WrongTypeOfReturnValue ...
someMethod()).thenReturn(List<xxx>) , I got this WrongTypeOfReturnValue exception. Thru debug, I can see that the someMethod method is actually been ...
Read more >
Mockito - Resetting Mock - Tutorialspoint
//reset mock reset(calcService);. Here we've reset mock object. MathApplication makes use of calcService and after reset the mock, using mocked method will fail ......
Read more >
Mockito framework site
Mockito is a mocking framework that tastes really good. It lets you write beautiful tests with a clean & simple API. Mockito doesn't...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found