こんにちはKID.Aです。
前回の「[Android 6.0] App Linksについて」の記事でApp Linksの認証を試しましたが、うまくいきませんでしたので少し調査をしました。
まずは、以下のマニフェストを設定した場合の挙動を確認します。
<activityandroid:name=".SecondActivity" android:label="@string/title_activity_second"> <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="http" android:host="www.targethost.com" /> </intent-filter> </activity>
このアプリをインストールします。
adb install -r app-release.apk
インストール後、logcatに以下のログがでます。
I/SingleHostAsyncVerifier: Verification result: checking for a statement with source a < a: "https://www.targethost.com" > , relation delegate_permission/common.handle_all_urls, and target b < a: "jp.co.applinkstest" b < a: "56:E5:74:35:D2:A6:3C:4A:ED:B1:15:F2:45:F6:B0:0B:FC:4C:A7: E1:2C:F9:A0:C9:CE:D0:84:CF:C7:E9:5F:41" > > --> false. D/SingleHostAsyncVerifier: Remaining verification failures before we've exhausted all certs: 0 D/statementservice.ResultFuture: setResult on host: a < a: "https://www.targethost.com" > : false
ログをみるとhttps://www.targethost.comに認証がされているのがわかります。
設定ファイルにhttpだけ記載しても認証の接続先はhttpsになります。
android:autoVerify=”true”にする場合はhttpsが有効のサーバを用意する必要になります。
また、上記の通信時にはサーバでhttpsを無効にしていました。そのため、認証が失敗しています。
認証が失敗していればVerification resultがfalseとなり一目瞭然にわかります。
サーバのhttpsを有効にして認証が成功すると以下のようなログがでます。
D/IntentFilterVerificationReceiver: Received ACTION_INTENT_FILTER_NEEDS_VERIFICATION. I/IntentFilterIntentService: Verifying IntentFilter. verificationId: 25 scheme:"https" hosts:"www.targethost.com" package:"jp.co.applinkstest". I/SingleHostAsyncVerifier: Verification result: checking for a statement with source a < a: "https://www.targethost.com" > , relation delegate_permission/common.handle_all_urls, and target b < a: "jp.co.applinkstest" b < a: "56:E5:74:35:D2:A6:3C:4A:ED:B1:15:F2:45:F6:B0:0B:FC:4C:A7: E1:2C:F9:A0:C9:CE:D0:84:CF:C7:E9:5F:41" > > --> true. D/statementservice.ResultFuture: setResult on host: a < a: "https://www.targethost.com" > : true I/IntentFilterIntentService: Verification 25 complete. Success:true. Failed hosts:.
認証が成功した状態でdumpsysを使い認証の状態をみてみます。
adb shell dumpsys package domain-preferred-apps
・結果
App verification status: Package: jp.co.applinkstest Domains: www.targethost.com Status: undefined
Statusはundefinedになっています。
本来はalwaysに変わるはずですが、ここは認証の成否に問わず変わらなかったです。
Statusは認証が成功・失敗時にも変わりませんでしたが、以下のブロードキャストを投げると認証が成功している場合と成功していない場合で挙動が違うことが確認できました。
adb shell am start -a android.intent.action.VIEW \ -c android.intent.category.BROWSABLE \ -d "http://www.targethost.com/"
・https://www.targethost.comと認証が成功していない場合の結果
・https://www.targethost.comと認証が成功している場合の結果
まとめとしてApp Linksの認証をおこなう場合、以下の点を気をつけます
・intent-filterに記載するhostはhttps対応にすること
・App verification statusがundefinedでも認証が成功している可能性があります
また確認方法としては
・アプリインストール時にlogcat上でVerification resultをみて結果を確認する
・認証が成功していたら、dumpsysでなくてブロードキャストで直接確認する
とした方がよさそうです。