微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 如何在使用Robolectric时加载.so?

W / Environment:EXTERNAL_STORAGE undefined;回到认状态

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.runchild(RobolectricTestRunner.java:188)
    at org.robolectric.RobolectricTestRunner.runchild(RobolectricTestRunner.java:54)
    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: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:java.library.path中没有libamapv304

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

最佳答案
我会以某种受控方式加载本机库.为简单起见,假设它在应用程序中:

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
    }
    ...
}

原文地址:https://www.jb51.cc/android/429918.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐