Search code examples
androidandroid-proguardandroid-buildandroid-moduleandroid-r8

Proguard/R8 rule to exclude asset folder


My project is comprised of the main module, followed by other module dependencies. One such dependency is "commons". In "commons" there is an asset folder called "flags" let's say.

If we know in advance that this folder will never be used, is there a way to write a proguard rule to exclude this specific "flags" folder?

In my project, the main module does not use any code from the "commons" module that specifically targets this folder, yet it is still included in the final package.


Solution

  • Neither R8 nor Proguard can remove assets as these are not even passed as inputs to these tools. Also, even if they are part of the inputs, it is non-trivial to find if asset is used or not. For the same reason R8 and Proguard do not remove Java resources (they may only change their path/names).

    Solution for your problem could be using the DSL in the application build.gradle file. Namely, if you add:

    android {
      aaptOptions {
        ignoreAssetsPattern "<dir>flags"
      }
    }
    

    the final APK will not contain any files under flags directory. Keep in mind that this option is not variant-specific, and all application variants (e.g. debug, release) will not contain these assets.

    Hope that helps.