如何在AndroidManifest.xml中为Android Deeplinking注入主机和架构 [英] How to Inject host and schema in AndroidManifest.xml for Android Deeplinking

查看:133
本文介绍了如何在AndroidManifest.xml中为Android Deeplinking注入主机和架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用build.gradle作为此处引用

I am trying to dynamically inject the url schema and path into my AndroidManifest.xml file using build.gradle as referenecd here

但是,这不允许触发深层链接.

However, this does not allow deeplinks to trigger.

当我在AndroidManifest.xml中使用静态值时,我测试了深层链接的工作.

I tested my deeplinking works when I use static values in the 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中定义这些字符串,如下所示:

I think what you can do here is define those strings in your defaultConfig, like this:

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"
            }
        }
    }
    ...
}

重建后的

,如果您希望拥有类似代码定义的productFavours,则会为您生成 host_name app_scheme .这对我处理的每个项目都有效:)

after rebuilding, host_name and app_scheme will be generated for you if you prefer to have productFlavours like what you have defined on you code. This works for me on every project i handled :)

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} .

Then from your AndroidManifest.xml file, you can call @string/host_name instead of ${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:我更喜欢用蛇形来定义字符串资源.:)

PS: I prefer defining string resources with snake case. :)

这篇关于如何在AndroidManifest.xml中为Android Deeplinking注入主机和架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆