Robolectric tanks on Application objects that load JNI libraries. Can I get a workaround?

后端 未结 3 1111
悲&欢浪女
悲&欢浪女 2021-01-02 06:11

The Application object for my Android app loads a JNI library, and Robolectric doesn\'t seem to like that. When I go to run my tests Robolectric craps out and I

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-02 06:57

    Solution works for Robolectric 1.2, not 2.+

    Thanks to Jan Berkel for answering this here: 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
       }
    }
    

提交回复
热议问题