(#) Using Private APIs !!! WARNING: Using Private APIs This is a warning. Id : `PrivateApi` Summary : Using Private APIs Severity : Warning Category : Correctness Platform : Android Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Since : 3.0.0 (October 2017) Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor See : https://developer.android.com/preview/restrictions-non-sdk-interfaces Implementation : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-checks/src/main/java/com/android/tools/lint/checks/PrivateApiDetector.kt) Tests : [Source Code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PrivateApiDetectorTest.kt) Using reflection to access hidden/private Android APIs is not safe; it will often not work on devices from other vendors, and it may suddenly stop working (if the API is removed) or crash spectacularly (if the API behavior changes, since there are no guarantees for compatibility). (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/myapplication/ReflectionTest1.java:8:Warning: Accessing internal APIs via reflection is not supported and may not work on all devices or in the future [PrivateApi] Class<?> c = Class.forName("com.android.internal.widget.LockPatternUtils"); // ERROR ------------------------------------------------------------- src/test/pkg/myapplication/ReflectionTest1.java:9:Warning: Accessing internal APIs via reflection is not supported and may not work on all devices or in the future [PrivateApi] int titleContainerId = (Integer) Class.forName("com.android.internal.R{$}id").getField("title_container").get(null); -------------------------------------------- src/test/pkg/myapplication/ReflectionTest1.java:11:Warning: Accessing internal APIs via reflection is not supported and may not work on all devices or in the future [PrivateApi] Class SystemProperties = cl.loadClass("android.os.SystemProperties"); ------------------------------------------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/myapplication/ReflectionTest1.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg.myapplication; import android.app.Activity; public class ReflectionTest1 extends Activity { public void testForName(ClassLoader cl) throws Exception { Class.forName("java.lang.String"); // OK Class> c = Class.forName("com.android.internal.widget.LockPatternUtils"); // ERROR int titleContainerId = (Integer) Class.forName("com.android.internal.R{$}id").getField("title_container").get(null); @SuppressWarnings("rawtypes") Class SystemProperties = cl.loadClass("android.os.SystemProperties"); } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ You can also visit the [source code](https://cs.android.com/android-studio/platform/tools/base/+/mirror-goog-studio-main:lint/libs/lint-tests/src/test/java/com/android/tools/lint/checks/PrivateApiDetectorTest.kt) for the unit tests for this check to see additional scenarios. The above example was automatically extracted from the first unit test found for this lint check, `PrivateApiDetector.testForNameOnInternalClass`. To report a problem with this extracted sample, visit https://issuetracker.google.com/issues/new?component=192708. (##) Suppressing You can suppress false positives using one of the following mechanisms: * Using a suppression annotation like this on the enclosing element: ```kt // Kotlin @Suppress("PrivateApi") fun method() { forName(...) } ``` or ```java // Java @SuppressWarnings("PrivateApi") void method() { forName(...); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection PrivateApi problematicStatement() ``` * Using a special `lint.xml` file in the source tree which turns off the check in that folder and any sub folder. A simple file might look like this: ```xml <?xml version="1.0" encoding="UTF-8"?> <lint> <issue id="PrivateApi" severity="ignore" /> </lint> ``` Instead of `ignore` you can also change the severity here, for example from `error` to `warning`. You can find additional documentation on how to filter issues by path, regular expression and so on [here](https://googlesamples.github.io/android-custom-lint-rules/usage/lintxml.md.html). * In Gradle projects, using the DSL syntax to configure lint. For example, you can use something like ```gradle lintOptions { disable 'PrivateApi' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore PrivateApi ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).