I'm importing an android library in an application built with gradle, like that:
dependencies {
compile 'com.example:great-lib:0.1-SNAPSHOT'
}
This library contains only assets, js, css and images to be used in a webview, with a layout like that:
assets/
|-> great.css
|-> great.min.js
|-> great.min.js.map
|-> js/
| |-> plop.js
| |-> foo.js
| ...
|-> img/
| ...
The js
folder contains source files (to be used with source maps). I would like to include it and the .map
file for the debug builds, and have only the minified js in release builds, but I can't find a way to do that.
So far I've tried :
android {
// this doesn't exclude anything
packageOptions {
exclude 'assets/js'
}
buildTypes {
release {
// this does exclude the js folder, but in both release and debug
aaptOptions {
ignoreAssetsPattern "!js"
}
}
}
}
Any idea if what I want is possible to achieve, and if so how?
(I've also thought of publishing two versions of the library (great-lib
and great-lib-debug
), and have the dependency in debugCompile
and releaseCompile
, but I'd prefer avoiding that and publishing a single version)
It's not possible through a filter.
You could have 2 assets folders though. a main one (src/main/assets) used for both
debug
andrelease
and one (src/debug/assets) used only for the debug build.source
I ended up doing the following:
It works ok, but there are a few things I don't really like:
finalizedBy
), so it doesn't work well with "up-to-date" checking. But it's only for release builds, I'm doing debug ones more oftenGradle provides "aaptOptions, ignoreAssetsPattern" to filter/exclude assets folders and files from release or debug build.
Example for debug build (
js
folder andgreat.css
files):Example for release build (
js
folder andgreat.css
files):I think you can use proguard. Proguard is include with android studio,obfuscate code, and remove not used classes, and if you want remove all resources that app not used. Only put in your build.gradle this:
This is link information about that:
You can personalize, exclude particular files or ignore particular files
I had success with this approach:
This should address the issues with @Xavier's answer:
mergeAssets
task so the deletion is reflected in the task's output and up-to-date checking should be unaffected.buildType
name, which is less problematic than matching the entire variant name (though it is still stringly typed).Note that this approach also works for
res
files rather thanassets
: just replacemergeAssets
withmergeResources
.Other answers mentioning
packagingOptions
andaaptOptions
are barking up the wrong tree, as these are scoped to all variants (they are defined in theandroid
scope, notbuildType
orproductFlavor
).