Skip to content Skip to sidebar Skip to footer

Exclude Assets For Release Build Type

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 asset

Solution 1:

I had success with this approach (updated 2019-5-13 for TaskProvider support; see edit history for older versions):

android {
⋮
    applicationVariants.all { variant->
        if (variant.buildType.name == 'release') {
            variant.mergeAssetsProvider.configure {
                doLast {
                    delete(fileTree(dir: outputDir, includes: ['**/js', '**/*.js.map']))
                }
            }
        }
    }
⋮
}

This should address the issues with @Xavier's answer:

  • The deletion is done as part of the variant's mergeAssets task so the deletion is reflected in the task's output and up-to-date checking should be unaffected.
  • The paths are calculated without magic strings. You may need to adjust the include patterns in case my example is too permissive.
  • The variant is being selected by the 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 than assets: just replace mergeAssets with mergeResources.

Other answers mentioning packagingOptions and aaptOptions are barking up the wrong tree, as these are scoped to all variants (they are defined in the android scope, not buildType or productFlavor).

Solution 2:

I ended up doing the following:

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:

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

Solution 3:

Gradle provides "aaptOptions, ignoreAssetsPattern" to filter/exclude assets folders and files from release or debug build.

Example for debug build (js folder and great.css files):

debug {            
        aaptOptions {
            ignoreAssetsPattern '!js:!great.css:'
        }
    }

Example for release build (js folder and great.css files):

release {
        aaptOptions {
            ignoreAssetsPattern '!js:!great.css:'
        }
    }

Solution 4:

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:

 release {

            minifyEnabled true//remove classes, obfuscate code and zipalign
            shrinkResources true//remove resources
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'//autogenerated files
        }

This is link information about that:

You can personalize, exclude particular files or ignore particular files

Solution 5:

It's not possible through a filter.

You could have 2 assets folders though. a main one (src/main/assets) used for both debug and release and one (src/debug/assets) used only for the debug build.

source

Post a Comment for "Exclude Assets For Release Build Type"