How to Inject host and schema in AndroidManifest.xml for Android Deeplinking
Asked Answered
T

1

2

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.

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"
    }
Theatrics answered 6/1, 2021 at 0:37 Comment(1)
What do you mean by However, this does not allow deeplinks to trigger.? is it not working with resValue defined on each flavor?Kreisler
K
6

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

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

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: I prefer defining string resources with snake case. :)

Kreisler answered 8/1, 2021 at 6:9 Comment(2)
Moving the resValue to defaultConfig worked. Thanks! PS: I defined the strings as camelCase because that is how the docs do it.Theatrics
Ah i see, Nice! :)Kreisler

© 2022 - 2024 — McMap. All rights reserved.