Any way to read a constant depending buildType ${deepLinkHost}
?
debug -> deepLinkUri = http://link.debug/
staging -> deepLinkUri = http://link
It looks like it's not currently supported out of the box, but there is a pretty simple workaround. Normally registering a deep link requires two steps:
deepLink
to the navigation graph. Since we can't specify any substitution or a @string
resource as the uri, let's just define the host name as a variable:
. This link will match any host and pass deepLinkHost
as a parameter.AndroidManifest.xml
, so our activity actually reacts on the deep link. The recommended approach for the latest android studio is to add
to the manifest, so it generates necessary intent-filter automatically. It will, however, register our activity to accept links with any host, which is probably not what we want. So instead of doing this, let's go with the standard approach. Basically here we define the intent-filter manually instead of auto-generating it from navigation graph. So we can use manifest substitutions just as we would normally do.Example
and
build.gradle
buildTypes {
debug {
manifestPlaceholders.deepLinkUri = "http://link.debug"
}
staging {
manifestPlaceholders.deepLinkUri = "http://link.staging"
}
release {
manifestPlaceholders.deepLinkUri = "http://link"
}
}
AndroidManifest.xml
navigation_main.xml
22.05.2020 Update:
Starting with the gradle plugin version 3.6 the deep link uri cannot contain parameters in scheme and host parts, it will result in Improper use of wildcards and/or placeholders in deeplink URI host
error.
Fortunately one can use a wildcard, so to make the approach work again just change the deepLink
definition in your nav graph file:
to