20

I have a multi-module project. From the root of my project (which contains multiple modules), I want to be able to call 'gradle build' and have it use a different AndroidManifest in one of my modules depending on some parameter I pass in. What's the best way to accomplish this? Should I use a gradle.properties file or can I specify a different build.gradle somehow in my settings.gradle file? Any help appreciated!

settings.gradle:

include 'ActionBarSherlock'
include '<main_app>'

build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
    }
}

apply plugin: 'android'

dependencies {
    compile project(':ActionBarSherlock')
}

android {
    buildToolsVersion "17.0"
    compileSdkVersion 17

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            aidl.srcDirs = ['src']
            renderscript.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }
}

I'm looking for the best way to use a different AndroidManifest.xml, say one I have in //test/AndroidManifest.xml. And I need to be able to specify this change from the command-line. Any ideas?

Karim Varela
  • 7,562
  • 10
  • 53
  • 78

2 Answers2

18

I solved this by using different build types.

Here's my build.gradle:

buildscript {
    repositories {
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:0.4.2'
   }
}

apply plugin: 'android'

dependencies {
    compile project(':facebook-android-sdk-3.0.1:facebook')
    compile project(':google-play-services_lib')
    compile project(':nineoldandroids')
    compile project(':SlidingMenu-master:library')
    compile project(':ViewPagerIndicator')
    compile project(':volley')
    compile project(':windowed-seek-bar')
    compile files('compile-libs/androidannotations-2.7.1.jar', 'libs/Flurry_3.2.1.jar', 'libs/google-play-services.jar', 'libs/gson-2.2.4.jar', 'libs/picasso-1.1.1.jar', 'libs/crittercism_v3_0_11_sdkonly.jar', 'libs/gcm.jar', 'libs/apphance-library.jar')
}

android {
    buildToolsVersion "17.0"
    compileSdkVersion 17

    signingConfigs {
        debug {
            storeFile file('keystores/debug.keystore')
        }
    }

    buildTypes {
        debug {
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    resources.srcDirs = ['src']
                    aidl.srcDirs = ['src']
                    renderscript.srcDirs = ['src']
                    res.srcDirs = ['res']
                    assets.srcDirs = ['assets']
                }
            }
        }

        release {
            zipAlign true
            sourceSets {
                main {
                    manifest.srcFile 'AndroidManifest.xml'
                    java.srcDirs = ['src']
                    resources.srcDirs = ['src']
                    aidl.srcDirs = ['src']
                    renderscript.srcDirs = ['src']
                    res.srcDirs = ['res']
                    assets.srcDirs = ['assets']
                }
            }
        }

        utest {
            debuggable true
            signingConfig signingConfigs.debug

            sourceSets {
                main {
                    manifest.srcFile 'utest/AndroidManifest.xml'
                    java.srcDirs = ['src']
                    resources.srcDirs = ['src']
                    aidl.srcDirs = ['src']
                    renderscript.srcDirs = ['src']
                    res.srcDirs = ['res']
                    assets.srcDirs = ['assets']
                }
            }
        }
    }
}

You can see that for my utest build, I'm specifying a manifest in a different directory. works.

Karim Varela
  • 7,562
  • 10
  • 53
  • 78
  • 2
    I am seeing issues with different AndroidManifest.xml files being merged together that should not be merged. For example, I have a debug and a qa buildType that are being merged together when building for debug. – Ray Hunter Nov 16 '13 at 16:17
  • I think that is by design, so that you can have different permissions for different builds. – Lou Morda Aug 26 '15 at 22:40
  • 1
    This answer is more useful than whole plugin documentation ;) – Łukasz Zwierko Oct 08 '16 at 20:09
  • Is there a way to disable merging? I select a different build variant and it's building with a merged manifest unless I comment out the variant in the build.gradle – Computer's Guy Jan 25 '21 at 13:15
  • Does not work at all for me. Only the "manifest.srcFile" under the last build_variant in the gradle file is evaluated, all others are ignored. Even invalid paths are ignored. – Andreas K. aus M. Dec 26 '21 at 18:07
  • Same with productFlavors: All "manifest.srcFile" lines in the gradle file are ignored, except the last one. – Andreas K. aus M. Dec 26 '21 at 18:16
2

Use this:

manifest.srcFile "src/development/AndroidManifest.xml"