2

I am new to gradle and want to use the gradle android plugin with flavors, buildtypes and custom sourcesets. Building with flavors and buildtypes works find. Unfortunately I have a legacy project directory structure, so I have to customize the res directory for the buildtypes.

Somehow the additional resources (basically some string config values) for a buildtype do not seem to get merged into the build and do not replace resource keys from the main tree.

This was asked before, I know there is Gradle productflavors sourcesets and old project structure and How can I specify per flavor buildType sourceSets?

I want to quote Xavier from the first post: "You need to first declare your flavors and then customize the sourceSets."

I understand that a flavor configuration automatically creates sourcesets and that they need to be modified afterwards. Thats what I tried in the following build.gradle, but my added "res.srcDirs" for each buildtype seem to get ignored and I cannot figure out why.

In the second post (last comment with the link to github), I see complicated sourceset modification in the build.gradle. Is there also a simpler way for this? (see here: https://github.com/shakalaca/learning_gradle_android/blob/master/07_tricks/app/build.gradle)

This is my build configuration for the gradle android plugin:

android {
    compileSdkVersion 18
    buildToolsVersion "18.0.1"

    defaultConfig {
        minSdkVersion 9
        targetSdkVersion 17
    }

    sourceSets {

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

    productFlavors {
        normal {
        }

        proguard {
            proguardFile file("proguard-project.txt")
        }
    }


    buildTypes {

        dev {
            packageNameSuffix ".dev"
            debuggable true
            signingConfig android.signingConfigs.debug

        }

        beta {
            packageNameSuffix ".beta"
            debuggable true
            signingConfig android.signingConfigs.debug
        }

        live {
            packageNameSuffix ".live"
            debuggable true
            signingConfig android.signingConfigs.debug
        }
    }

   sourceSets.dev.res.srcDirs = ['variations/dev']
   sourceSets.beta.res.srcDirs = ['variations/staging']
   sourceSets.live.res.srcDirs = ['variations/live']
}
Community
  • 1
  • 1
Chilibeta
  • 27
  • 1
  • 4

2 Answers2

6

If you change the order of your script you can have a much better structure and make it easier to read.

Say you have a release, beta and debug type and you want a different source dir you can do it like this:

    buildTypes {
    release {
        debuggable false
        jniDebugBuild false
        runProguard true
        proguardFile 'proguard-android.txt'

        // Links to signing config above.
        signingConfig signingConfigs.release
    }

    debug {
        packageNameSuffix ".debug"
        versionNameSuffix ".debug"

    }
    beta {
        // Import release code - avoid having the same code twice!
        initWith release
        packageNameSuffix ".beta"
        versionNameSuffix ".beta"
    }
}

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 {
        res.srcDirs = ['res', 'res-release']
    }

    beta {
        res.srcDirs = ['res', 'res-beta']
    }

    debug {
        res.srcDirs = ['res', 'res-debug']
    }
}

Notice the order of buildTypes and sourceSets!

slott
  • 3,266
  • 1
  • 35
  • 30
4

Your build.gradle should be fine, check your variations directory, the dev/stagin/live folder structure should be the same with res directory.

app/AndroidManifest.xml
app/src/com/...
app/res/values/strings.xml
app/res/layout/main_activity.xml
app/variations/dev/values/strings.xml
app/variations/staging/values/strings.xml
app/variations/live/values/strings.xml

BTW you defined the proguardFile but there's no runProguard true

shakalaca
  • 1,692
  • 12
  • 5
  • Wrong res folder structure for the buildtypes was indeed the problem. Thanks a lot and for mentioning that with the proguard definition. – Chilibeta Sep 08 '13 at 09:04