How to avoid unused resources and code from an Android Library Project getting into my APK?

梦想与她 提交于 2019-12-04 10:48:04

问题


I have an Android Library Project in my Eclipse workspace, that I use as a unique toolbox. By chance(1), I just discovered that resources from the library (xml animations, xml layouts, even drawables!!!) are packed into the APK's of the projects that use the library, even if I don't use them.

After reading Does android always package unused resources?, I wonder if it's normal. How can I avoid this? Is the only way having different library projects?

EDIT: I've found, decompiling the .dex, that unused code too makes it into the apk.

(1) I was trying to test a new icon for my app, /res/drawable/icon.png, but the default icon would keep appearing. I removed the image and it kept showing the default icon! It had to be the /res/drawable-mdpi/icon.png from the library.


回答1:


The new Android build system has a resource stripping mechanism built in that can be run as a final step on the build process. Use it in addition to removing resources that lint identifies.

Note that the resource stripping mechanism is particularly useful in conjunction with Proguard (also bundled with the build system) and when you're using libraries in your project. The idea is:

  • Proguard removes classes that you're not using, including those that came from libraries.
  • The above process can delete code references to resources included with those libraries.
  • Those unreferenced resources can thus be stripped from the APK, because there's no code that uses them anymore.

Removing unused resources that lint identifies is still useful because removing them:

  • Speeds up your build
  • Reduces your project's maintenance burden.



回答2:


Proguard can strip unused code.

But it won't do anything about unused resources and it'll also obfuscate your code. You have to give it some thought before using it.




回答3:


As somebody said before, Proguard can strip unused code.

To remove unused resources you can use Android Lint tool from ADT 16. It will help you not only to remove unused resources, but also to find potential bugs. This quote is from its official site:

Here are some examples of the types of errors that it looks for:

  • Missing translations (and unused translations)
  • Layout performance problems (all the issues the old layoutopt tool used to find, and more)
  • Unused resources
  • Inconsistent array sizes (when arrays are defined in multiple configurations)
  • Accessibility and internationalization problems (hardcoded strings, missing contentDescription, etc)
  • Icon problems (like missing densities, duplicate icons, wrong sizes, etc)
  • Usability problems (like not specifying an input type on a text field)
  • Manifest errors and many more.


来源:https://stackoverflow.com/questions/5560372/how-to-avoid-unused-resources-and-code-from-an-android-library-project-getting-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!