(#) Using APIs affected by query permissions
!!! WARNING: Using APIs affected by query permissions
This is a warning.
Id
: `QueryPermissionsNeeded`
Summary
: Using APIs affected by query permissions
Severity
: Warning
Category
: Correctness
Platform
: Android
Vendor
: Android Open Source Project
Feedback
: https://issuetracker.google.com/issues/new?component=192708
Since
: 4.1.0 (October 2020)
Affects
: Kotlin and Java files and manifest files
Editing
: This check runs on the fly in the IDE editor
See
: https://g.co/dev/packagevisibility
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/PackageVisibilityDetector.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/PackageVisibilityDetectorTest.kt)
Apps that target Android 11 cannot query or interact with other
installed apps by default. If you need to query or interact with other
installed apps, you may need to add a `` declaration in your
manifest.
As a corollary, the methods `PackageManager#getInstalledPackages` and
`PackageManager#getInstalledApplications` will no longer return
information about all installed apps. To query specific apps or types of
apps, you can use methods like `PackageManager#getPackageInfo` or
`PackageManager#queryIntentActivities`.
(##) Example
Here is an example of lint warnings produced by this check:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~text
src/test/pkg/MainActivity.kt:14:Warning: As of Android 11, this method
no longer returns information about all apps; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.getInstalledPackages(0) // ERROR
--------------------
src/test/pkg/MainActivity.kt:15:Warning: As of Android 11, this method
no longer returns information about all apps; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.getInstalledApplications(0) // ERROR
------------------------
src/test/pkg/MainActivity.kt:17:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.queryBroadcastReceivers(Intent(), 0) // ERROR
-----------------------
src/test/pkg/MainActivity.kt:18:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.queryContentProviders("", 0, 0) // ERROR
---------------------
src/test/pkg/MainActivity.kt:19:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.queryIntentServices(Intent(), 0) // ERROR
-------------------
src/test/pkg/MainActivity.kt:20:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
pm.queryIntentActivities(Intent(), 0) // ERROR
---------------------
src/test/pkg/MainActivity.kt:22:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
Intent().resolveActivity(pm) // ERROR
---------------
src/test/pkg/MainActivity.kt:23:Warning: Consider adding a
declaration to your manifest when calling this method; see
https://g.co/dev/packagevisibility for details [QueryPermissionsNeeded]
Intent().resolveActivityInfo(pm, 0) // ERROR
-------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Here is the source file referenced above:
`src/test/pkg/MainActivity.kt`:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~kotlin linenumbers
package test.pkg
import android.app.Activity
import android.content.Intent
import android.os.Bundle
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val context = applicationContext
val pm = context.packageManager
pm.getInstalledPackages(0) // ERROR
pm.getInstalledApplications(0) // ERROR
pm.queryBroadcastReceivers(Intent(), 0) // ERROR
pm.queryContentProviders("", 0, 0) // ERROR
pm.queryIntentServices(Intent(), 0) // ERROR
pm.queryIntentActivities(Intent(), 0) // ERROR
Intent().resolveActivity(pm) // ERROR
Intent().resolveActivityInfo(pm, 0) // ERROR
this.getInstalledPackages() // OK
this.resolveActivity() // OK
}
private fun getInstalledPackages() = Unit
private fun resolveActivity() = Unit
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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/PackageVisibilityDetectorTest.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, `PackageVisibilityDetector.testCannotQueryPackages`.
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("QueryPermissionsNeeded")
fun method() {
getInstalledPackages(...)
}
```
or
```java
// Java
@SuppressWarnings("QueryPermissionsNeeded")
void method() {
getInstalledPackages(...);
}
```
* Using a suppression comment like this on the line above:
```kt
//noinspection QueryPermissionsNeeded
problematicStatement()
```
* Adding the suppression attribute
`tools:ignore="QueryPermissionsNeeded"` on the problematic XML
element (or one of its enclosing elements). You may also need to add
the following namespace declaration on the root element in the XML
file if it's not already there:
`xmlns:tools="http://schemas.android.com/tools"`.
```xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:tools="http://schemas.android.com/tools">
...
<uses-permission tools:ignore="QueryPermissionsNeeded" .../>
...
</manifest>
```
* 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="QueryPermissionsNeeded" 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 'QueryPermissionsNeeded'
}
```
In Android projects this should be nested inside an `android { }`
block.
* For manual invocations of `lint`, using the `--ignore` flag:
```
$ lint --ignore QueryPermissionsNeeded ...`
```
* Last, but not least, using baselines, as discussed
[here](https://googlesamples.github.io/android-custom-lint-rules/usage/baselines.md.html).