Search code examples
androidkotlinandroid-11

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


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?


Solution

  • 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()