[Android]Relax “Restrictions on non-SDK interfaces” on Android P

Android P has a feature of Restrictions on non-SDK interfaces.
=> https://developer.android.com/preview/restrictions-non-sdk-interfaces

If you have a method like below, you will face java.lang.NoSuchMethodException: setAnimationScales [class [F] error. This is because of the non-sdk interface feature.

    private fun animationHandler() {
        try {
            val asInterface = Class.forName("android.view.IWindowManager\$Stub").getDeclaredMethod("asInterface", IBinder::class.java)

            val getService = Class.forName("android.os.ServiceManager").getDeclaredMethod("getService", String::class.java)

            val windowManagerClazz = Class.forName("android.view.IWindowManager")

            setAnimationScalesMethod = Class.forName("android.view.IWindowManager").getDeclaredMethod("setAnimationScales", FloatArray::class.java)
            getAnimationScalesMethod = windowManagerClazz.getDeclaredMethod("getAnimationScales")
            windowManagerObject = asInterface.invoke(null, getService.invoke(null, "window") as IBinder)
        } catch (e: Exception) {
            throw RuntimeException("Failed to access animation methods", e)
        }
    }

https://github.com/KazuCocoa/DroidTestHelper/blob/3781fa40a6c9dfd34e1cb94ae230a98931af3313/droidtesthelperlib/src/main/java/com/kazucocoa/droidtesthelperlib/HandleAnimations.kt#L23

To avoid the error, we can call below adb commands.

adb shell settings put global hidden_api_policy_pre_p_apps  1
adb shell settings put global hidden_api_policy_p_apps 1

After this, calling setAnimationScales succeeds.
I’ve appended the note in https://github.com/KazuCocoa/DroidTestHelper#note-for-android-p .

BTW, we can get P image from https://dl.google.com/android/repository/sys-img/google_apis_playstore/x86-P_r02.zip . After downloading it and putting it in /Users/username/Library/Android/sdk/system-images/android-P/android_apis_playstore, you can create Android P image via AVDManager.

1 Comment

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.