简体   繁体   中英

Remove not needed resources from android library in apk

I have a shared library that is used by multiple variance of the app. I want to be able to strip out unused library resources as per app configuration.

  • I don't want to remove the resources permanently, so Lint does not help
  • The resources are referenced by shared code, I just know that in certain variance, a sizeable chunk is not used
  • The shared code which reference the resources are being used (just in certain usage variance, a sizeable chunk is not in play) so proguard also does not help

I'm heading toward hackery with ant build script to remove it from the library jar. Any other idea?

Found my solution, use property aapt.ignore.assets to drop out whatever I don't want in the final apk. Snip from sdk r22 tools/ant/build.xml

    <!-- 'aapt.ignore.assets' is the list of file patterns to ignore under /res and /assets.
     Default is "!.svn:!.git:.*:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

     Overall patterns syntax is:
       [!][<dir>|<file>][*suffix-match|prefix-match*|full-match]:more:patterns...

     - The first character flag ! avoids printing a warning.
     - Pattern can have the flag "<dir>" to match only directories
       or "<file>" to match only files. Default is to match both.
     - Match is not case-sensitive.
-->
<property name="aapt.ignore.assets" value="" />

The unintended side effect of this property is it get used during "code-gen" as well and the drawable id will not be generated and proguard will complain. You will need to add dontwarn into your config.

You will also want to wrap these resources into its own class that won't be invoked when the resources are not in play. Because if they are in a common class, even if not used, java static initializer will crash trying to link them up.

将从.apk构建中排除整个./www/app目录:

<property name="aapt.ignore.assets" value="&lt;dir&gt;app:" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM