Android多产品风味配置动态链接后报资源链接失败错误如何解决?
问题根因
错误日志里的unexpected element <intent-filter> found in <manifest>已经明确指出错误:intent-filter 标签不允许直接作为 <manifest> 根节点的子元素,它必须嵌套在 Activity、Service、BroadcastReceiver 这类应用组件标签内部才合法。你当前的production、staging专属AndroidManifest.xml都把处理深链的intent-filter直接写在了manifest根节点下,才会触发资源链接失败。
解决方案
方案1:使用Manifest占位符动态替换(更推荐,无需维护多套重复代码)
- 删除production、staging专属目录下AndroidManifest.xml里直接写在根节点的整个
<intent-filter>代码块。 - 在
app.gradle的productFlavors配置中,为每个环境添加Manifest占位符:productFlavors { production { dimension "base" applicationId 'at.mksquad.swapit.production' manifestPlaceholders = [dynamicLinkHost: "swapitapp.page.link"] } staging { applicationIdSuffix ".staging" applicationId 'at.mksquad.swapit.staging' manifestPlaceholders = [dynamicLinkHost: "kuncic.page.link"] } } - 把主AndroidManifest.xml里
AuthActivity的深链intent-filter中data标签的host值替换为占位符:<data android:host="${dynamicLinkHost}" android:scheme="https" />
构建时会自动根据当前选择的flavor替换为对应host值,无需额外维护多套intent-filter结构。
方案2:修正flavor Manifest的标签嵌套结构
如果要单独在flavor的Manifest中配置,需要把intent-filter嵌套到对应Activity节点下,依托Manifest合并规则生效,以production环境为例:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="at.mksquad.swapit"> <application> <activity android:name=".ui.screenflow.auth.AuthActivity"> <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:host="swapitapp.page.link" android:scheme="https" /> </intent-filter> </activity> </application> </manifest>
staging环境对应修改host即可,同时需要删除主Manifest里AuthActivity下原来写死的深链intent-filter,避免重复配置。
内容的提问来源于stack exchange,提问作者Edgar
相关产品推荐
相关产品推荐

