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 themanifestPlaceholdersproperty. This property takes a map of key-value pairs, as shown here:

  1. android {
  2. defaultConfig {
  3. manifestPlaceholders = [hostName:"www.example.com"]
  4. }
  5. ...
  6. }

You can then insert one of the placeholders into the manifest file as anattribute value like this:

  1. <intent-filter ... >
  2. <data android:scheme="http" android:host="${hostName}" ... />
  3. ...
  4. </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:

  1. android {
  2. defaultConfig {
  3. applicationId "com.example.myapp"
  4. }
  5. productFlavors {
  6. free {
  7. applicationIdSuffix ".free"
  8. }
  9. pro {
  10. applicationIdSuffix ".pro"
  11. }
  12. }
  13. }

Then you can insert the application ID in your manifest like this:

  1. <intent-filter ... >
  2. <action android:name="${applicationId}.TRANSMOGRIFY" />
  3. ...
  4. </intent-filter>

And the manifest result when you build the "free" product flavor is this:

  1. <intent-filter ... >
  2. <action android:name="com.example.myapp.free.TRANSMOGRIFY" />
  3. ...
  4. </intent-filter>

For more information, readSet the application ID.