How to exclude certain files from Android Studio gradle builds?

I use Android Studio with Google Drive, which makes hidden system files (such as Icon and desktop.ini ) visible, causing errors as described here . Instead of running scripts to delete these files, I would like to instruct gradle 2.14, to ignore them.

In an attempt to figure out how to exclude files, I created files with the name "excludeme.txt" in the directories \app\src\main\res\values and in \app\src\main\java\com\hfad\stopwatch\ . I tried unsuccessfully various modifications of the two build.gradle files in my project. None of them excluded the file.

In particular, I made the following modification to the build.gradle file for Module: app , for this question :

 dependencies { compile fileTree(dir: 'libs', include: ['*.jar'], excludes: ['**/excludeme.txt']) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) testCompile 'junit:junit:4.12' } 

I also added this to the android section of the same build.gradle file, for this question :

 aaptOptions { ignoreAssetsPattern "!excludeme.txt" } 

I also added:

 sourceSets { main { java { srcDir "src" [I also tried w/o this line] exclude "**/excludeme.txt" } } } 

When I chose Build > Clean Project , which called the build, I got this output, which shows that the file in the resources folder was not ignored:

 Information:Gradle tasks [clean, :app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources] C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Stopwatch\app\src\main\res\values\excludeme.txt Error:(1, 1) Error: Premature end of file. Error:Execution failed for task ':app:mergeDebugResources'. > C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Stopwatch\app\src\main\res\values\excludeme.txt:1:1: Error: Premature end of file. 

The results were the same if I removed the exclamation mark:

 aaptOptions { ignoreAssetsPattern "!excludeme.txt" } 

How to properly instruct gradle to ignore files containing a specific template when creating Android projects?

Update 1

As suggested by Stefan below, I added the following to the build.gradle file for Module: app :

 android { [omitted] sourceSets.main.res.exclude '**/whatever.txt' } 

I added a file called whatever.txt to the res/layout directory, and the build failed:

 Error:Execution failed for task ':app:mergeDebugResources'. > C:\Users\Ellen\GoogleDrive\Ellen-CS115\AndroidStudioProjects\Starbuzz\app\src\main\res\layout\whatever.txt: Error: The file name must end with .xml 

Update 2

At the suggestion of Nathan, I added:

 packagingOptions { exclude '**/whatever.txt' exclude 'src/main/res/layout/whatever.txt' } 

to build.gradle (Module: app) in the android section. No improvement:

 Information:Gradle tasks [:app:generateDebugSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:generateDebugAndroidTestSources] /Users/spertus/GoogleDrive/Ellen-CS115/AndroidStudioProjects/MacTest/app/src/main/res/layout/whatever.txt Error:Error: The file name must end with .xml Error:Execution failed for task ':app:mergeDebugResources'. > /Users/spertus/GoogleDrive/Ellen-CS115/AndroidStudioProjects/MacTest/app/src/main/res/layout/whatever.txt: Error: The file name must end with .xml Information:BUILD FAILED 

(This last build was on a Mac, unlike the others that were on Windows.)

+8
android-studio android-gradle build.gradle gradle
source share
3 answers

It looks like a recent change in gradle has changed the DSL for sourceSets, so now you need to specify an exception in the filter field:

 android { sourceSets.main.res.filter.exclude '**/whatever.txt' } 
+1
source share

The Android plugin has its own concept of SourceSets: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-Configuring-the-Structure

So you probably want to do something like

 android { sourceSets.main.res.exclude '**/whatever.txt' } 
0
source share

In a child project, in your case, I believe this is :APP , add the following to the build.gradle file.

 packagingOptions { //Exclude the file(s) in this method. Provide the path and file name. exclude 'src\main\res\layout\whatever.txt' } 

The PackagingOptions DSL object allows you to control which files are included in the APK . exclude takes a string or pattern as an argument, which allows you to ignore files or directories by name or pattern.

EDIT Resource settings from Android Studio Docs .

  • Create an XML file in your project with the <resources> .
  • Add tools:keep="[list resources]"
  • Add `tools: discard =" [resource list] "
  • Save the file in the project resources in res/raw/keep.xml The assembly will not specify this file.

The final file should look the same as in Android Studio Docs

 <?xml version="1.0" encoding="utf-8"?> <resources xmlns:tools="http://schemas.android.com/tools" tools:keep="@layout/l_used*_c,@layout/l_used_a,@layout/l_used_b*" tools:discard="@layout/unused2" /> 
0
source share

All Articles