错误: java.lang.UnsatisfiedLinkError with roboelectric and realm

2022-09-04 04:43:46

嗨,我正在为我的应用程序使用数据库,并使用单元测试框架来执行单元测试。我看到是用原生c代码编写的,它有文件用于他们的功能。realmroboelectricrealmlibrealm-jni.so

我遇到错误,在某个地方读到roblectric不支持本机库。我认为,必须有办法解决这个问题。我尝试了接受的答案 如何加载 .so 在使用Robolactric时?但没有运气。UnsatisfiedLinkError

java.lang.UnsatisfiedLinkError: Can't load library: C:\Users\XS~1\AppData\Local\Temp\android-tmp-robolectric7796034244909642596\app_lib\realm-jni.dll.2.3.1
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1824)
    at java.lang.Runtime.load0(Runtime.java:809)
    at java.lang.System.load(System.java:1086)
    at com.getkeepsafe.relinker.SystemLibraryLoader.loadPath(SystemLibraryLoader.java:29)
    at com.getkeepsafe.relinker.ReLinkerInstance.loadLibraryInternal(ReLinkerInstance.java:198)
    at com.getkeepsafe.relinker.ReLinkerInstance.loadLibrary(ReLinkerInstance.java:136)
    at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:70)
    at com.getkeepsafe.relinker.ReLinker.loadLibrary(ReLinker.java:57)
    at io.realm.internal.RealmCore.loadLibrary(RealmCore.java:59)
    at io.realm.Realm.init(Realm.java:187)
    at com.xyz.helloworld.HelloWorldApplication.onCreate(HelloWorldApplication.java:17)
    at org.robolectric.internal.ParallelUniverse.setUpApplicationState(ParallelUniverse.java:147)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:329)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:259)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:41)
    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.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:199)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:114)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:57)
    at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:66)
    at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:51)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
    at org.gradle.internal.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
    at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
    at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:109)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
    at org.gradle.internal.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
    at org.gradle.internal.remote.internal.hub.MessageHub$Handler.run(MessageHub.java:377)
    at org.gradle.internal.concurrent.ExecutorPolicy$CatchAndRecordFailures.onExecute(ExecutorPolicy.java:54)
    at org.gradle.internal.concurrent.StoppableExecutorImpl$1.run(StoppableExecutorImpl.java:40)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

HelloWorldApplication.java

public class HelloWorldApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        Realm.init(this);

        RealmConfiguration config = new RealmConfiguration.Builder()
                .name("book.realm")
                .schemaVersion(1)
                .build();

        Realm.setDefaultConfiguration(config);
    }

    protected void loadNativeLibraries() {
        try {
            System.loadLibrary("librealm-jni");
        } catch (UnsatisfiedLinkError e) {
        }
    }
}

TestHelloWorldApplication.java

public class TestHelloWorldApplication extends HelloWorldApplication  {

    @Override
    protected void loadNativeLibraries() {
        //do nothing
    }
}

答案 1

看看这个

这是一个悬而未决的问题,Realm还不支持Robolectric(


答案 2

推荐