NoClassDefFoundError ObjenesisStd on Android API 19 - android

I'm getting the following crashing during an instrumentation test ONLY on emulators running API v19. If I run on newer versions everything works fine.
03-01 20:26:18.781 2878-2878/? E/MonitoringInstrumentation: Exception
encountered by: Thread[main,5,main]. Dumping thread state to outputs
and pining for the fjords.
java.lang.NoClassDefFoundError: org.objenesis.ObjenesisStd
at org.mockito.internal.creation.jmock.ClassImposterizer.(ClassImposterizer.java:36)
at org.mockito.internal.creation.jmock.ClassImposterizer.(ClassImposterizer.java:29)
at org.mockito.internal.util.MockCreationValidator.isTypeMockable(MockCreationValidator.java:17)
at org.mockito.internal.util.MockCreationValidator.validateType(MockCreationValidator.java:21)
at org.mockito.internal.creation.MockSettingsImpl.validatedSettings(MockSettingsImpl.java:133)
at org.mockito.internal.creation.MockSettingsImpl.confirm(MockSettingsImpl.java:127)
at org.mockito.internal.MockitoCore.mock(MockitoCore.java:50)
at org.mockito.Mockito.mock(Mockito.java:1243)
at org.mockito.Mockito.mock(Mockito.java:1120)
The problem is this line:
java.lang.NoClassDefFoundError: org.objenesis.ObjenesisStd
My app is multi-dex, and I'm using dexmaker-mockito for androidTestCompile. I'm at a complete loss why this only breaks on an older API. It started happening when I added another module to my project, which is a pure java module with no dependency on mockito.

This exception (ClassNotFoundException) tells you about a unmet dependency at runtime: the JVM needs to load a class; which is not present in the class path.
Here it is Mockito that needs Objenesis. Normally that library should be pulled automatically when you a system like maven and give the correct dependency to Mockito.

Related

Android Webview Chromium UnitTests fail with java.lang.NoSuchMethodError

We are currently facing this NoSuchMethod Error when we want to create the unit test with Robolectric version 4.3.1 and Mockito version 2.18.3
Exceptions:
java.lang.NoSuchMethodError: 'void com.android.webview.chromium.WebViewChromium.<init>(com.android.webview.chromium.WebViewChromiumFactoryProvider, android.webkit.WebView, android.webkit.WebView$PrivateAccess, boolean)'
Here is the code we did:
mWebView = Mockito.spy(
new WebviewChromium(
mockWebViewChromiumProvider, webView, webView.new PrivateAccess(), false));
In BUILD.gn, we have included the "//android_webview/glue:glue_java" as dependency, we can successfully compile the code but it sounds like in the runtime, it refers to the another wrong jar, does someone know how to config the build file to use the correct jar or any ideas about how to solve this error?
did you check this followjng thread https://github.com/robolectric/robolectric/issues/5092 and this one I think you need open jdk 11 or 13 I recently had the same issue after adding open jdk 13 to android studio it solved my problem robolectric started working fine you can check following thread as well https://github.com/robolectric/robolectric/issues/5092

java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/widget/RecyclerView when running recorded Robo test on Firebase test lab

I have recorded a Robo test in Android Studio and I run the file in Firebase Test Lab as a Robo test - it fails with the following stack trace when the test tries to click on the RecyclerView elements:
FATAL EXCEPTION: Thread-12
Process: com.transfollow.tf, PID: 12981
java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/widget/RecyclerView;
at androidx.test.espresso.contrib.RecyclerViewActions$ActionOnItemAtPositionViewAction.getConstraints(Unknown Source:0)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.getConstraints(ViewInteraction.java:3)
at androidx.test.espresso.ViewInteraction.a(ViewInteraction.java:14)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:2)
The app uses androidX and jetifier that's why the app instrumentation tests work fine, even though the RecyclerViewActions has references to support v7 libs. But why does this happen with robo and what can be done to fix this?
This issue was recently fixed and the updated Robo should work correctly with both android and androidx RecyclerViews. We will release the updated Robo shortly, at which point your FTL Robo tests will not produce the error above.

Shadow PackageParser.call ParsePackage PackageParser$Packageparserexception

I am trying to move my code to dynamic feature and while moving the test cases i created a test package inside dynamic feature module and moved related test classes there.
The test cases are not running and failing with below exception
java.lang.RuntimeException: android.content.pm.PackageParser$PackageParserException: Failed to parse /Users/anujjha/../build/intermediates/apk_for_local_test/debugUnitTest/packageDebugUnitTestForUnitTest/apk-for-local-test.ap_
at org.robolectric.shadows.ShadowPackageParser.callParsePackage(ShadowPackageParser.java:69)
at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage_measured(AndroidTestEnvironment.java:314)
at org.robolectric.android.internal.AndroidTestEnvironment.lambda$loadAppPackage$1(AndroidTestEnvironment.java:284)
at org.robolectric.util.PerfStatsCollector.measure(PerfStatsCollector.java:53)
at org.robolectric.android.internal.AndroidTestEnvironment.loadAppPackage(AndroidTestEnvironment.java:282)
at org.robolectric.android.internal.AndroidTestEnvironment.installAndCreateApplication(AndroidTestEnvironment.java:178)
at org.robolectric.android.internal.AndroidTestEnvironment.setUpApplicationState(AndroidTestEnvironment.java:169)
at org.robolectric.RobolectricTestRunner.beforeTest(RobolectricTestRunner.java:301)
There are all junit test cases. Any idea how to make it work.and what can i check in code or gradle.

Robolectric AndroidX fragments NoClassDefFoundError

After migration of code and tests to AndroidX, all seems to work pretty well, however Robolectric junit tests for two fragments are failing due to NoClassDefFoundError: androidx/fragment/testing/R$style exception.
The stack trace:
java.lang.NoClassDefFoundError: androidx/fragment/testing/R$style at
androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity.onCreate(FragmentScenario.java:79)
at android.app.Activity.performCreate(Activity.java:5933) at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at
androidx.test.runner.MonitoringInstrumentation.callActivityOnCreate(MonitoringInstrumentation.java:674)
at
org.robolectric.android.controller.ActivityController.lambda$create$0(ActivityController.java:69)
at
org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:365)
at
org.robolectric.android.controller.ActivityController.create(ActivityController.java:69)
at
org.robolectric.android.controller.ActivityController.create(ActivityController.java:74)
at
org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:39)
at
androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:207)
at
androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:283)
at
androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:265)
For testing fragments I'm using FragmentScenario, and it seems the FragmentScenario.EmptyFragmentActivity refers to a missing R class from a package androidx.fragment.testing.R:
setTheme(getIntent().getIntExtra(THEME_EXTRAS_BUNDLE_KEY,
R.style.FragmentScenarioEmptyFragmentActivityTheme));
Any idea what might be wrong? Maybe I'm missing a dependency that's not that obvious to me.
Reproducible in following project:
https://github.com/marcinbak/androidx-test-error
Also reported in Google's issue tracker: https://issuetracker.google.com/issues/122321150
According to answer from Google it is required to include androidx_fragment_test (androidx.fragment:fragment-testing:1.1.0-alpha03) artifact in the testing APK (not in instrumentation APK or testing dependency).
What this means it has to be added as debugImplementation androix_fragment_test in your build.gradle file.
If you also run tests on release target then you have to add implementation androix_fragment_test.
You can see the whole conversation in Google's issue tracker: https://issuetracker.google.com/issues/122321150

VerifyError in multidex app when injecting dependency with Dagger

A sample app for library has ~67k methods. It has multidex enabled to overcome the 65k method limit. Unfortunately with multidex enabled the app crashes on VerifyError when trying to inject EndpointAdapter in main activity.
This issue doesn't occur when the app is proguarded and the multidex is disabled, so it must be caused by multidex and Dagger 1 problems.
I'm sure EndpointAdapter is in the main dex file, but some classes generated by Dagger are located in the second dex file generated by multidex. This issue occurs on devices with API < 21 (eg. on genymotion with KitKat 4.4.4).
Any idea why it crashes with VerifyError?
FATAL EXCEPTION: main
Process: pl.toro.libsample.debug, PID: 11775
java.lang.VerifyError: pl/toro/lib/network/EndpointAdapter
at java.lang.Class.getDeclaredConstructors(Native Method)
at java.lang.Class.getDeclaredConstructors(Class.java:574)
at dagger.internal.loaders.ReflectiveAtInjectBinding.getConstructorsForType(ReflectiveAtInjectBinding.java:232)
at dagger.internal.loaders.ReflectiveAtInjectBinding.create(ReflectiveAtInjectBinding.java:168)
at dagger.internal.FailoverLoader.getAtInjectBinding(FailoverLoader.java:74)
at dagger.internal.Linker.createBinding(Linker.java:224)
at dagger.internal.Linker.linkRequested(Linker.java:141)
at dagger.ObjectGraph$DaggerObjectGraph.getInjectableTypeBinding(ObjectGraph.java:309)
at dagger.ObjectGraph$DaggerObjectGraph.inject(ObjectGraph.java:279)
at pl.toro.lib.app.BaseApplication.inject(BaseApplication.java:135)
...
Here's output of the MultiDex tag
VM with version 1.6.0 does not have multidex support
install
MultiDexExtractor.load(/data/app/pl.toro.libsample.debug-1.apk, false)
Detected that extraction must be performed.
Extraction is needed for file /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip
Extracting /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes-1477675005.zip
Renaming to /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip
Extraction success - length /data/data/pl.toro.libsample.debug/code_cache/secondary-dexes/pl.toro.libsample.debug-1.apk.classes2.zip: 187777
load found 1 secondary dex files
install done
EDIT
I've switched to Dagger 2 and this issue is resolved as of now. Dagger 2 no longer uses reflection which is the major factor of this issue.
As pointed out in this blog post[1], are you creating the dagger graph after installing the multi-dex. So in MultiDexApplication#attachBaseContext after the call to super (or calling MultiDex.install() yourself).
[1] https://developers.soundcloud.com/blog/congratulations-you-have-a-lot-of-code-remedying-androids-method-limit-part-2

Categories

Resources