Scorpio
Scorpio

Reputation: 1144

Gradle remove resource files from build

I am trying to generate some files according to some flags using Mako. Excluding the unnecessary information, I want to have some *.tpl files both in the src and res folders of the project and I need them to be ignored at build time.

I am using Android Studio, so I build using Gradle. My build.gradle file looks like this:

sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            res.srcDirs = ['res']
            assets.srcDirs = ['assets']
        }
    }

I have tried to remove those files from build changing the file to this:

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

That works for the java files, but at build time I get this error:

:Presentation:generateReleaseResValues UP-TO-DATE
:Presentation:generateReleaseResources UP-TO-DATE
:Presentation:packageReleaseResources
D:\example\res\layout\stuff.xml.tpl
Error:Error: The file name must end with .xml
:Presentation:packageReleaseResources FAILED
Error:Execution failed for task ':Presentation:packageReleaseResources'.
> D:\example\res\layout\stuff.xml.tpl: Error: The file name must end with .xml
Information:BUILD FAILED

Any idea what I'm doing wrong here?

Later Edit: Other things I tried: As per @Stanislav 's answer, I changed to exclude '**/*.xml.tpl' which fails with the same error.

I then tried to change to exclude '**/*.tpl.xml' to avoid that error, but I got this one:

Error:Execution failed for task ':Presentation:packageReleaseResources'.
> D:example\res\layout\stuff.tpl.xml: Error: '.' is not a valid file-based resource name character: File-based resource names must contain only lowercase a-z, 0-9, or underscore

So the next test was to name the file stuff_tpl.xml and the exclude command to exclude '**/*_tpl.xml', but that throws the error:

Error:(1) Error parsing XML: syntax error
D:example\res\layout\stuff_tpl.xml

I also tried the following lines:

excludes = ['**/*_tpl.xml']
excludes = ['layout/*_tpl.xml']
excludes = ['layout/stuff_tpl.xml']

But none of them removed the file from the build.

Upvotes: 2

Views: 3424

Answers (1)

Scorpio
Scorpio

Reputation: 1144

According to the answer to this question, it is not possible to remove Android resources from build, but only JAVA resources.

I am now storing the template files in a separate folder, outside the project and using Mako and an additional python script to move them to the drawable folder in the project generation step.

Upvotes: 2

Related Questions