Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle productflavors sourcesets and old project structure

Tags:

android

gradle

Today I started converting our project to use the gradle build system. I can't change the project structure, and I have different flavors, so I was wondering how I could override the sourceset for "dev" and "prod", because this apparently doesn't work:

buildscript {
    repositories {
        mavenCentral()
    }

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

apply plugin: 'android'

android {
    compileSdkVersion 17
    buildToolsVersion "17"

    dependencies {
        compile project(':ABS')
        compile project(':google-play-services_lib')
        compile project(':MergeAdapter')
        compile fileTree(dir: 'libs', include: '*.jar')
    }

    signingConfigs {
        release {
            storeFile file("myapp.keystore")
                    storePassword "**********"
                    keyAlias "**********"
                    keyPassword "**********"
        }
    }

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

        dev {
            java.srcDirs = ['src-dev']
        }

        prod {
            java.srcDirs = ['src-prod']
        }

        instrumentTest.setRoot('tests')
    }

    productFlavors {
        dev {
            packageName "com.myapp.dev"
        }

        prod {
            packageName "com.myapp"
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
        }
    }
}

This gives me the following error:

Cannot add a AndroidSourceSet with name 'dev' as a AndroidSourceSet with that name already exists.

The thing is, I can't use the new project structure and am stuck with this one, so how do I override the source directory here so it include, for the dev version, src-dev and for the prod version src-prod (instead of src/main/java and src/dev/java or src/prod/java)

Thx!

EDIT: The src-prod and src-dev directories only contain classes with constants that are used to point to different environments. I will do the same with assets (different icon for dev env)

like image 865
eMich Avatar asked Dec 08 '22 14:12

eMich


2 Answers

You need to first declare your flavors and then customize the sourceSets.

What happens is that when you customize the sourceSets it created android.sourceSets.dev, but then when creating android.productFlavors.dev it attempts to create a matching android.sourceSets.dev and fails (because it's already there).

We can't really know that the already created sourceSet is meant to be the same thing, so we fail rather than potentially using a sourceSet for that flavor when you meant it to be used for something else.

like image 80
Xavier Ducrohet Avatar answered Mar 29 '23 08:03

Xavier Ducrohet


Thank you, this works for me.

In my old style project, I needed two res variants for Amazon and Samsung builds. There are now three res folders containing a flavors.xml file which has some boolean flags.

res/values/flavors.xml
res-samsung/values/flavors.xml
res-amazon/values/flavors.xml

The gradle file first defines the flavors:

productFlavors {
    amazon {
    }

    samsung {
    }
}

Then, in the sourceSet closure, I don't add but just change the directories for the implicitely generated flavor-sourceSets:

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

samsung.res.srcDirs = ['res-samsung']
amazon.res.srcDirs = ['res-amazon']

instrumentTest.setRoot('tests')
}

Gradle's mergeXXXResources tasks then favour the res-xxx folder contents and as the result I have the desired boolean flags in the appropriate flavor APKs.

like image 45
Uwe Post Avatar answered Mar 29 '23 08:03

Uwe Post