(#) Restricted API !!! ERROR: Restricted API This is an error. Id : `RestrictedApi` Summary : Restricted API Severity : Error Category : Correctness Platform : Any Vendor : Android Open Source Project Feedback : https://issuetracker.google.com/issues/new?component=192708 Affects : Kotlin and Java files Editing : This check runs on the fly in the IDE editor 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/RestrictToDetector.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/RestrictToDetectorTest.kt) Copyright Year : 2017 This API has been flagged with a restriction that has not been met. Examples of API restrictions: * Method can only be invoked by a subclass * Method can only be accessed from within the same library (defined by the Gradle library group id) * Method can only be accessed from tests. You can add your own API restrictions with the `@RestrictTo` annotation. (##) Example Here is an example of lint warnings produced by this check: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text src/test/pkg/RestrictToSubclassTest.java:26:Error: Class1.onSomething can only be called from subclasses [RestrictedApi] cls.onSomething(); // ERROR: Not from subclass ----------- src/test/pkg/RestrictToSubclassTest.java:27:Error: Class1.counter can only be accessed from subclasses [RestrictedApi] int counter = cls.counter; // ERROR: Not from subclass ------- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Here is the source file referenced above: `src/test/pkg/RestrictToSubclassTest.java`: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~java linenumbers package test.pkg; import androidx.annotation.RestrictTo; @SuppressWarnings("ClassNameDiffersFromFileName") public class RestrictToSubclassTest { public static class Class1 { @RestrictTo(RestrictTo.Scope.SUBCLASSES) public void onSomething() { } @RestrictTo(RestrictTo.Scope.SUBCLASSES) public int counter; } public static class SubClass extends Class1 { public void test1() { onSomething(); // OK: Call from subclass int counter = cls.counter; // OK: Reference from subclass } } @SuppressWarnings("MethodMayBeStatic") public static class NotSubClass { public void test2(Class1 cls) { cls.onSomething(); // ERROR: Not from subclass int counter = cls.counter; // ERROR: Not from subclass } } } ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 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/RestrictToDetectorTest.kt) for the unit tests for this check to see additional scenarios. (##) 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("RestrictedApi") fun method() { problematicStatement() } ``` or ```java // Java @SuppressWarnings("RestrictedApi") void method() { problematicStatement(); } ``` * Using a suppression comment like this on the line above: ```kt //noinspection RestrictedApi 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="RestrictedApi" 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 'RestrictedApi' } ``` In Android projects this should be nested inside an `android { }` block. * For manual invocations of `lint`, using the `--ignore` flag: ``` $ lint --ignore RestrictedApi ...` ``` * Last, but not least, using baselines, as discussed [here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).