Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with gradle android plugin and buildtype sourcesets

Tags:

android

gradle

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']
}
like image 496
Chilibeta Avatar asked Aug 26 '26 21:08

Chilibeta


1 Answers

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!

like image 53
slott Avatar answered Aug 30 '26 04:08

slott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!