Expecting mocked instances to be initialised correctly.
Currently, whenever a mock of a class is created a MockKAgentException is thrown (see stacktrace below) on api level 28 or greater. I don't seem to be having any issues on lower API levels.
Caused by: io.mockk.proxy.MockKAgentException: MockK could not self-attach a jvmti agent to the current VM. This feature is required for inline mocking.
This error occured due to an I/O error during the creation of this agent: java.io.IOException: No such file or directory
Potentially, the current VM does not support the jvmti API correctly
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:63)
at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:46)
at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172)
... 33 more
Caused by: java.io.IOException: No such file or directory
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
at java.io.File.createTempFile(File.java:2018)
at java.io.File.createTempFile(File.java:2064)
at io.mockk.proxy.android.JvmtiAgent.appendToBootstrapClassLoaderSearch(JvmtiAgent.kt:53)
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:48)
... 35 more
// -----------------------[ GRADLE DEFINITIONS ] -----------------------
dependencies {
androidTestImplementation "io.mockk:mockk-android:1.9.3"
}
// -----------------------[ YOUR CODE STARTS HERE ] -----------------------
object MockHelper {
fun mockIsEnabled(enabled: Boolean = false) {
val mock: LocationChecker = mockk(relaxed = true) //failure here
val component = DaggerTestAppComponent
.builder()
.testModule(TestModule(locationChecker = mock))
.build()
val application = Application.getApplication(InstrumentationRegistry.getTargetContext())
application.appComponent = component
}
}
@RunWith(AndroidJUnit4::class)
class ScreenTest : BaseUITest() {
@Before
fun setUp() {
MockHelper.mockIsEnabled(true)
}
}
// -----------------------[ YOUR CODE ENDS HERE ] -----------------------
@jtrollkarl I'm running into this, too. And I think I have a root cause. The rest of the exception message complains about not being able to find libmockkjvmtiagent.so. As you can see in this screenshot (apkAnalyzer of my app's test apk), the x86_64 and arm64-v8a binaries for libmockkjvmtiagent.so do not exist:

I don't seem to be having any issues on lower API levels.
Same here. That seems to be due to this SDK_INT check: https://github.com/mockk/mockk/blob/fdb6839b316bfee571a387852c3de7448a6eb64b/agent/android/src/main/kotlin/io/mockk/proxy/android/AndroidMockKAgentFactory.kt#L32
I don't see anywhere else in mockk's source that this particular exception can be thrown.
An additional contributing factor why you're not seeing this on other API levels is _probably_ that you're loading a x86_64 or arm64-v8a in your tests on a device that is x86_64 or arm64-v8a hardware. _Conveniently_, x86_64 platform images only seem to be available from API 28 onwards on the google repository:
$ ./tools/bin/sdkmanager "system-images;android-27;google_apis;x86_64"
Warning: Failed to find package system-images;android-27;google_apis;x86_64
Error: Package path is not valid. Valid system image paths are:ository...
system-images;android-28;google_apis;x86
system-images;android-26;google_apis_playstore;x86
system-images;android-18;google_apis;x86
system-images;android-25;google_apis;x86
system-images;android-25;google_apis_playstore;x86
system-images;android-26;google_apis;x86
system-images;android-28;google_apis;x86_64
system-images;android-23;google_apis;x86
system-images;android-27;google_apis;x86
system-images;android-19;google_apis;x86
system-images;android-22;google_apis;x86
system-images;android-21;default;armeabi-v7a
system-images;android-21;google_apis;x86
system-images;android-22;default;x86
null
So you may see this if you test on emulators--since the platform emulated prior to API 28 are definitely 32-bit, they'd be loading the 32-bit versions of your shared objects and the 32-bit version of libmockkjvmtiagent.so, which exists.
So my suggestion to work around this for now is to exclude the 64-bit versions of the shared objects that you load from your test apks (which I realize defeats the point of testing them in the first place). You'd like to test the 64-bit versions to make it as close to production as possible, but it does not seem doable at the moment with mockk.
Likely, the motivation for including the 64-bit binaries is the Google Play requirement to include 64-bit native binaries by August 1st. That is the motivation for me. So it would be nice to see a 64-bit version of libmockkjvmtiagent.so.
@oleksiyp Do you think the x86_64 and arm64-v8a architectures will be supported soon?
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as stale just ask to put an important label.
Add an important label! This makes mockk unusable on android 10.
@oleksiyp Can you please re-open this issue?
I also have this problem after upgrading to Android Studio 3.6 Beta 2 (from 3.6 Beta 1 which was working)
I will try to rollback Android Studio updates
Nope that did not work
Where should this file reside? libmockkjvmtiagent.so
I still get
Caused by: io.mockk.proxy.MockKAgentException: MockK could not self-attach a jvmti agent to the current VM. This feature is required for inline mocking.
This error occured due to an I/O error during the creation of this agent: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
Potentially, the current VM does not support the jvmti API correctly
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:63)
at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:46)
at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172)
... 29 more
Caused by: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
at dalvik.system.VMDebug.nativeAttachAgent(Native Method)
at dalvik.system.VMDebug.attachAgent(VMDebug.java:570)
at android.os.Debug.attachJvmtiAgent(Debug.java:2481)
at io.mockk.proxy.android.JvmtiAgent.<init>(JvmtiAgent.kt:46)
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:36)
... 31 more
----- end exception -----
The last thing I also upgraded was the Android Gradle Plugin - from 3.5.1 to 3.6.0-beta01
Reverting back to Android Gradle Plugin 3.5.1 makes mockk work again!
Also works with Android Studio 3.6 Beta 2
Hope that helps
I also have this problem after upgrading from min_sdk 21 to min_sdk 23.
It happens only in the x64 Emulator
MockK version: 1.10.0
OS: >= API level 28
Kotlin version: 1.3.72
JDK version: 8
JUnit version: 4.12
Type of test: instrumented test
min_sdk = 23
target_sdk = 29
Android Emulator API version 28
Potentially, the current VM does not support the jvmti API correctly
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:65)
at io.mockk.impl.JvmMockKGateway.<init>(JvmMockKGateway.kt:46)
at io.mockk.impl.JvmMockKGateway.<clinit>(JvmMockKGateway.kt:172)
... 29 more
Caused by: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
at dalvik.system.VMDebug.nativeAttachAgent(Native Method)
at dalvik.system.VMDebug.attachAgent(VMDebug.java:526)
at android.os.Debug.attachJvmtiAgent(Debug.java:2398)
at io.mockk.proxy.android.JvmtiAgent.<init>(JvmtiAgent.kt:48)
at io.mockk.proxy.android.AndroidMockKAgentFactory.init(AndroidMockKAgentFactory.kt:38)
... 31 more
Same issue here
MockK version: 1.10.0
OS: >= API level 28
Kotlin version: 1.3.71
JDK version: 8
JUnit version: 4.12
Type of test: instrumented test
min_sdk = 26
target_sdk = 29
Android Emulator API version 29
@dri94 I spent some time trying to fix this issue. Unfortunately, I didn't find easy way to fix it.
@oleksiyp I'm confused, I just now see you moved it to fixed a couple of days ago, is it not fixed? Either way, thanks for all the hard work. I wish i was more adept to help you with this mocking library as it is truly a great one.
it was labeled by mistake
It is also happening on x86 image, not only x86_64. I'm not sure what we can do
Some for me, unfortunately. I just updated the Android Gradle plugin from 3.5.1 to 3.6.3 in my Android project:
implementation("com.android.tools.build:gradle:3.6.3")
Now my instrumented tests fail. Reverting to 3.5.1 seems like the only option so far.
I also tried android.useNewApkCreator=false and android:extractNativeLibs="true" out of lack of ideas, but with no success.
Tried updating Android Gradle Plugin to 4.0.0 and the issue is still present resulting in failing instrumented tests
I tried it again and found that I put the flag "extractNativeLibs" into the main manifest instead of the androidTest manifest which has no effect.
It works if you put an AndroidManifest.xml into your src/androidTest folder with this content:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.package">
<application android:extractNativeLibs="true" />
</manifest>
Of course this seems to only be a workaround for that change, however, but at least it works for now:
https://developer.android.com/studio/releases/gradle-plugin#extractNativeLibs
I have a simple workaround.
Remove all of your instrumented tests from the app module.
I put all my features and their tests in an Android library module.
With that the mockk will work normally.
Any update on this issue?
Weird: this issue was reported with MockK version: 1.9.3, but I don't get this error with this version, but I do get it with 1.10.2
I have a same problem.
MockK version: 1.10.2
OS: >= API level 28
Kotlin version: 1.4.10
JDK version: 8
JUnit version: 4.12
Type of test: instrumented test
min_sdk = 28
target_sdk = 29
Android Emulator API version 29
Caused by: io.mockk.proxy.MockKAgentException: MockK could not self-attach a jvmti agent to the current VM. This feature is required for inline mocking.
This error occured due to an I/O error during the creation of this agent: java.io.IOException: Unable to dlopen libmockkjvmtiagent.so: dlopen failed: library "libmockkjvmtiagent.so" not found
@DonJogi This didn't work for me, I even did:
SourceSets {
androidTest.manifest.srcFile "src/androidTest/AndroidManifest.xml"
}
That did not work either. Even changed the test build type to debug and put the manifest in the debug package. Did not work. The same error. Am I missing something?
Also I'd like to say moving down to Gradle 3.5.1 is a crazy solution, can't even use viewbinding with that version
Weird: this issue was reported with MockK version: 1.9.3, but I don't get this error with this version, but I do get it with 1.10.2
I have this same exact issue. I'm using mockk 1.9.3 until 1.10.x is fixed.
Decided to pull androidTestImplementation "io.mockk:mockk-android:$mockk_version" from instrumented tests because there doesn't seem to be to a worthwhile solution. Definitely will proceed to use Mockk in JVM tests.
FYI: If you are using Mockk in an instrumented test on a device with API 28 mockk-android prevents you from running the tests with debugger attached. There is no clear error indicating the tests don't work because of Mockk. Could apply to other API versions too.
I have the same issue and i found this on a x86_64 device:
Accessing hidden method Ldalvik/system/VMRuntime;->setHiddenApiExemptions([Ljava/lang/String;)V (blacklist,core-platform-api, reflection, denied)
Therefore is another call which is allowed:
Accessing hidden method Ldalvik/system/VMRuntime;->getRuntime()Ldalvik/system/VMRuntime; (greylist,core-platform-api, reflection, allowed)
It seems here is another blocker around this.
Are there any updates on this issue? Was anyone able to get it working?
Most helpful comment
I tried it again and found that I put the flag "extractNativeLibs" into the main manifest instead of the androidTest manifest which has no effect.
It works if you put an AndroidManifest.xml into your src/androidTest folder with this content:
Of course this seems to only be a workaround for that change, however, but at least it works for now:
https://developer.android.com/studio/releases/gradle-plugin#extractNativeLibs