Navigation components : Deeplink using uri depending buildType

前端 未结 1 588
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 23:09

Any way to read a constant depending buildType ${deepLinkHost}?

debug -> deepLinkUri = http://link.debug/
staging -> deepLinkUri = http://link         


        
1条回答
  •  夕颜
    夕颜 (楼主)
    2021-02-01 23:47

    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:

    1. Add 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.
    2. Register an intent-filter in the 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

    • debug -> http://link.debug/identification?code=123
    • staging -> http://link.staging/identification?code=123
    • release -> http://link/identification?code=123

    and

    • debug -> http://link.debug/banner?id=123
    • staging -> http://link.staging/banner?id=123
    • release -> http://link/banner?id=123

    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

    0 讨论(0)
提交回复
热议问题