排除发布构建类型的资产

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'
}

此库仅包含要在 webview 中使用的资产、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-libgreat-lib-debug),并且在中有依赖debugCompilereleaseCompile,但我宁愿避免这种情况并发布单个版本)

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

这篇关于排除发布构建类型的资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Android release APK crash with java.lang.AssertionError: impossible in java.lang.Enum(Android 发布 APK 因 java.lang.AssertionError 崩溃:在 java.lang.Enum 中不可能)
Finished with Non Zero Exit Value 3(以非零退出值 3 结束)
On gradle:3.0.0 More than one file was found with OS independent path #39;META-INF/ASL2.0#39;(在 gradle:3.0.0 上找到多个文件,其独立于操作系统的路径为“META-INF/ASL2.0)
Android : app loading library at runtime on Lollipop but not IceCreamSandwich(Android:运行时在 Lollipop 上而不是 IceCreamSandwich 上的应用程序加载库)
buildConfigField depending on flavor + buildType(buildConfigField 取决于风味 + buildType)
How do I suppress warnings when compiling an android library with gradle?(使用 gradle 编译 android 库时如何抑制警告?)