Use variables in manifest

亡梦爱人 提交于 2020-06-23 08:51:41

问题


Integrating some libraries like Branch-io in Android need to define meta-data in project manifest. some of this variable is like TestMode

    <meta-data android:name="io.branch.sdk.TestMode" android:value="true" />

So, when we want to publish the application we should change it to False.

Is there any way to define a variable somewhere according to BuildType and assign it to the Meta-data to that?


回答1:


Yes you can inject build variables from gradle to manifest, it is done by adding variable to the build.gradle:

android {
    defaultConfig {
        manifestPlaceholders = [hostName:"www.example.com"]
    }
    deployConfg {
        manifestPlaceholders = [hostName:"www.prod-server.com"]
    }
    ...
}

And then in your manifest you can get it by:

<intent-filter ... >
    <data android:scheme="http" android:host="${hostName}" ... />
    ...
</intent-filter>

You can read more about how this works here.




回答2:


You can do it by adding manifestPlaceholders to your build.gradle file:

android {
    ...
    buildTypes {
        debug {
            manifestPlaceholders = [isBranchSdkInTestMode:"true"]
            ...
        }
        release {
            manifestPlaceholders = [isBranchSdkInTestMode:"false"]
            ...
        }
    }
    ...
}

In AndroidManifest.xml, you can use it as ${isBranchSdkInTestMode}:

<meta-data android:name="io.branch.sdk.TestMode" android:value="${isBranchSdkInTestMode}" />


来源:https://stackoverflow.com/questions/58065984/use-variables-in-manifest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!