Verify App Links on Android 12 and higher

NgocLan Luu-Thi
5 min readOct 16, 2022

--

App links is a topic that an Android developer must be working on, but have you ever run into the weird scenario where, after configuration was completed, you self-test on a device and a simulator, and everything is good, till the tester reopens the task because the app link is not working on their device. LoL! And I still try to show evidence that it’s working right!!! 🤭🤭🤭

https://giphy.com/embed/rIpm4UI7pv7WFiSm4s on Giphy.com

And here are some tips to solve the problem:

I. Find the root cause

1. Recognize: the device that cannot open App links: Samsung A71 Android 12

2. Check config code:

  • Check config on AndroidManifest.xml file
<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" />
<!-- Ex URIs "http://app-link.com/demo?id=255” -->
<data
android:host="app-link.com"
android:scheme="http" />
</intent-filter>
  • Check the handling intent on MainActivity.kt
private fun handleIntent(intent: Intent) {
val appLinkAction = intent.action
val appLinkData: Uri? = intent.data
if (Intent.ACTION_VIEW == appLinkAction) {
appLinkData?.lastPathSegment?.also { lastPath ->
when (lastPath) {
"demo" -> {
appLinkData.getQueryParameter("id")?.also {
//Handle logic
findViewById<TextView>(R.id.tvContent).text=
"Data get from App link ID = $it"
}
}
}
}
}
}

This is a sample project with a single activity that has a TextView to display a message.

Figure 1: App links work right on Android 11

As you can see, the flow app works right on Android 11. But with Android 12…no action when I click on the link, and why?

After finding the cause of the problem - why Android 12 cannot open with App links, I found this note in the documentation of the Android developer:

On apps that target Android 12, the system makes several changes to how Android App Links are verified. These changes improve the reliability of the app-linking experience and provide more control to app developers and end users. You can manually invoke domain verification to test the reliability of your declarations.

That is a big change on Android 12 and higher! I missed!!!

II. Find solutions

  1. Manual:

At that time, the good news is with Android 12 and higher, users can change the settings: App info -> Open by default -> Add link.

Figure 2: Manually enable the detect the app links on Android 12 Emulator

From here, users can select the domains that they want to associate with your app, as shown in Figure 2. Later select any link within the domains they add, and the link automatically opens in your app. That is a rapid solution for the dev to have a self-test.

🙂 Is there another way that I can make app links verified automatically?

2. Make the assetlinks.json to verify the App links

Let’s compare my app with Twitter as well! Until the switch button is hidden at the tail of the domain name, your app is guaranteed verified!

Figure 3: The difference between Twitter and my app on settings info — one verified and non-verified links

With the solution below you need help from your infrastructure team, there are 3 steps:

👉 Step 1: Configure on <intent-filter> on Android 12 and higher, to detect http/https scheme you can’t use Web links, instead, you should use App links that include android:autoVerify=”true” and this will notify the system to check whether the domain is verified or not.

<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" />
<!-- Ex URIs "http://app-link.com/demo?id=255” -->
<data
android:host="app-link.com"
android:scheme="http" />
</intent-filter>

👉 Step 2: Generate a assetlinks.json digital asset file to create a link between the host and the application, what should I prepare?

Option 1Create the assetlinks.json file manually

  • Create an empty file named assetlinks.json
  • Place the following information inside this file template.
[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "<Your App’s package name>",
"sha256_cert_fingerprints":
["<Your App’s SHA256 finger print>"]
}
}]

Just replace 🧑‍💻

  • package_name
  • sha256_cert_fingerprint

If you have several applications or flavors utilizing the same domain, include them in the file.

[{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.app.demo.dev",
"sha256_cert_fingerprints":
["<Your App’s SHA256 finger print>"]
}
},
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.app.demo.qa",
"sha256_cert_fingerprints":
["<Your App’s SHA256 finger print>"]
}
},
{
"relation": ["delegate_permission/common.handle_all_urls"],
"target": {
"namespace": "android_app",
"package_name": "com.app.applink",
"sha256_cert_fingerprints":
["<Your App’s SHA256 finger print>"]
}
]

Generate the sha256_cert_fingerprints by using the command (ensure to create and configure a Keystore for your app first)

keytool -list -v -keystore app-link-release-key.keystore
Figure 4: Use the command to get SHA-256 and the result

Option 2 — Use Android Studio’s App Link Assistant

On the menu of Android Studio: Tools -> App Links Assistant -> Open Digital Assets Links File Generator, and fill below information -> Click Generate Digital Assets Link File.

Figure 5: Use App Links Assistant to generate assetlinks.json

Option 3 — Use Google’s statement list generator tool

Figure 6: Use Google tools generator to generate content

👉 Step 3: Publish the assetlinks.json file (for the infrastructure team) at the location:

https://domain.name/.well-known/assetlinks.json
Gif on Giphy.com

Don’t forget to extend a special thank you to your infrastructure team! :)

3. How to make sure that it works?

Figure 7: Result when using the Google Digital Asset Links tools
  • Check on your App: After having confirmed that the hosted JSON file is valid, install the app on your device with the build type Release (important). Wait at least 20 seconds for the asynchronous verification process to complete. Use the command below to check whether the system verified your app, if true, your app will be opened.
adb shell am start -a android.intent.action.VIEW \
-c android.intent.category.BROWSABLE \
-d "http://domain.name:optional_port"
  • Check the status on App Info: As I have mentioned above, go to App Info, and have no switch button appear following the domain name.

Alright, checks completed! You can now confidently send your app to the testers for another round of verification.

III. Conclusion

Here are the key takeaways from this post:

  • Ensure that android:autoVerify=”true” in the Android Manifest within intent filters, that respond to intents with http/https scheme on Android 12 and higher.
  • Generate an assetlinks.json digital asset file to create a link between the host and the application.
  • Deploy the assetlinks.json at location: https://domain.name/.well-known/assetlinks.json
  • Review the verification results and continue test flow app links on your app.

References

#Android #AppLinks

--

--