Open links in browser with flutter
Posted on December 4, 2020 • 1 minutes • 195 words
When using the url_launcher package for flutter, you may have run into the problem that canLaunch
always returns false
.
That’s because starting with Android 11 (API level 30) you have to explicitly request to „see“ other apps from yours. This is called package visibility and is explained in more detail here.
So we have to say, that we want to see apps that are capable of browsing the web. How do we do that?
In the AndroidManifest.xml
we add the following:
<manifest>
<queries>
<intent>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="https" />
</intent>
</queries>
</manifest>
There is also a page on the android docs that explains this in further detail.
And now you rebuild your app aaand … maybe get the next error:
https://android-developers.googleblog.com/2020/07/preparing-your-build-for-package-visibility-in-android-11.htmlAndroid resource linking failed /Users/sample/AndroidStudioProjects/MyApp/app/build/intermediates/merged_manifests/debug/AndroidManifest.xml:18: error: unexpected element <queries> found in <manifest>
That’s because you have to upgrade your gradle android plugin version as written here.
For that open the file build.gradle
in your android
folder and change the following line accordingly:
dependencies {
classpath 'com.android.tools.build:gradle:3.5.0'
In this case I upgraded mine from 3.5.0
to 3.5.4
which now includes support for the queries tag in the AndroidManifest.xml
.
Rebuild and your done!