測試應用程式連結

導入應用程式連結功能時,請測試連結功能,確保系統能將應用程式與網站建立關聯,並按照預期處理網址要求。

如要測試現有的聲明檔案,可以使用聲明清單產生器和測試工具

以下各節說明如何手動測試應用程式連結驗證。您也可以選擇透過 Play 深層連結工具或 Android Studio 應用程式連結小幫手測試驗證。

確認要驗證的主機清單

測試時,請確認系統應為應用程式驗證的相關聯主機清單。請列出所有網址,這些網址的對應意圖篩選器包含下列屬性和元素:

  • 值為 httphttpsandroid:scheme 屬性
  • android:host 屬性,並包含網域網址模式
  • android.intent.action.VIEW 動作元素
  • android.intent.category.BROWSABLE 類別元素

請使用這份清單,確認每個具名主機和子網域都提供 Digital Asset Links JSON 檔案。

確認 Digital Asset Links 檔案

針對每個網站,使用 Digital Asset Links API 確認 Digital Asset Links JSON 檔案是否已正確代管及定義:

https://digitalassetlinks.googleapis.com/v1/statements:list?
   source.web.site=https://<var>domain.name</var>:<var>optional_port</var>&amp;
   relation=delegate_permission/common.handle_all_urls

如果是動態應用程式連結,您也可以檢查關係擴充功能。

https://digitalassetlinks.googleapis.com/v1/statements:list?source.web.site=https://www.example.com&relation=delegate_permission/common.handle_all_urls&return_relation_extensions=true

在測試過程中,您可以檢查目前的連結處理系統設定。使用下列指令,取得連線裝置上所有應用程式的現有連結處理政策清單:

adb shell dumpsys package domain-preferred-apps

下列指令會執行相同作業:

adb shell dumpsys package d

這項指令會傳回裝置上定義的每個使用者或設定檔清單,並在清單前加上下列格式的標頭:

App linkages for user 0:

在這個標頭之後,輸出內容會使用下列格式,列出該使用者的連結處理設定:

Package: com.android.vending
Domains: play.google.com market.android.com
Status: always : 200000002

這份清單會指出該使用者有哪些應用程式與哪些網域相關聯:

  • Package - 根據應用程式資訊清單中宣告的套件名稱識別應用程式。
  • Domains - 顯示這個應用程式處理的網頁連結主機完整清單,並以空白字元做為分隔符。
  • Status - 顯示這個應用程式目前的連結處理設定。如果應用程式通過驗證,且資訊清單包含 android:autoVerify="true",狀態會顯示為 always。這個狀態後方的十六進位數字,與 Android 系統記錄的使用者應用程式連結偏好設定有關。這個值不會指出驗證是否成功。

測試範例

如要成功驗證應用程式連結,系統必須能夠驗證應用程式與您在符合應用程式連結條件的指定意圖篩選器中,所指定的每個網站。以下範例顯示定義多個應用程式連結的資訊清單設定:

<activity android:name="MainActivity">
        <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" />
            <data android:scheme="https" />
            <data android:host="www.example.com" />
            <data android:host="mobile.example.com" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="https" />
            <data android:host="www.example2.com" />
        </intent-filter>
    </activity>

    <activity android:name="SecondActivity">
        <intent-filter>
            <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" />
            <data android:host="account.example.com" />
        </intent-filter>
    </activity>

      <activity android:name="ThirdActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <data android:scheme="https" />
            <data android:host="map.example.com" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="market" />
            <data android:host="example.com" />
        </intent-filter>
      </activity>

</application>

平台會嘗試從上述資訊清單驗證的主機清單如下:

www.example.com
mobile.example.com
www.example2.com
account.example.com

平台不會嘗試驗證下列主機:

map.example.com (it does not have android.intent.category.BROWSABLE)
market://example.com (it does not have either an "http" or "https" scheme)

如要進一步瞭解對帳單清單,請參閱「建立對帳單清單」。

自 Android 17 起,您可以使用活動管理員 (am start) 指令搭配 --debug-link 旗標,診斷系統如何解析特定網址。這項工具會詳細列出符合意圖的候選應用程式,以及在解析期間評估的應用程式資訊清單和 assetlinks.json 檔案 (適用於動態應用程式連結) 中的特定規則。

如要測試特定網址的連結解析,請在終端機視窗中執行下列指令:

adb shell am start --debug-link -a android.intent.action.VIEW -d "https://xyz.com/foo"

診斷輸出內容會印在 App Link Resolution Debug 標題下方,並包含下列章節,協助您瞭解解決程序:

  • 目標詳細資料:根據套件名稱和目標活動,識別每個相符的候選應用程式。
  • 意圖篩選器比對 (AndroidManifest.xml):顯示資訊清單意圖篩選器中哪些靜態屬性 (例如 schemehostpathpathPrefixpathPattern) 與 URI 相符。
  • 應用程式連結驗證:顯示目前的網域驗證狀態 (例如 STATE_SUCCESS)。
  • 動態應用程式連結:如果應用程式在 assetlinks.json 檔案中使用動態應用程式連結比對規則,這個部分會列出針對 URI 評估的每項規則。每項規則都會指出相符的 URI 篩選條件 (例如路徑前置字串或模式) 和 allow 欄位:
    • allow = 0允許/納入規則 (allow: true)。如果符合這項規則,應用程式就能開啟 URI。
    • allow = 1封鎖/排除規則 (allow: false / exclude: true)。如果符合這項規則,應用程式就無法開啟 URI。
    • 注意:空白篩選器字串 (filter =) 表示空白路徑前置字串,符合網域下的所有路徑 (做為萬用字元或全域比對)。

偵錯輸出範例

假設應用程式 (com.example.xyzapp) 與網域 https://xyz.com 相關聯,且在 assetlinks.json 檔案中定義動態規則,排除 /foo* 並允許所有其他路徑:

[
  {
    "relation": [
      "delegate_permission/common.handle_all_urls"
    ],
    "target": {
      "namespace": "android_app",
      "package_name": "com.example.xyzapp",
      "sha256_cert_fingerprints": ["..."]
    },
    "relation_extensions": {
      "delegate_permission/common.handle_all_urls": {
        "dynamic_app_link_components": [
          {"/": "/foo*", "exclude": true},
          {"/": "*"}
        ]
      }
    }
  }
]

使用 --debug-link 診斷網址 https://xyz.com/foo 時:

adb shell am start --debug-link -a android.intent.action.VIEW -d "https://xyz.com/foo"

這項指令會輸出下列診斷細目:

--- App Link Resolution Debug ---

URI: https://xyz.com/foo
Resolution: Ambiguous (Multiple apps or Browser fallback)
This usually happens when multiple apps can handle the link and no default is set.

All Matching Candidates:

Target:
  Package: com.example.xyzapp
  Activity: com.example.xyzapp.MainActivity

  Intent Filter Match (AndroidManifest.xml)
    Scheme: 'https' matched android:scheme="https"
    Host: 'xyz.com' matched android:host="xyz.com"

App Link Verification:
  Verification status: STATE_SUCCESS
  Dynamic App Links:
    -> Matched Rule 0: UriRelativeFilterGroup { allow = 1, uri_filters = {UriRelativeFilter { uriPart = PATH, patternType = PREFIX, filter = /foo }},  }
    -> Matched Rule 1: UriRelativeFilterGroup { allow = 0, uri_filters = {UriRelativeFilter { uriPart = PATH, patternType = PREFIX, filter =  }},  }

Target:
  Package: org.chromium.webview_shell
  Activity: org.chromium.webview_shell.WebViewBrowserActivity

  Intent Filter Match (AndroidManifest.xml)
    Scheme: 'https' matched android:scheme="https"

---------------------------------

Starting: Intent { act=android.intent.action.VIEW dat=https://xyz.com/foo }

在本例中,系統評估了 assetlinks.json 中的兩項動態應用程式連結規則:

  • 規則 0 (allow = 1filter = /foo):{"/": "/foo*", "exclude": true} 產生,這是排除規則 (allow: false),會封鎖以 /foo 路徑前置字元開頭的網址。
  • 規則 1 (allow = 0filter =):{"/": "*"} 產生,這是包含規則 (allow: true),具有空白路徑前置字元 (filter =),可比對 xyz.com 下的所有路徑 (適用於所有路徑)。

在此情境中,解析度運作方式如下:

  1. 規則 0 和規則 1 都符合網址 https://xyz.com/foo
  2. 系統會由上至下依序評估動態應用程式連結規則 (並套用相符的第一項規則)。
  3. 由於規則 0 在陳述式清單中排在第一位,且是排除規則 (allow = 1),因此優先於一般允許規則 (規則 1)。
  4. 因此,應用程式會排除處理 https://xyz.com/foo,導致系統回復為瀏覽器或顯示消歧義對話方塊。