How to exclude file from resources using Gradle an

2019-04-08 22:14发布

问题:

I have a generated string resource xml file in my project which is generated from a file with a .template extension. I'm trying to exclude this template from the build since it gives me duplicate resource error. The following does not work for me and I'm stuck:

  sourceSets {
       main {
           manifest.srcFile 'AndroidManifest.xml'
           java.srcDirs = ['src']
           resources.srcDirs = ['res']
           resources.exclude '**/build_id.template'
           res.srcDirs = ['res']
           res.exclude '**/build_id.template'
           assets.srcDirs = ['assets']
       }
   }

The error:

Error:Error: Duplicate resources: <project-dir>/res/values/build_id.template:string/build_id, <project-dir>/res/values/build_id.xml:string/build_id

How can I exclude a particular resource file properly?

回答1:

For the sanity of other folks working on your project, I strongly discourage putting templates (input) and auto-generated files (output) in the same directory. You could create your own, independent 'templates' directory to keep it from being included in your build.