简体   繁体   English

从APK中的android库中删除不需要的资源

[英]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 我不想永久删除资源,因此Lint无济于事
  • 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 引用资源的共享代码正在使用中(只是在某些使用差异下,没有很大的块在起作用),因此proguard也无济于事

I'm heading toward hackery with ant build script to remove it from the library jar. 我正在使用蚂蚁构建脚本将其从库jar中删除,从而走向黑客。 Any other idea? 还有其他想法吗?

Found my solution, use property aapt.ignore.assets to drop out whatever I don't want in the final apk. 找到我的解决方案,使用属性aapt.ignore.assets删除最终apk中不需要的所有内容。 Snip from sdk r22 tools/ant/build.xml sdk r22工具/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. 此属性的意外副作用是,它也会在“代码生成”期间使用,并且不会生成drawable id,并且proguard会抱怨。 You will need to add dontwarn into your config. 您将需要将dontwarn添加到您的配置中。

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. 因为如果它们在公共类中,即使不使用它们,java静态初始化程序也会在尝试将它们链接时崩溃。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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