アプリ開発備忘録

PlayStationMobile、Android、UWPの開発備忘録

【Android】URLをタップしたらアプリを開くようにWebでAppLinkの検証設定を行う

以前のAndroidでは、URLをアプリに設定するだけで、URLをタップするとアプリが開くように設定できました。
しかし、Android12からはiOSのように、Webサイト側に設定を行わないと自動でアプリが開かないようになりました。
https://developer.android.com/about/versions/12/web-intent-resolution

以下のように、手動でやると開くようにはなります。

設定をする

https://developer.android.com/training/app-links/verify-site-associations

Android

まずは AndroidManifest.xml 側です。
android:autoVerify="true" を付けます。
複数ドメイン中、1つだけとかでも大丈夫です。所有しているドメインだけに記述しましょう。

<activity>
    <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:host="matsudamper.net" android:pathPrefix="/" android:scheme="https" />
    </intent-filter>
</activity>

Web

https://domain.name/.well-known/assetlinks.json にファイルを置きます。 置くファイルはAndroid Studio上で生成できます。

App Linksと入力すると、画面が出ます。

既に intent-filter が設定されているものとして、3を開きます。

ドメイン名とpackage name、debugかrelease等を選択すると、下のウィンドウにjsonが出力されます。

この出力されたjsonhttps://domain.name/.well-known/assetlinks.json に置けば完了です。

[
    {
        "relation": [
            "delegate_permission/common.handle_all_urls"
        ],
        "target": {
            "namespace": "android_app",
            "package_name": "net.matsudamper.app",
            "sha256_cert_fingerprints": [
                "00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00"
            ]
        }
    }
]

自分は、デバッグ時にprefixに .debug を付けています。LINEログインはpackage nameが違うとログインできないので、debugとreleaseの両方のfingerprintを記述しています。

[
    {
        "relation": [
            "delegate_permission/common.handle_all_urls"
        ],
        "target": {
            "namespace": "android_app",
            "package_name": "net.matsudamper.app",
            "sha256_cert_fingerprints": [
                "release用のキーのsha256_cert_fingerprints",
                "debug用のキーのsha256_cert_fingerprints"
            ]
        }
    },
    {
        "relation": [
            "delegate_permission/common.handle_all_urls"
        ],
        "target": {
            "namespace": "android_app",
            "package_name": "net.matsudamper.app.debug",
            "sha256_cert_fingerprints": [
                "release用のキーのsha256_cert_fingerprints",
                "debug用のキーのsha256_cert_fingerprints"
            ]
        }
    }
]

設定完了

設定ができると、このような表示になります。