You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用GoRouter的Flutter深度链接失效:Web无法跳转至Android App

问题:Android深度链接未触发App跳转,直接打开Web应用

我已为Flutter应用配置深度链接,形如https://edinburgh-guide-airbnb.web.app/Leith的链接可在Web应用中正常跳转至对应页面。现需实现:用户点击该链接时,已安装原生App则打开App,未安装则降级至Web应用。

参考文档后,我在AndroidManifest.xml中添加了如下配置:

</intent-filter>
    <!-- Deep linking -->
    <meta-data android:name="flutter_deeplinking_enabled" android:value="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="edinburgh-guide-airbnb.web.app" />
    </intent-filter>

我认为该配置应拦截所有指向edinburgh-guide-airbnb.web.app的URL,但实际未生效——点击链接时Android直接跳转至Web应用,完全未触发App的深度链接处理。怀疑是manifest.xml配置有误,或是对链接拦截逻辑理解错误。

以下是main.dart中使用GoRouter处理深度链接的代码:

final GoRouter _router = GoRouter(
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      builder: (BuildContext context, GoRouterState state) {
        return MyHomePage(title: "", analytics: analytics, observer: observer);
      },
      routes: <RouteBase>[
        GoRoute(
          path: 'leith',
          builder: (BuildContext context, GoRouterState state) {
            return MyHomePage(title: "Leith", analytics: analytics, observer: observer);
          },
        ),
      ],
    ),
  ],
);

请问我的manifest.xml格式是否有误?或是对链接拦截逻辑理解错误?


排查与解决方案

1. 修正Manifest配置的核心问题

  • 移除多余的闭合标签:你提供的代码开头有一个</intent-filter>,这属于无效的XML结构,会导致整个深度链接配置被系统忽略。正确的配置应将深度链接的intent-filter放在<activity>标签内部,与启动页的intent-filter同级:
    <activity android:name=".MainActivity" ...>
        <!-- 启动页intent-filter -->
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    
        <!-- 深度链接配置 -->
        <meta-data android:name="flutter_deeplinking_enabled" android:value="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" android:host="edinburgh-guide-airbnb.web.app" android:pathPrefix="/" />
            <data android:scheme="https" android:host="edinburgh-guide-airbnb.web.app" android:pathPrefix="/" />
        </intent-filter>
    </activity>
    
  • 添加路径前缀约束:在data标签中补充android:pathPrefix="/",确保域名下所有子路径都能被匹配(虽然理论上默认匹配,但部分系统版本需要明确声明)。
  • 处理autoVerify验证:android:autoVerify="true"要求你在Web服务器的.well-known/assetlinks.json中配置App与域名的关联,否则系统会跳过App跳转逻辑。如果暂时不需要验证,可先将该属性改为false测试跳转是否生效。

2. 验证深度链接有效性

  • 使用ADB命令直接测试App跳转:
    adb shell am start -W -a android.intent.action.VIEW -d "https://edinburgh-guide-airbnb.web.app/Leith" com.your.app.package
    
    若能正常打开App并跳转到对应页面,说明GoRouter配置无问题,问题出在系统或浏览器的链接拦截逻辑上。
  • 检查系统默认应用设置:在Android系统「设置→应用→默认应用→打开链接」中,确认你的App已被设置为edinburgh-guide-airbnb.web.app的默认处理应用,若未添加则手动关联。

3. GoRouter配置补充

当前GoRouter配置已能匹配/leith路径,若需确保App启动时正确处理初始深度链接,可在GoRouter初始化时添加initialLocation监听,但现有配置已满足基本跳转需求,重点仍在Manifest与系统验证环节。


内容的提问来源于stack exchange,提问作者Sam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 10:21:05