Sub topic 7
Setting Up Android App Links
This chapter assumes you know basic Dart and Flutter, can edit Android project files, and understand URL parts such as a scheme, host, and path. You also need an HTTPS domain that you control.
Android App Links let Android open verified HTTPS URLs directly in your Flutter app. For example, a user who taps https://example.com/products/42 can go straight to product 42 instead of seeing a browser choice dialog.
How verification works
An Android App Link needs two matching declarations:
- An intent filter in the app says which URLs the app can handle.
- An
assetlinks.jsonfile on the website proves that the website authorizes the app.
Android compares the app's package name and signing certificate fingerprint with the values on the website.
Add the intent filter
Open android/app/src/main/AndroidManifest.xml. Add the following intent-filter inside the app's main activity. The activity already has an <activity> element generated by Flutter.
<activity
android:name=".MainActivity"
android:exported="true"
android:launchMode="singleTop"
android:theme="@style/LaunchTheme">
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="https"
android:host="example.com"
android:pathPrefix="/products" />
</intent-filter>
</activity>android:autoVerify="true" asks Android to verify the domain during app installation. The scheme, host, and pathPrefix limit the URLs that match. This example handles HTTPS URLs on example.com whose path begins with /products.
The manifest must also contain the Android namespace on its root element:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.app">
<!-- application and activity declarations -->
</manifest>Use your real package name and domain. A broad filter such as the host alone may send URLs to your app that it does not know how to display.
Publish `assetlinks.json`
Create this file at:
https://example.com/.well-known/assetlinks.json
[
{
"relation": [
"delegate_permission/common.handle_all_urls"
],
"target": {
"namespace": "android_app",
"package_name": "com.example.app",
"sha256_cert_fingerprints": [
"12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF"
]
}
}
]Replace the package name and placeholder fingerprint. The fingerprint must belong to the certificate that signs the installed app. Debug, internal, and production builds often use different certificates, so list each required fingerprint.
The server must return the file over HTTPS with valid JSON. Redirects, authentication, an incorrect content type, or a typo in the package name can prevent verification.
> callout warning > Do not use a debug certificate fingerprint for a production build. The installed release APK or app bundle must be signed by a certificate represented in assetlinks.json.
Connect the verified URL to Flutter
Android only delivers the URL to the application. Flutter still needs a router that maps /products/42 to a product screen. With go_router, the route can read the product ID from the path:
GoRoute(
path: '/products/:id',
builder: (context, state) {
final id = state.pathParameters['id']!;
return ProductScreen(id: id);
},
)Keep platform matching and Flutter routing consistent. If Android accepts /products, but the Flutter router only accepts /items, the app will open without showing the intended screen.
Exercise
Which two values must match between the installed Android app and assetlinks.json?
Show answer
The app's package name and the SHA-256 fingerprint of its signing certificate.