RxJava: PlatformDependent.resolveAndroidApiVersion() fails when running under Robolectric

PlatformDependent.resolveAndroidApiVersion() tries to determine the Android SDK version by reading the android.os.Build$VERSION#SDK_INT field:

        try {
            return (Integer) Class
                    .forName("android.os.Build$VERSION", true, getSystemClassLoader())
                    .getField("SDK_INT")
                    .get(null);
        } catch (Exception e) {
            // Can not resolve version of Android API, maybe current platform is not Android
            // or API of resolving current Version of Android API has changed in some release of Android
            return ANDROID_API_VERSION_IS_NOT_ANDROID;
        }

When running under Robolectric, the class will be found (since Robolectric bundles an original android.jar). However, the method is using the system class loader for loading it (instead of the Robolectric instrumenting class loader), which will not instrument the class to run in the JVM. As a result, static initialization of the class fails with an UnsatisfiedLinkError when calling SystemProperties.get(), which calls native method native_get.

PlatformDependent.resolveAndroidApiVersion() should use the default class loader or discard UnsatisfiedLinkErrors (or all throwables).

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Comments: 17 (5 by maintainers)

Most upvoted comments

@jwedel hopefully we can do better though 😃. I’ll try a fix tomorrow.

@jwedel this is the class I used: https://gist.github.com/cesar1000/06139a68d5c062beaecbd26d52c3563d. You should put it under src/test/java/android/os in the module containing your tests.