Monday, March 26, 2018

Add folder to srcDir except for one file in gradle

Leave a Comment

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?

2 Answers

Answers 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" /> 

Answers 2

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' } 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment