11

Integrating some libraries like Branch-io in Android need to define meta-data in the project manifest. some of these variables are 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?

FarshidABZ
  • 3,860
  • 4
  • 32
  • 63

2 Answers2

12

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}" />
Natig Babayev
  • 3,128
  • 15
  • 23
9

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.

Daniel
  • 2,320
  • 1
  • 14
  • 27