How to split source by buildType AND flavor

android, android-studio, gradle

Solution

There's no official way to set resource files of the 'flavorBuildType' (like flavor1Debug) combination, so you have to do a little hack here.

First define the new path of AndroidManifest.xml:

project.ext.flavor1 = [
    debugManifest: 'src/flavor1Debug/AndroidManifest.xml',
    releaseManifest: 'src/flavor1Release/AndroidManifest.xml'
]

project.ext.flavor2 = [
    debugManifest: 'src/flavor2Debug/AndroidManifest.xml',
    releaseManifest: 'src/flavor2Release/AndroidManifest.xml'
]

Second, tell gradle to use the new AndroidManifest.xml in processManifest task. Now the problem is: sourceSet.manifest.srcFile is read-only, we can't just replace it on-fly. Since we're using separate resource (debug & release) for each flavor, we can copy the new AndroidManifest.xml to the origin flavor folder and gradle will build the APK file with right settings.

android.applicationVariants.all { variant ->
    variant.processManifest.doFirst {
        if (project.ext.has(variant.productFlavors.name)) {
            if (project.ext[variant.productFlavors.name].debugManifest != null &&
                project.ext[variant.productFlavors.name].releaseManifest != null) {
                def manifestDirectory = android.sourceSets[variant.productFlavors.name].manifest.srcFile.parentFile
                if (variant.buildType.name.equals("debug")) {
                    copy {
                        from project.ext[variant.productFlavors.name].debugManifest
                        into manifestDirectory
                    }
                } else if (variant.buildType.name.equals("release")) {
                    copy {
                        from project.ext[variant.productFlavors.name].releaseManifest
                        into manifestDirectory
                    }
                }
            }
        }
    }

    variant.processManifest.doLast {
        if (project.ext.has(variant.productFlavors.name)) {
            project.delete android.sourceSets[variant.productFlavors.name].manifest.srcFile
        }
    }
}

And for the AndroidManifest.xml, just leave the GCM related setting there:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.android.example.free.debug"
    android:versionCode="1"
    android:versionName="1.0" >

    <permission
        android:name="com.android.example.free.debug.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="com.android.example.free.debug.permission.C2D_MESSAGE" />
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />

</manifest>

You can get sample code here : https://github.com/shakalaca/learning_gradle_android/tree/master/07_tricks

It supports both Google Maps API v2 & GCM cases, but I think it is easy to support content providers as well after minor code change.

Problem

I have an app with different package name for the buildTypes debug/release as well as for two productFlavors. The interesting part of my build.gradle looks like this: ``` android { buildTypes { debug { packageNameSuffix ".debug" } release { signingConfig signingConfigs.release } } productFlavors { flavor1 { packageName "com.example.app.flavor1" } flavor2 { packageName "com.example.app.flavor2" } } ``` So there are for package names: com.example.app.flavor1 com.example.app.flavor1.debug com.example.app.flavor2 com.example.app.flavor2.debug Everything is fine with the code. But because the modified package name get's messed up while merging the manifest I need to set some things like GCM permissions and content providers authority by hand for each valid package. But how do it do it? No matter if I put the `AndroidManifest.xml` into `src/falvor{1,2}` or in `src/{debug,release}`, I end up with only two configurations. I tried things like `flavor1Debug` without luck.

Original source

Related problems