排除发布版本类型的资产 [英] Exclude assets for release build type
问题描述
我正在使用gradle构建的应用程序中导入一个android库,如下所示:
I'm importing an android library in an application built with gradle, like that:
dependencies {
compile 'com.example:great-lib:0.1-SNAPSHOT'
}
此库仅包含要在Web视图中使用的资产,js,css和图像,其布局如下:
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/
| ...
js
文件夹包含源文件(与源映射一起使用).我想将其和.map
文件包含在调试版本中,并且在发行版本中仅包含缩小的js,但我找不到解决方法.
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?
(我还考虑过发布两个版本的库(great-lib
和great-lib-debug
),并且在debugCompile
和releaseCompile
中具有依赖性,但是我宁愿避免这种情况并只发布一个版本)
(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)
推荐答案
我最终做了以下事情:
android.applicationVariants.all { variant ->
if (variant.name.contains('Release')) {
// exclude source and sourcemap from release builds
def noJsSourceTask = task("delete${variant.name}JsSource", type: Delete) {
delete "${buildDir}/intermediates/assets/${variant.dirName}/js"
delete "${buildDir}/intermediates/assets/${variant.dirName}/great.min.js.map"
}
variant.mergeAssets.finalizedBy noCeJsSourceTask
}
}
可以,但是有些事情我不是很喜欢:
It works ok, but there are a few things I don't really like:
- 我要触摸任务完成后生成的文件(
finalizedBy
),所以它不能与最新"检查一起很好地工作.但这仅适用于发行版,我会更频繁地进行调试 - 要删除的文件的路径是手动建立的.我不确定它的通用性是否足以按原样在其他项目中重用
- 我要根据其名称选择变体.我本来希望更结构化.
- I'm touching at the files produced by a task after it is done (the
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 often - the path of the files to delete is manually built. I'm not sure if it's generic enough to be reused in other projects as-is
- I'm selecting the variants based on their name. I would have liked something more structured.
这篇关于排除发布版本类型的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!