使用Robolectric时如何加载.so?

How to load .so when using Robolectric?

W/Environment: EXTERNAL_STORAGE 未定义;回到默认值

java.lang.UnsatisfiedLinkError: com.autonavi.amap.mapcore.MapCore.nativeNewInstance(Ljava/lang/String;)J
    at com.autonavi.amap.mapcore.MapCore.nativeNewInstance(Native Method)
    at com.autonavi.amap.mapcore.MapCore.<init>(MapCore.java:62)
    at com.amap.api.mapcore.AMapDelegateImpGLSurfaceView.<init>(AMapDelegateImpGLSurfaceView.java:356)
    at com.amap.api.mapcore.AMapDelegateImpGLSurfaceView.<init>(AMapDelegateImpGLSurfaceView.java:318)
    at com.amap.api.mapcore.ak.a(MapFragmentDelegateImp.java:123)
    at com.amap.api.maps.MapView.onCreate(MapView.java:131)
    at com.e.activity.DriverReleaseActivity.initData(DriverReleaseActivity.java:179)
    at com.e.base.BaseActivity.onCreate(BaseActivity.java:29)
    at android.app.Activity.performCreate(Activity.java:5933)
    at org.robolectric.util.ReflectionHelpers.callInstanceMethod(ReflectionHelpers.java:195)
    at org.robolectric.util.ActivityController.run(ActivityController.java:122)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:304)
    at org.robolectric.shadows.CoreShadowsAdapter.runPaused(CoreShadowsAdapter.java:45)
    at org.robolectric.util.ActivityController.create(ActivityController.java:118)
    at org.robolectric.util.ActivityController.create(ActivityController.java:129)
    at com.enjoytech.ecar.carpooling.activity.DriverReleaseAcitivityTest.init(DriverReleaseAcitivityTest.java:87)
    at com.enjoytech.ecar.carpooling.activity.DriverReleaseAcitivityTest.test(DriverReleaseAcitivityTest.java:79)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:251)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runChild(RobolectricTestRunner.java:54)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner.evaluate(RobolectricTestRunner.java:152)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

进程已完成,退出代码为 -1

无法加载 .so 文件。

当我使用

try {      
    System.loadLibrary("libamapv304");
    System.loadLibrary("libamapv304ex");
} catch (UnsatisfiedLinkError e) {
    e.printStackTrace();
}

它导致 java.lang.UnsatisfiedLinkError: no libamapv304 in java.library.path

如何使用 .so 通过 Roboletric 完成单元测试?

我会以某种受控方式加载本机库。为简单起见,假设它位于 Application:

public class NativeLibApplication extends Application {
    ...
    protected void loadNativeLibraries() {
        try {      
            System.loadLibrary("libamapv304");
            System.loadLibrary("libamapv304ex");
        } catch (UnsatisfiedLinkError e) {
            ...
        }
    }
    ...
}

Robolectric 让您有可能在测试中调整您的应用程序。您应该在 test 文件夹下的同一包中创建 TestNativeLibApplication 并禁止加载本机库:

public class TestNativeLibApplication extends NativeLibApplication {
    ...
    @Override
    protected void loadNativeLibraries() {
        //do nothing
    }
    ...
}

支持打包原生库的库

总结

1.在您的应用代码中禁用加载。

2。移植动态 Link 库。

  • 如果使用最简单的Linux操作系统,直接获取x86-64(Depending on your cpu architecture)的so文件,添加依赖so库(在ndk-bundle,平台文件夹)。

  • 如果您使用masOS操作系统,则需要多做一些工作。并且你应该有原生的so库源码,在macOS系统下编译:

``` Bash

.o

cc -c -I/System/Library/Frameworks/JavaVM.framework/Headers *.cpp

得到 xxx.dylib

g++ -dynamiclib -undefined 抑制 -flat_namespace *.o -o something.dylib

```

  • Windows类似

3。在 RobolectricApplication 中加载 so 库。

结束,运行你的测试用例,做得好:

load so test case http://rocko-blog.qiniudn.com/2016-11-27_09-32-15_test_case_success.png

详情

示例代码:RobolectricSupportNativeLibs