Question

I have migrated my project from IDEA to Android Studio to use Gradle. I have successfully build the App (haven't looked at the tests yet) and now want to include product flavours (my reason for moving).

I can now select one of four build variants in the "Build Variants" menu, and building with the new settings renders a new App on my Phone, so that seems all ok.

However, where do I make the changes to the sources (with the original directory structure) or do I need to change it? What is the best way? (I don't have the structure /src/xxx/res, just src/org/bla/bla)

The main changes are to resources like string files and graphics. Thanks!

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.7.+'
    }
}
apply plugin: 'android'

repositories {
    mavenCentral()
}

dependencies {
    compile 'com.google.android.gms:play-services:4.1.+'
    compile 'org.apache.commons:commons-lang3:3.2.+'
    compile 'org.apache.httpcomponents:httpmime:4.1.+'
    compile 'com.android.support:appcompat-v7:+'
}

android {

    compileSdkVersion 19
    buildToolsVersion "19.0.1"

    defaultConfig {
        minSdkVersion 8
        targetSdkVersion 19
        versionCode 1
        //versionName "0.5"
    }

    productFlavors {
        iDomsPortalDev {
            packageName 'org.idoms.iDomsAndroid.iDomsPortalDev'
        }
        iDomsPortal {
            packageName 'org.idoms.iDomsAndroid.iDomsPortal'
        }
    }

    buildTypes {
        debug {
            packageNameSuffix ".debug"
            //versionNameSuffix "-debug"
        }
        release {
            packageNameSuffix ".release"
            //versionNameSuffix "-release"
        }
    }

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

        // Move the tests to tests/java, tests/res, etc...
        instrumentTest.setRoot('tests')

        // Move the build types to build-types/<type>
        // For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
        // This moves them out of them default location under src/<type>/... which would
        // conflict with src/ being used by the main source set.
        // Adding new build types or product flavors should be accompanied
        // by a similar customization.

        debug.setRoot('build-types/debug')
        release.setRoot('build-types/release')
        iDomsPortal.setRoot('product-flavors/iDomsPortal')
        iDomsPortalDev.setRoot('product-flavors/iDomsPortalDev')

}
println "main: " + android.sourceSets.main.res.srcDirs

    android.applicationVariants.all { variant ->
        println variant.name + ": " + android.sourceSets[variant.name].res.srcDirs
    }

    android.productFlavors.all { flavor ->
        println flavor.name + ": " + android.sourceSets[flavor.name].res.srcDirs
    }

    android.buildTypes.all { buildType ->
        println buildType.name + ": " + android.sourceSets[buildType.name].res.srcDirs
    }

[EDIT: updated data above and put the code to printed the output AFTER the changes and now it works!]

Was it helpful?

Solution

I assume you're asking where you put flavor-specific reources; if you're asking where to make changes to the main sources and resources, that's in the directory structure you mapped out under sourceSets.main.

You can add this bit of script to the top level of your build.gradle file (e.g. after the android block) to print out the directories where it will look for resources:

println "main: " + android.sourceSets.main.res.srcDirs

android.applicationVariants.all { variant ->
    println variant.name + ": " + android.sourceSets[variant.name].res.srcDirs
}

android.productFlavors.all { flavor ->
    println flavor.name + ": " + android.sourceSets[flavor.name].res.srcDirs
}

android.buildTypes.all { buildType ->
    println buildType.name + ": " + android.sourceSets[buildType.name].res.srcDirs
}

This outputs the flavor-specific resource directories, and also the build variant (flavor + build type directories).

When I run them on your build file, I get:

main: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/res]
app1: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1/res]
app2: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2/res]
debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/debug/res]
release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/release/res]
app1Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Debug/res]
app1Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Release/res]
app2Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Debug/res]
app2Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Release/res]

The directory for main is expected -- that's what's explicitly set in sourceSets.main. The debug and release types we see remapped to the build-types directory as per the statements at the end of your sourceSets block. For the app1 and app2 flavors, it's looking in src/app1 and src/app2, which is probably not what you want. I'd recommend moving the entire root directory for your flavors, similar to how you do for the build types. I'd change that block at the end to:

// Move the build types to build-types/<type>
// For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
// This moves them out of them default location under src/<type>/... which would
// conflict with src/ being used by the main source set.
// Adding new build types or product flavors should be accompanied
// by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
app1.setRoot('product-flavors/app1')
app2.setRoot('product-flavors/app2')

As for the variants, app1Debug et al, you maybe don't have to worry about them, unless you're doing something so specific that you really need to override on that detailed a basis. At any rate, now when I run and look at my diagnostic output, I see:

main: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/res]
app1: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/product-flavors/app1/res]
app2: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/product-flavors/app2/res]
debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/debug/res]
release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/build-types/release/res]
app1Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Debug/res]
app1Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app1Release/res]
app2Debug: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Debug/res]
app2Release: [/Users/sbarta/AndroidStudioProjects/AS_Eclipse/app/src/app2Release/res]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top