JustLearningAgain
JustLearningAgain

Reputation: 2287

How do I change the AndroidManifest.xml file based on build variants?

I have an app with multiple build variants. The variants are used to build versions of the same app for different companies. So, I have several different variants that build different apps:

The build.gradle file handles this very well:

     productFlavors {
    app1 {
        applicationId "com.acme.app1"
    }

    app2 {
        applicationId "com.schmoe.app2"
    }

    app3 {
        applicationId "com.yop.app3"
    }

}

Here's my problem. I am integrating Dropbox into my apps. The AndroidManifest.xml file must be changed for each variant to include the appropriate app key (stored in my string file). Dropbox has the following addition to the manifest file:

         <activity
        android:name="com.dropbox.client2.android.AuthActivity"
        android:launchMode="singleTask"
        android:configChanges="orientation|keyboard">

        <intent-filter>
            <!-- Change this to be db- followed by your app key -->
            <data android:scheme="db-abcdef" />
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.BROWSABLE"/>
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

Each build variant needs to change the following line:

 <data android:scheme="db-abcdef" />

to the appropriate value for each app variant. In other words, I need to replace part of the above string based on the build variant. For instance

App1

 <data android:scheme="db-111111" />

App2

 <data android:scheme="db-222222" />

App3

 <data android:scheme="db-333333" />

The line is the same for each variant up to the text "db-".

What I need is to dynamically replace the variable portion of the string (the x values) "db-xxxxxx" with a string from my string file.

I think this can be done with gradle scripts but I'm a complete newb with gradle. HELP!

If you can help, please be very specific about what goes where since I SUCK at gradle files and scripting. Thanks in advance!

Upvotes: 16

Views: 10410

Answers (4)

kip2
kip2

Reputation: 6863

Yet another way that is somewhat similar to Hibbem's answer:

android {
    defaultConfig {
        manifestPlaceholders = [schemeName: "default-scheme"]
    }
    ...
    app1 {
      applicationId "com.acme.app1"
      manifestPlaceholders = [schemeName: "db-111111"]
    }
    ...
}

And in your manifest:

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

More from the official docs

Upvotes: 1

Hibbem
Hibbem

Reputation: 1549

For people visiting this in 2019: You can use manifestPlaceholders per flavor. In your case:

productFlavors {
  app1 {
      applicationId "com.acme.app1"
      manifestPlaceholders.scheme = "db-111111"
  }

  app2 {
      applicationId "com.schmoe.app2"
      manifestPlaceholders.scheme = "db-222222"
  }

  app3 {
      applicationId "com.yop.app3"
      manifestPlaceholders.scheme = "db-333333"
  }
}

and then use it in your manifest:

<activity
    android:name="com.dropbox.client2.android.AuthActivity"
    android:launchMode="singleTask"
    android:configChanges="orientation|keyboard">

    <intent-filter>
        <!-- Change this to be db- followed by your app key -->
        <data android:scheme="${scheme}" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE"/>
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Upvotes: 4

Simas
Simas

Reputation: 44118

You can save the string in build.gradle too:

productFlavors {
    app1 {
        applicationId "com.acme.app1"
        resValue "string", "my_app_key", "db-abc"
    }

    app2 {
        applicationId "com.schmoe.app2"
        resValue "string", "my_app_key", "db-def"
    }

    app3 {
        applicationId "com.yop.app3"
        resValue "string", "my_app_key", "db-ghi"
    }
}

And then use it like:

<data android:scheme="@string/my_app_key" />

Upvotes: 21

323go
323go

Reputation: 14274

You can do this the same way you would modify the app name, using a string resource. In your manifest, include:

<data android:scheme="@string/db_scheme" />

Next, you add resources folders for the various build types, and in each, place a custom copy of a resource file, say res/values/flavor.xml:

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="app_name">Customer 1 App</string>
    <string name="db_scheme">db-111111</string>
</resources>

This will allow you to have different API keys, schemes, app-names, icons, etc. for your various flavors.

Upvotes: 7

Related Questions