如何在 AndroidManifest.xml 中为 Android Deeplinking 注入主机和架构

问题描述

我正在尝试使用 build.gradle as referenecd here 将 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","www.host_staging.com"
        resValue "string","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","www.host_demo.com"
            }

            staging {
                dimension 'yourAppName'
                resValue "string","www.host_staging.com"
            }
        }
    }
    ...
}

重建后,host_nameapp_scheme 会为您生成,如果您更喜欢在代码中定义的 productFlavours。这对我处理的每个项目都适用:)

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:我更喜欢用蛇形大小写定义字符串资源。 :)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...