37

I have a suite of projects that use the same module, which contains nearly all the actual code. The project is setup like:

project/
  - app/
    - build.gradle
  - libraries/
    - module/
      - build.gradle
  - build.gradle
  - settings.gradle

The dependencies are all setup correctly, and I can build and run apps great, however I can only add flavors to the project, which is not the ideal solution. settings.gradle contains the following:

include ':app', ':libraries:module'

In the app directory's build.gradle file, I added the following block:

productFlavors {
    alpha
    production
}

Using gradle 0.11, this syncs and creates assembleAlphaDebug, assembleAlphaRelease, assembleProductionDebug, assembleProductionRelease tasks. When I attempt to do this in the module instead, I get the error:

No resource found that matches the given name (at 'theme' with value '@style/MyCustomTheme')

in the built app/src/main/AndroidManifest.xml. For some reason, the module is not being built, so the custom theme is not working. What am I doing wrong?

Phil
  • 35,852
  • 23
  • 123
  • 164
  • As per my understanding even app is a module same as you "module" and putting flavors in any of module will work. What error are you getting while compiling the code. – Piyush Agarwal Jun 19 '14 at 13:46
  • @pyus13 Looking further, the error seems to be an issue with not finding a resource for the given flavor. I have a custom theme set in the app's manifest, but I get `No resource found that matches the given name (at 'theme' with value '@style/MyCustomTheme')`. – Phil Jun 19 '14 at 13:49
  • is your module is a library project. Means it has "android-library" or "android" as plugin ? – Piyush Agarwal Jun 19 '14 at 14:04
  • @pyus13 it uses the *android-library* plugin. – Phil Jun 19 '14 at 14:05
  • As commented by Android Studio team lead here libraries doesn't support flavors . http://stackoverflow.com/questions/17107908/android-library-with-specific-product-flavors-dependency – Piyush Agarwal Jun 19 '14 at 14:13
  • @pyus13 I saw that, but I think it's outdated. This indicates that it should be supported: http://tools.android.com/tech-docs/new-build-system/migrating_to_09 – Phil Jun 19 '14 at 14:15
  • Check this if this could help you https://groups.google.com/forum/#!topic/adt-dev/iUdV2xVCc38 – Piyush Agarwal Jun 19 '14 at 14:27
  • What errors are you seeing? – Scott Barta Jun 19 '14 at 17:48
  • @ScottBarta I get the error `No resource found that matches the given name (at 'theme' with value '@style/MyCustomTheme')` when I build the *app*. – Phil Jun 19 '14 at 18:29
  • This is sort of a different problem. I think it might help if you recast your question and include more information on how you're setting this theme up and what the dependencies are between main app and library. Your problem doesn't really seem to be with setting a library flavor up, unless that theme resource lives in only one flavor in the library, which isn't clear yet. – Scott Barta Jun 19 '14 at 18:41
  • **For more detailed Q/A see:** [Multi Flavor App based on multi Flavor Library?](https://stackoverflow.com/questions/24860659/multi-flavor-app-based-on-multi-flavor-library-in-android-gradle) – Top-Master Apr 30 '22 at 03:40

2 Answers2

82

In the library module's build.gradle, you need a couple extra lines to tell it to export the flavors and which build variant to use by default if not specified when being included from another module:

android {
    defaultPublishConfig "productionRelease"
    publishNonDefault true

    productFlavors {
        alpha {
        }
        production {
        }
    }
}

That publishNonDefault bit is only necessary if someone would want to depend on something other than the productionRelease variant. Presumably this is the case if you set up multi-flavors in your library in the first place.

Now if you add a dependency from another module via this in its build.gradle:

dependencies {
    compile project(':module')
}

it will depend on the productionRelease variant by default. If you'd like to depend on a non-default variant:

dependencies {
    compile project(path: ':module', configuration:'alphaDebug')
}
Scott Barta
  • 79,344
  • 24
  • 180
  • 163
3
  1. First add below gradle code snippet to your app/build.gradle
flavorDimensions "env"
productFlavors {
    dev {
        dimension "env"
    }
    pre {
        dimension "env"
    }
    prod {
        dimension "env"
    }
}
  1. Second, add below gradle code snippet to your module/build.gradle
flavorDimensions "env"
productFlavors {
    register("dev")
    register("pre")
    register("prod")
}

I have posted an ansower in this Use different library module for each android flavor

caopeng
  • 914
  • 13
  • 23
  • For details see: stackoverflow.com/[**Multi Flavor App with multi Flavor Library?**](https://stackoverflow.com/q/24860659/8740349) – Top-Master Apr 30 '22 at 03:49
  • 1
    Can you explain exactly what 'register' does here? Is this just the same as 'dev { }' in a more concise way, without having to add {'dimension "env" } to each one? – mawalker Jun 30 '22 at 13:09