How to use multiple res.srcDirs and override some resources with gradle

Parmaia picture Parmaia · Jul 20, 2015 · Viewed 14.3k times · Source

I want to build different versions of my app based on different productFlavors, but need some degree of flexibility that I can't achieve yet.

This is my folder structure:

+src
  +main
    +java
    +res
  +base
    +java
    +res
  +custom1
    +java
    +res
  +custom2
    +res

The common code is on main (a service) and the base ui is on base (the activity). Then a custom version of the app that defines a new ui is on custom1 (new activity). This is running fine. But I need another version of the app (custom2) that uses the base ui, but changing some of the res (icons, strings or colors).

What I'm trying in my build.gradle file is:

android{
    ...
  productFlavors {
    base {
      ...
    }
    custom1 {
      ...
    }
    custom2 {
      ...
    }
  }

  sourceSets{
    custom2{
      java.srcDirs = ['src/base/java']
      res.srcDirs = ['src/custom2/res', 'src/base/res']
    }
  }
}

To specify that custom2 will use the source code and resources of base and the resources of custom2.

The problem is that I get a:

Error: Duplicate resources: <project_path>/src/base/res/values-es/strings.xml:string-en/app_name, <project_path>/src/custom2/res/values-es/strings.xml:string-en/app_name

Because app_name is defined both on base and on custom2, but my goal is to override the definition of resources in base with the ones of custom2.

I know that app_name will be overriden in main if I don't specify anything in res.srcDirs for custom2, but then all the resources from base will be unavailable.

Is the approach correct? or I'm abusing of the flexibility that Gradle offers? or there is a way to do what I'm trying to do?

Thanks in advance!

Answer

Gabriele Mariotti picture Gabriele Mariotti · Jul 20, 2015

You can't achieve it in this way.

There is a priority in resource merging:

The merged resources are coming from 3 types of sources:

  • The main resources, associated with the main sourceSet, generally located in src/main/res
  • The variant overlays, coming from the Build Type, and Flavor(s).
  • The Library Project dependencies, which contribute resources through the res entry in their aar bundle.

As described here

As mentioned above, each sourceSet can define multiple resource folders. For instance:

android.sourceSets {
   main.res.srcDirs = ['src/main/res', 'src/main/res2']
}

In this case, both resource folders have the same priority. This means that if a resource is declared in both folders, the merge will fail and an error will be reported.

The same happens in your case. You can't duplicate the same resources in this way.