android - 如何在 AndroidManifest.xml 中为 Android Deeplinking 注入主机和架构
问题描述
我正在尝试使用 build.gradle 将 url 架构和路径动态注入到我的 AndroidManifest.xml 文件中作为参考这里
但是,这不允许触发深层链接。
当我在 AndroidManifest.xml 中使用静态值时,我测试了我的深度链接工作。
// AndroidManifest.xml
<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="${appScheme}"
android:host="${hostName}"
android:path="/path2" />
<data
android:scheme="${appScheme}"
android:host="${hostName}"
android:path="/path1" />
</intent-filter>
// build.gradle (:app)
defaulConfig {
manifestPlaceholders = [
hostName: "www.host_name.com",
appScheme: "https"
]
}
demo {
resValue "string", "hostName", "www.host_demo.com"
resValue "string", "appScheme", "https"
}
staging {
resValue "string", "hostName", "www.host_staging.com"
resValue "string", "appScheme", "http"
}
解决方案
我认为您可以在这里做的是在您的 defaultConfig 中定义这些字符串,如下所示:
android {
...
defaultConfig {
...
flavorDimensions 'yourAppName'
resValue "string", "host_name", "www.live_demo.com"
resValue "string", "app_scheme", "http"
productFlavors {
demo {
dimension 'yourAppName'
resValue "string", "host_name", "www.host_demo.com"
}
staging {
dimension 'yourAppName'
resValue "string", "host_name", "www.host_staging.com"
}
}
}
...
}
重建后,如果您希望使用您在代码中定义的 productFlavours,将为您生成host_name
。app_scheme
这对我处理的每个项目都适用:)
app/build/generated/res/resValues/debug/values/gradleResValues.xml
app/build/generated/res/resValues/demo/values/gradleResValues.xml
app/build/generated/res/resValues/staging/values/gradleResValues.xml
然后从您的AndroidManifest.xml
文件中,您可以调用@string/host_name
而不是${hostName}
.
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="@string/app_scheme"
android:host="@string/host_name"
android:path="/path2" />
<data
android:scheme="@string/app_scheme"
android:host="@string/host_name"
android:path="/path1" />
</intent-filter>
PS:我更喜欢用蛇案例定义字符串资源。:)
推荐阅读
- java - org.json.JSONException:值
org.json.JSONArray 类型的无法转换为 JSONObject - sql - 主列和 auto_increment 列的顺序无法正常工作
- javascript - React无法读取if块中未定义的属性'setState'
- linux - AWS CDK - Linux EC2 实例 Cloudformation Init - /opt/aws/bin/cfn-signal 重启后无法正常运行
- javascript - React Native:嵌套(3级)反应导航器不起作用
- c# - 在内容页面的 OnAppearing() 中使用 ScrollView 中的 ScrollToAsync() 方法时出现问题
- spring - 我需要帮助使用 Spring、Junit 5、Mockito、REST-Assured 和 Lombok 设置控制器测试
- gdb - GDB 没有采用放置在 ~/.gdbint 和/或 ~/.gdbrc 文件中的指令
- kubernetes - 如何将启用 SSL 的 Cassandra 集群配置为 cassandra-reaper 的后端存储?
- javascript - 在Angular 8中具有多个选择的复选框内包含内容的自定义复选框