Search code examples
androidandroid-9.0-pie

Android P non SDK interface restrictions applicability to android.support and/or androidx


We all know and love the "Restrictions on non SDK interfaces". That page has this to say about the definition of the term:

Generally speaking, SDK interfaces are those ones found documented in the Android framework Package Index.

The package menu on the left side of this package index has a first section titled "Android Platform", then a separate section called "Android Support Library". And the blacklist here doesn't have any symbols in android.support.

So, is it absolutely certain that the non SDK interface policy applies only to classes in "Android Platform"? And will never by design include Androidx?


Solution

  • Well, yes.

    The support library is there to help developers target wider audiences without sacrificing functionality. @hide APIs exist in the native platform because they're not supposed to be used by apps. The support library's sole reason for existing is to be used by apps.

    The native SDK is an SDK. It's there to tell Android Studio which classes, methods and constants exist already exist in Android, in the framework.jar. None of it is actually built into your APK. Think of the native SDK (and any SDK) as a sort of "promise" to Android Studio that what you're doing will actually work when it's run. The SDK also depends on what actually exists on the device. If the SDK has a method the device doesn't, Android Studio will compile, but the app will crash on that device when attempting to call that method.

    When a class, method or variable has the @hide flag in AOSP, the SDK compiler Google uses simply removes that class, method or variable from the SDK JAR it builds. That means that Android Studio has no "promise" that these things exist (as far as it knows, they don't), so it won't build. However, those methods still exist on the device. This is why, up until Pie, you could just use reflection to access these hidden parts of Android.

    Pie introduced a "security" "feature" that checks whether the method, class or variable an app is using is blacklisted. If it is, when the app attempts access, Android throws a SecurityException, if the app isn't a system app. There's a sort of gatekeeper between your app and and the framework.

    However, AndroidX isn't an SDK. It's a library. Libraries are compiled into your APK, as their components don't already exist on the target device(s). I have seen a few methods in AndroidX annotated with @hide, but I'm pretty sure this was a mistake. The Gradle compiler, at least by default, doesn't remove anything with @hide, and it wouldn't make sense to, since the library only exists inside your APK.