Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude certain files from Android Studio gradle builds?

I am using Android Studio with Google Drive, which makes hidden system files visible (such as Icon and desktop.ini), causing errors, as described here. Rather than run scripts to delete these files, I'd like to instruct gradle 2.14 to ignore them.

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

Specifically, I made the following modification to the build.gradle file for Module: app, per 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, per 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 selected Build > Clean Project, which triggered a 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 point:

aaptOptions {
    ignoreAssetsPattern "!excludeme.txt"
}

How do I properly instruct gradle to ignore files containing a certain pattern when building Android projects?

Update 1

Per Stefan's suggestion 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 named whatever.txt in the res/layout directory, and the build failed with:

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

Per Nathan's suggestion, 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, which were on Windows.)

like image 371
Ellen Spertus Avatar asked Jan 27 '17 17:01

Ellen Spertus


People also ask

How do I exclude a particular class in Gradle?

You may also use a closure or Spec to specify which files to include or exclude. The closure or Spec is passed a FileTreeElement, and must return a boolean value. See Jar. exclude, FileTreeElement and Finding Files.

Can I delete the .Gradle file?

Inside the project you can find the . gradle folder. Inside you can find all settings and other files used by gradle to build the project. You can delete these files without problems.

Why there are two Gradle files in Android Studio?

Android Studio projects contain a top-level project Gradle build file that allows you to add the configuration options common to all application modules in the project. Each application module also has its own build. gradle file for build settings specific to that module.

What is task clean in Gradle?

Gradle adds the task rule clean<Taskname> to our projects when we apply the base plugin. This task is able to remove any output files or directories we have defined for our task. For example we can assign an output file or directory to our task with the outputs property.


1 Answers

Looks like a recent change in gradle changed the DSL for sourceSets, so you now need to specify the exclude on the filter field:

android {
    sourceSets.main.res.filter.exclude '**/whatever.txt'
}
like image 60
Diarrhio Avatar answered Sep 21 '22 18:09

Diarrhio