JY2k
JY2k

Reputation: 2909

Can Manifest value depend on build.gradle file

I am working on a multi flavoured product. I want to be able to disable and enable components in the manifest per flavour. I've been able to create such a dependency from bool.xml file to the AndroidManifest.xml in the following way:

<activity
    android:name=".ui.activities.SpecialActivity"
    android:enabled="@bool/is_activity_enabled_in_manifest"/>

Can I create the same dependency from the build.gradle to the AndroidManifest.xml?

Upvotes: 1

Views: 946

Answers (1)

azizbekian
azizbekian

Reputation: 62189

Take a look at Manifest Placeholders.

Basically, you specify variable in gradle file:

android {
    defaultConfig {
        manifestPlaceholders = [hostName:"www.example.com"]
    }
    ...
}

And then refer to them from AndroidManifest.xml

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

In your case this may be applied:

android {
    defaultConfig {
        manifestPlaceholders = [ isActivityEnabled:"true" ]
    }

    buildTypes{
        debug{
            // Some debug setup
        }
        release{
            // Some release setup
        }
    }

    productFlavors {
        // List of flavor options
    }
    productFlavors.all{ flavor->
        if (flavor.name.equals("someFlavor")) {
            if (buildType.equals("release")) {
                manifestPlaceholders = [ isActivityEnabled:"false" ]
            } else {
                manifestPlaceholders = [ isActivityEnabled:"false" ]
            }
        } else {
            if (buildType.equals("release")) {
                manifestPlaceholders = [ isActivityEnabled:"true" ]
            } else {
                manifestPlaceholders = [ isActivityEnabled:"true" ]
            }    
        }
    }
}

The snippet above is taken from this question.

Upvotes: 2

Related Questions