應用程式連結是使用 HTTP 或 HTTPS 配置的深層連結,且經過 Android 驗證與您的網站相關聯。如要註冊處理應用程式連結,請按照下列步驟操作:
- 在應用程式資訊清單中新增一或多個意圖篩選器,指定網站網域或網址。
- 將
autoVerify="true"attribute新增至意圖篩選器元素。這會向系統發出信號,表示應嘗試根據網站的assetlinks.json設定驗證架構和主機網域。 - 宣告網站關聯。
以下是應用程式連結宣告的範例,其中包含架構和主機,以及 autoVerify="true:
<activity
android:name=".MainActivity"
android:exported="true"
...>
<!-- Make sure you explicitly set android:autoVerify to "true". -->
<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" />
<!-- If a user clicks on a link that uses the "http" scheme, your
app should be able to delegate that traffic to "https". -->
<!-- Do not include other schemes, as this will prevent verification. -->
<data android:scheme="http" />
<data android:scheme="https" />
<!-- Include one or more domains that should be verified. -->
<data android:host="www.example.com" />
<data android:host="*.example.com" />
</intent-filter>
</activity>
程式碼重點
- AutoVerify:應用程式連結必須使用
android:autoVerify="true" 屬性。這會向系統發出信號,表示應嘗試驗證應用程式與<data>標記中指定的架構和網域之間的關聯。建議您為每個要驗證的 Intent 篩選器新增autoVerify="true"。 - 資料元素:每個應用程式連結意圖篩選器都必須包含一或多個
<data>元素,指定與可驗證網站網域相符的架構和主機格式。 - 架構:意圖篩選器必須包含
http和https架構的<data>元素。 主機:您可以選擇新增
<data>元素,比對一或多個主機。使用萬用字元 (*) 比對多個子網域 (例如*.example.com)。系統會嘗試根據網站上的 assetlinks.json 檔案驗證每個主機。請注意,任何路徑層級的路由都應由 assetlinks.json 檔案處理 (請參閱下方的最佳做法一節)。多個主機:如果您宣告多個主機網域,系統 (Android 12 以上版本) 會嘗試驗證每個網域。如果驗證任何主機,應用程式就會成為該主機連結的預設處理常式。在 Android 11 以下版本,只要有一個主機無法驗證,驗證就會失敗。
多個意圖篩選器:如要宣告不重複的網址 (例如特定架構和主機組合),請務必建立個別篩選器,因為同一個意圖篩選器中的多個
<data>元素會合併在一起,以反映組合屬性的所有變化版本。
資訊清單篩選規則注意事項
如果您在 Android 15 以上版本中設定篩選器,以便搭配動態應用程式連結使用,請務必注意,伺服器端 assetlinks.json 檔案中宣告的動態規則,無法擴大您在應用程式資訊清單中靜態宣告的網址規則範圍。
因此,我們建議採用下列做法:
- 在應用程式資訊清單中,盡可能設定最廣泛的範圍,例如只宣告架構和網域
- 依據伺服器端的 assetlinks.json 規則進一步調整,例如路徑層級的路由。
有了這個理想的設定,您就能視需要動態新增 assetlinks.json 檔案中的新應用程式連結路徑,並確知這些路徑會符合您在應用程式資訊清單中設定的廣泛範圍。
支援多個主機的應用程式連結
系統必須能夠根據該意圖篩選器中相應網域上託管的 Digital Asset Links 檔案,驗證應用程式網址意圖篩選器資料元素中指定的主機。如果驗證失敗,系統會預設採用標準行為來解析意圖,如「建立應用程式內容的深層連結」一文所述。不過,系統仍可將應用程式驗證為應用程式其他意圖篩選器中定義的任何網址模式預設處理常式。
舉例來說,如果系統在 https://www.example.com/.well-known/assetlinks.json 找到 assetlinks.json 檔案,但未在 https://www.example.net/.well-known/assetlinks.json 找到,則具有下列意圖篩選器的應用程式只會通過 https://www.example.com 的驗證:
<application>
<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="http" />
<data android:scheme="https" />
<data android:host="www.example.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="www.example.net" />
</intent-filter>
</activity>
</application>
支援多個子網域的應用程式連結
Digital Asset Links 通訊協定會將意圖篩選器中的子網域視為獨立主機。因此,如果意圖篩選器列出多個具有不同子網域的主機,您必須在每個網域發布有效的 assetlinks.json。舉例來說,下列意圖篩選器包含 www.example.com 和 mobile.example.com,做為可接受的意圖網址主機。因此,有效的 assetlinks.json 必須同時發布至 https://www.example.com/.well-known/assetlinks.json 和 https://mobile.example.com/.well-known/assetlinks.json。
<application>
<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>
</activity>
</application>
或者,如果您使用萬用字元 (例如 *.example.com) 宣告主機名稱,則必須在根主機名稱 (example.com) 發布 assetlinks.json 檔案。舉例來說,只要在 https://example.com/.well-known/assetlinks.json 發布 assetlinks.json 檔案,具有下列意圖篩選器的應用程式就會通過 example.com 任何子名稱 (例如 foo.example.com) 的驗證:
<application>
<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:host="*.example.com" />
</intent-filter>
</activity>
</application>
檢查是否有多個應用程式與同一個網域相關聯
如果您發布多個與相同網域相關聯的應用程式,這些應用程式都能順利通過驗證。不過,如果應用程式可以解析完全相同的網域主機和路徑 (例如應用程式的精簡版和完整版),則只有最近安裝的應用程式可以解析該網域的網頁意圖。
在這種情況下,請檢查使用者裝置上是否有可能發生衝突的應用程式 (前提是您具備必要的套件瀏覽權限)。然後在應用程式中,顯示包含呼叫 queryIntentActivities 結果的自訂選擇器對話方塊。使用者可以從對話方塊中顯示的相符應用程式清單中,選取偏好的應用程式。
Android 14 以下版本的動態應用程式連結回溯相容性
動態應用程式連結功能 (包括 assetlinks.json 中的進階比對規則和 <uri-relative-filter-group> 的使用方式) 僅在 Android 15 (API 級別 35) 以上版本完全支援。
在 Android 14 (API 級別 34) 以下版本中,系統只會考量資訊清單 <data> 元素中宣告的 scheme 和 host,進行應用程式連結驗證。系統不會套用 assetlinks.json 中的路徑專屬規則、排除條件和動態更新。
也就是說,如果資訊清單只指定 scheme 和 host,應用程式可能會在 Android 14 以下版本中,意外擷取已驗證網域的所有路徑,無論您在 Android 15 以上版本的 assetlinks.json 中定義了哪些路徑專屬規則。
為較舊的 Android 版本設定備用策略,不使用深層連結
如要在 Android 15 以上版本使用動態應用程式連結,處理網域中更明確的路徑,請在資訊清單的意圖篩選器中加入不相符的路徑,避免應用程式在 Android 14 以下版本處理網域的所有連結。新增 <data> 元素,並使用 android:path 屬性,該屬性不太可能成為連結的有效路徑。這可確保意圖篩選器不會在較舊版本上比對所有路徑。
例子:
<activity
android:name=".MainActivity"
android:exported="true"
...>
<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" />
<data android:scheme="https" />
<data android:host="www.example.com" />
<!-- Add a non-matching path for backward compatibility -->
<data android:path="/no_match_for_older_android_versions" />
<uri-relative-filter-group android:allow="true">
<data android:pathPattern="/.*"/>
</uri-relative-filter-group>
</intent-filter>
</activity>
新增 <data android:path="/no_match_for_older_android_versions" /> 後,您可確保在 Android 14 以下版本中,這個意圖篩選器不會與任何傳入連結相符,同時仍允許系統根據 assetlinks.json 規則中的進階比對規則,驗證網域是否可用於 Android 15 以上版本的動態應用程式連結。
遷移現有應用程式連結
如果資訊清單中已有包含特定路徑規則 (例如 android:pathPrefix) 的應用程式連結,且您想在 Android 15 以上版本開始使用動態應用程式連結,可以直接在現有的意圖篩選器中新增 <uri-relative-filter-group> 元素。
由於 Android 14 以下版本會忽略 <uri-relative-filter-group> 元素,因此在搭載較舊版 Android 的裝置上,現有應用程式連結仍可照常運作。
不過,您必須審慎考量 Android 15 以上版本如何評估「混合」設定:
- 兩層篩選:在 Android 15 以上版本,系統會將意圖篩選器評估為聯集。如果網址符合舊版靜態
<data>代碼或<uri-relative-filter-group>中的廣泛規則,即通過資訊清單檢查。網址通過這項初始資訊清單檢查後,系統會套用assetlinks.json檔案中定義的動態規則,做為第二層精細篩選。也就是說,最終決定哪些相符網址會開啟應用程式的,是伺服器端的 JSON 規則。
混合設定範例:
<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:host="www.example.com" />
<!-- Legacy rule: Android 14 and lower use this. Android 15 and higher
also use this. -->
<data android:pathPrefix="/store" />
<!--
Dynamic rule: Android 14 and lower ignore this. Android 15 and higher
evaluate this as a union between all paths and the configuration
specified in the assetlinks.json file. Make sure to apply further
refinements in the assetlinks.json file to prevent all URL paths from
opening in the app.
-->
<uri-relative-filter-group android:allow="true">
<data android:pathPrefix="/" />
</uri-relative-filter-group>
</intent-filter>