Skip to content

PlatformDependent.resolveAndroidApiVersion() fails when running under Robolectric #4697

@cesar1000

Description

@cesar1000

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions