Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle: Exclude file from Android assets folder

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
  }
}
like image 787
Blackhex Avatar asked Sep 18 '14 10:09

Blackhex


5 Answers

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...

like image 59
noblemaster Avatar answered Nov 09 '22 18:11

noblemaster


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

like image 24
Noel De Martin Avatar answered Nov 09 '22 17:11

Noel De Martin


It's not possible at the moment.

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

like image 10
Xavier Ducrohet Avatar answered Nov 09 '22 16:11

Xavier Ducrohet


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/**']))
    }
 }
}
like image 7
Tanuj Jagoori Avatar answered Nov 09 '22 18:11

Tanuj Jagoori


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.

like image 1
GrieveAndrew Avatar answered Nov 09 '22 17:11

GrieveAndrew