gpt4 book ai didi

android - 加载 JNI 库的应用程序对象上的 Robolectric 坦克。我可以获得解决方法吗?

转载 作者:塔克拉玛干 更新时间:2023-11-02 18:53:29 28 4
gpt4 key购买 nike

我的 Android 应用程序的 Application 对象加载了一个 JNI 库,而 Robolectric 似乎不喜欢那样。当我去运行我的测试 Robolectric 时,我得到了这个堆栈跟踪:

java.lang.UnsatisfiedLinkError: no cperryinc-jni in java.library.path at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1758) at java.lang.Runtime.loadLibrary0(Runtime.java:823) at java.lang.System.loadLibrary(System.java:1045) at com.cperryinc.application.MoolaApplication.(MoolaApplication.java:24) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:169) at com.xtremelabs.robolectric.internal.ClassNameResolver.safeClassForName(ClassNameResolver.java:36) at com.xtremelabs.robolectric.internal.ClassNameResolver.resolve(ClassNameResolver.java:15) at com.xtremelabs.robolectric.ApplicationResolver.newApplicationInstance(ApplicationResolver.java:71) at com.xtremelabs.robolectric.ApplicationResolver.resolveApplication(ApplicationResolver.java:28) at com.xtremelabs.robolectric.RobolectricTestRunner.createApplication(RobolectricTestRunner.java:483) at com.xtremelabs.robolectric.RobolectricTestRunner.setupApplicationState(RobolectricTestRunner.java:360) at com.xtremelabs.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:299) at com.xtremelabs.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:277) at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184) at org.junit.runners.ParentRunner.run(ParentRunner.java:236) at org.junit.runner.JUnitCore.run(JUnitCore.java:157) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:182) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:62) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

我不确定我能做些什么。关于解决方法的任何想法?

最佳答案

解决方案适用于 Robolectric 1.2,不适用于 2.+

感谢 Jan Berkel 在这里回答这个问题: https://groups.google.com/d/msg/robolectric/beW9XjT8E1A/pJQrRaybN30J

class MyJniClass {
static {
try {
System.loadLibrary("libname");
} catch (UnsatisfiedLinkError e) {
// only ignore exception in non-android env
if ("Dalvik".equals(System.getProperty("java.vm.name"))) throw e;
}
}
}

then in the testrunner:

public class MyTestRunner  extends RobolectricTestRunner {
public MyTestRunner(Class testClass) throws InitializationError {
// remove native calls + replace with shadows
addClassOrPackageToInstrument("com.example.jni.MyJniClass");
}

protected void bindShadowClasses() {
// bind shadow JNI classes
}
}

关于android - 加载 JNI 库的应用程序对象上的 Robolectric 坦克。我可以获得解决方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10425591/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com