4

I'm developing an android app with multiple flavors like so:

sourceSets {
    main {
        res.srcDirs += ["headend/ott/res"]
    }

    flavor1 {
        res.srcDirs += ["src/module1/res-splash"]
    }

    flavor2 {
        java.srcDirs += ["src/module1/java"]
        res.srcDirs += ["src/module1/res"]
        res.srcDirs += ["src/module2/res"]
        assets.srcDirs += ["src/module1/assets"]
    }

    test {
        res.srcDirs += ["src/test/resources"]
    }
    ...

My problem is that, in flavor2, some of the module2 resources are supposed to be replacements of ones already present in module1, but with my current approach it causes the build to fail with duplicate resources.

So what I need is a way to add "src/module1/res" to flavor2 but without including one specific file.

I've tried

res{
    srcDirs += ["src/module1/res"]
    res.srcDirs += ["src/module1/res"]        
    exclude 'src/module1/res/drawable/specific_file.xml'
}

But to no avail.

Is this possible at all?

rds
  • 26,253
  • 19
  • 107
  • 134
ricardo silva
  • 331
  • 1
  • 18

2 Answers2

1

After looking at multiple answers like this, the code that you have looks correct to me. However, this bug stating that exclude paths are not implemented is still open.

This alternate approach which references these docs may work for you instead. I suggest adding a resource directory inside your flavour2 directory/module and using it to include a discard file.

sourceSets {
    flavor2 {
        res {
            srcDirs += ["src/module1/res"]
            srcDirs += ["src/module2/res"]
        }
    }
 }

Then add resources_discard.xml to module2/res/raw with the following:

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools" 
         tools:discard="@drawable/specific_file.xml" />
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • doesn't that discard the `module2/specific_file`? because what I need is to discard the `module1/specific_file` while adding a new `module2/specific_file` – ricardo silva Mar 19 '18 at 10:38
  • Ah ok, so you are also including some files from module2 which are overrides? Can you update your question code to show what you have there then so I can see paths – Nick Cardoso Mar 19 '18 at 10:47
  • I had that information already: "in flavor2, I need to replace some files in the res folder, which causes the build to fail with duplicate resources." Do you think it could be made clearer? – ricardo silva Mar 19 '18 at 10:52
  • All of your code above includes only res from module1 ... no second source – Nick Cardoso Mar 19 '18 at 10:53
0

Try doing this. In one of my projects, I was able to exclude java file from compiling by not using the srcDir path in the exclude path. exclude usually works without appending the srcDir path.

res{
    srcDirs += ["src/module1/res"]
    res.srcDirs += ["src/module1/res"]        
    exclude 'drawable/specific_file.xml'
}
Sam
  • 2,935
  • 1
  • 19
  • 26
  • As explained in my question, I've tried that already but it doesn't work – ricardo silva Mar 19 '18 at 14:02
  • Focus on the `exclude` path. you have this `exclude 'src/module1/res/drawable/specific_file.xml'`, Can you try this `exclude 'drawable/specific_file.xml'` – Sam Mar 19 '18 at 14:25