Consider adding a queries declaration to your manifest when calling this method when using intent.resolveActivity in android 11

Viewed 8679

I've an extension function for opening an intent for my activities:

fun Activity.openIntent(action: String?, type: String?, uri: Uri?) {
    Intent()
        .apply {
            action?.let { this.action = it }
            uri?.let { this.data = it }
            type?.let { this.type = it }
        }
        .also { intent ->
            packageManager?.let {
                if (intent.resolveActivity(it) != null)
                    startActivity(intent)
                else
                    showToast(R.string.application_not_found)
            }
        }
}

My targetSdkVersion is 30. It gives me a warning in intent.resolveActivity(it):

Consider adding a queries declaration to your manifest when calling this method.

So What should I do to solve this warning?

3 Answers

The simplest solution is to get rid of resolveActivity(). Replace:

            packageManager?.let {
                if (intent.resolveActivity(it) != null)
                    startActivity(intent)
                else
                    showToast(R.string.application_not_found)
            }

with:

            try {
                startActivity(intent)
            } catch (ex: ActivityNotFoundException) {
                showToast(R.string.application_not_found)
            }

This gives you the same result with a bit better performance, and it will get rid of the warning.

Another option would be to add the QUERY_ALL_PACKAGES permission. This may get you banned from the Play Store.

Otherwise, you will need to:

  • Build a list of every possible openIntent() call that your app may make

  • Add a <queries> element to your manifest that lists all of those possible Intent structures that you want to be able to use with resolveActivity()

  • Repeat this process periodically, in case you add new scenarios for openIntent()

So starting Android 11 (i.e, if your app targets Android 11) not all applications will be visible to your application. Some apps are visible by default but in order to access other applications through your application, you will have to declare queries in your manifest else your application will not be able to access them. You can read about that here.

So if your application targets Android 11 and is to access an application that may not be visible by default you will want to add queries for them in the manifest file.

In your case, this warning is not applicable as I believe you are using implicit intents to open other applications. Using implicit intents, other applications can be accessed irrespective of app visibility. If your app target Android 10 or lower you can suppress the warning as all apps are visible by default.

To suppress the lint warning you can either:

  1. Add the suppress annotation, like so:
@SuppressLint("QueryPermissionsNeeded")
fun Activity.openIntent(action: String?, type: String?, uri: Uri?): Activity {
  1. Add the following to your android block in your app module build gradle file:
lintOptions {
    ignore "QueryPermissionsNeeded"
}

Replace

if (intent.resolveActivity(it) != null)

with

if (it.resolveActivity(intent, 0) != null)

and the warning will be gone.

Related