How to load .so when using Robolectric?

半世苍凉 提交于 2019-12-05 10:06:18

I would load native libraries in some controlled way. For simplicity let assume it is in Application:

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

Robolectric gives you a possibility to tweak your application under the test. You should create TestNativeLibApplication in the same package under test folder and suppress loading native libraries:

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

Support libraries that package native libraries

Summary

1. Disable load so in your app code.

2. Porting Dynamic Link Library.

  • If you use the Linux operating system that is the most simple, just get the x86-64(Depending on your cpu architecture) 's so file, and add the dependency so library(Found in the ndk-bundle, platforms folder).

  • If you use the masOS operating system, you need to do a little more work. And you should have the native so library source code, and compile it under macOS system:

``` Bash

.o

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

get xxx.dylib

g++ -dynamiclib -undefined suppress -flat_namespace *.o -o something.dylib

```

  • Windows is similar.

3. Load so library in your RobolectricApplication.

End, run your test case, well done:

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

Detail

Sample code: RobolectricSupportNativeLibs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!