在Android JUnit测试中加载本机库 [英] Loading a native library in an Android JUnit test
问题描述
我使用 ndk-build
生成了一个本机库,我可以在我的Android应用程序中加载和使用它。但是,我想针对我的应用程序的这一部分编写一些测试。
I've generated a native library using ndk-build
which I'm able to load and use with in my Android application. However, I want to write some tests against this part of my app.
在我的测试中调用本机函数时,我收到此异常消息:
When calling the native function in my tests, I recieve this exception message:
java.lang.UnsatisfiedLinkError: no process in java.library.path
...其中进程
是我要导入的本机库,名为 libprocess.so
。
...where process
is my native library to import, named libprocess.so
.
我正在使用Roboelectric进行测试,并使用 RobolectricTestRunner
运行此特定的测试如果它有所作为。
I'm using Roboelectric for my tests, and running this particular one with the RobolectricTestRunner
, if it makes a difference.
如何让我的测试项目看到本地库?
How can I get my test project to 'see' the native library?
编辑:
我正在我的应用中加载库,如下所示:
I'm loading the library in my app like so:
static {
System.loadLibrary("process");
}
public static native int[] process(double[][] data);
调用 Process.process(array)
工作原理应用程序正常(库已加载),但在测试中运行时失败,但上面给出的例外情况。
calling Process.process(array)
works fine in the app (the library is loaded), but fails when run from the tests with the exception given above.
编辑2:
如果我将 -Djava.library.path =< libprocess.so>目录
设置为VM参数,然后:
Edit 2:
If I set -Djava.library.path="<the directory of libprocess.so>"
as a VM argument, then:
System.out.println(System.getProperty("java.library.path"));
确实显示我设置的路径,但我仍然得到相同的异常。我将目录设置为:
does show the path I set, but I still get the same exception. I'm setting the directory as:
<project-name>/libs/x86
...但作为绝对路径。
...but as an absolute path.
推荐答案
我已经切换到默认测试样式(使用ActivityUnitTestCase而不是RoboElectric),它现在运行正常。遗憾的是我必须牺牲测试运行的速度,但在模拟器上运行测试实际上是可行的。您还可以为JNI类创建一个影子类,详见此处:
I've switched to the default testing style (using ActivityUnitTestCase instead of RoboElectric) and it's now running fine. It's a shame I have to sacrifice the speed of test running, but running the tests on the emulator actually works. You could also create a shadow class for the JNI class, as detailed here:
Robolectric坦克在加载JNI库的Application对象上。我可以获得一种解决方法吗?
也许为我的机器编译库可能会有效,但我不能再花费更多时间了。
Perhaps compiling the library for my machine would have worked, but I couldn't spend any more time on it.
这篇关于在Android JUnit测试中加载本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!