- Inject build variables into the manifest
Inject build variables into the manifest
If you need to insert variables into your AndroidManifest.xml
file that aredefined in your build.gradle
file, you can do so with themanifestPlaceholders
property. This property takes a map of key-value pairs, as shown here:
android {
defaultConfig {
manifestPlaceholders = [hostName:"www.example.com"]
}
...
}
You can then insert one of the placeholders into the manifest file as anattribute value like this:
<intent-filter ... >
<data android:scheme="http" android:host="${hostName}" ... />
...
</intent-filter>
By default, the build tools also provide your app'sapplication ID in the${applicationId}
placeholder. The value always matches the final applicationID for the current build (includingchanges by build variants.This is useful when you want to use a unique namespace for identifierssuch as an intent action, even between your build variants.
For example, if your build.gradle
file looks like this:
android {
defaultConfig {
applicationId "com.example.myapp"
}
productFlavors {
free {
applicationIdSuffix ".free"
}
pro {
applicationIdSuffix ".pro"
}
}
}
Then you can insert the application ID in your manifest like this:
<intent-filter ... >
<action android:name="${applicationId}.TRANSMOGRIFY" />
...
</intent-filter>
And the manifest result when you build the "free" product flavor is this:
<intent-filter ... >
<action android:name="com.example.myapp.free.TRANSMOGRIFY" />
...
</intent-filter>
For more information, readSet the application ID.