24

Please, do you know a way how to exclude some file from Android project assets folder before multiple assets folders are merged or during the merge?

For example:

android {
  sourceSets {
    main {
      assets.srcDirs = [fileTree(dir: 'assets1', exclude: 'myfile.txt'), 'assets2'] // does not work
      assets.exclude 'assets1/myfile.txt' // does not work
      assets.exclude '**/*.txt' // does not work
      assets.exclude '*.txt' // does not work
    }
  }

  packagingOptions {
    exclude 'assets1/myfile.txt' // does not work
    exclude '**/*.txt' // does not work
    exclude '*.txt' // does not work either
  }


  aaptOptions {
    ignoreAssetsPattern "myfile.txt" // does not work
  }
}
Blackhex
  • 1,694
  • 3
  • 21
  • 45
  • FYI: `assets.srcDirs = [fileTree(dir: 'assets1', exclude: 'myfile.txt'), 'assets2']` does not work. – Blackhex Oct 22 '14 at 10:49
  • Did you find a solution to this? Having the same issue with some assets file from a jar dependency – 3c71 Mar 01 '15 at 08:27

7 Answers7

24

I run into the same problem and it seems adding a "!" works to indicate the file should be excluded:

aaptOptions {
    ignoreAssetsPattern "!myfile.txt" 
}

"assets.exclude" might work also by adding a "!" but I haven't tested it...

noblemaster
  • 404
  • 4
  • 8
  • 11
    use `*` for wide char, and use `:` to split patterns. like: **"!*.txt:!*.rtf"** – Behrouz.M Jun 11 '16 at 10:51
  • ! indicate about suppressing warning. Docs: https://android.googlesource.com/platform/frameworks/base/+/6d2c0e5/tools/aapt2/Files.h#88 – Ivan Shafran Jun 04 '20 at 14:52
  • Not sure if the docs you mention are related or wrong? I just double-checked and the '!' does indeed ignore the assets. I am aware it's odd, but that's just how it seems to be implemented. – noblemaster Jun 05 '20 at 15:13
11

I think this should do what you want:

android {
    aaptOptions {
        ignoreAssetsPattern "myfile.txt"
    }
}

Source: http://tools.android.com/tech-docs/new-build-system/user-guide#TOC-aapt-options

Noel De Martin
  • 2,779
  • 4
  • 28
  • 39
10

It's not possible at the moment.

The packagingOptions feature does not apply to Android resources or assets.

Xavier Ducrohet
  • 28,383
  • 5
  • 88
  • 64
  • Thanks, I'll try to experiment with dependency on Copy task or FileTree first before I'll give it up... – Blackhex Sep 19 '14 at 16:17
7

we can delete files/folders after build.gradle has finished its internal task of merging all assets .

android.applicationVariants.all { variant -> }

this is the loop where many android build tasks(in groovy language) are offered.

so in this case mergeAssets.doLast is the groovy task where we can perform delete operations.

here i used code to delete a zip file and images folder

tested on Android 3.1.4

hope it helps

android.applicationVariants.all { variant ->

if (variant.buildType.name == 'release') {

    variant.mergeAssets.doLast {
        println("deleting item.zip', 'images/**' from variant")
        delete(fileTree(dir: variant.mergeAssets.outputDir, includes: ['item.zip', 'images/**']))
    }
 }
}
Community
  • 1
  • 1
Tanuj Jagoori
  • 309
  • 3
  • 9
1

Try this:

export ANDROID_AAPT_IGNORE="ignoreAssetsPatternThatActuallyWorks"
./gradlew assembleDebug

It's the only way to influence the mergeDebugAssets step (code found here).

Filed a bug about this.

GrieveAndrew
  • 196
  • 2
  • 5
0

just use bash

zip -d xx.jar xxx.txt

to remove duplicated file from jar file

AlessioX
  • 3,167
  • 6
  • 24
  • 40
lotosbin
  • 637
  • 9
  • 9
0

To protect assets folder information If the html file is either css or js, the easiest way is to: Write your code in the html editor first, and then enter it in a Java class as follows :

 public class Content{
    public static final String  myContent ="<!DOCTYPE html> ... </html> "

And then call through the loadDataWithBaseURL method

 webView.loadDataWithBaseURL(null,Content.myContent, "text/html" , "UTF-8" ,null);

And you can call js and css in html code :

 ...
 <head>
 <link rel="stylesheet" type="text/css" 
  href="file:///android_asset/css/custom.css" /> 
 <script src="file:///android_asset/js/code.jquery.js"></script>                                                                                   
</head>
AnAIDE
  • 21
  • 2