Package visibility for url_launcher on Android 11 (API 30)
TL;DR: You can skip if you don’t use the `url_launcher` package or your app doesn’t target Android 11 and above.
Problem
Android 11 introduced a tighter security when interacting with other apps — the package visibility concept.
If you have used the url_launcher
package recently, you may have realized that canLaunch
always returns false
on Android 11 (API 30) and launchUrl
will fail.
Solution
This can be fixed by configuring your application to make other applications that have the android.intent.action.VIEW
intent filter using <queries>
element in the AndroidManifest.xml.
For the <queries>
element to work, it is recommended you upgrade your Android Gradle Plugin (AGP) to version 4.1.
In your project level gradle file, android/build.gradle,
do as shown below:
buildscript {
repositories {
google()
jcenter()
}
dependencies {
// Using gradle plugin version 4.1.1
classpath 'com.android.tools.build:gradle:4.1.1'
}
}
There are other dot versions for earlier versions of the Android Gradle Plugins. Find out more from here.
After upgrading the gradle plugin and building the app, add this element block to your AndroidManifest.xml
:
After doing this, rebuild the app and canLaunch
should return true
and launchUrl
should also open the url on Android 11 (API 30).
That was pretty quick. Easy peasy ain’t it? 😁
Edit:
The AGP v4.1.0+ requires v6.5+ of gradle and due to some issues, it may cause your release build to get stuck on the splash screen. Using AGP v3.5.4 fixed this issue for me. Like so:
buildscript {
...
dependencies {
// Using gradle plugin version 3.5.4
classpath 'com.android.tools.build:gradle:3.5.4'
}
}