如何避免 Android 库项目中未使用的资源和代码进入我的 APK?
我的 Eclipse 工作区中有一个 Android 库项目,我将其用作独特的工具箱。一次偶然的机会(1),我刚刚发现库中的资源(xml 动画、xml 布局、甚至可绘制对象!!!)被打包到使用该库的项目的 APK 中,即使我不使用它们。
阅读android是否总是打包未使用的资源?后,我想知道如果正常的话。我怎样才能避免这种情况?拥有不同的图书馆项目是唯一的方法吗?
编辑:我发现,反编译.dex,未使用的代码也将其放入apk中。
(1) 我试图为我的应用程序测试一个新图标 /res/drawable/icon.png
,但默认图标会继续出现。我删除了图像,它仍然显示默认图标!它必须是库中的 /res/drawable-mdpi/icon.png
。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
新的 Android 构建系统内置了资源剥离机制其中可以作为构建过程的最后一步运行。除了删除 lint 识别的资源之外,还可以使用它。
请注意,资源剥离机制与 Proguard(也与构建系统捆绑在一起)结合使用时特别有用,并且当您在您的项目中使用库。这个想法是:
删除 lint 识别的未使用资源仍然有用,因为删除它们:
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:
Removing unused resources that lint identifies is still useful because removing them:
Proguard 可以删除未使用的代码。
但它不会对未使用的资源做任何事情,而且还会混淆您的代码。在使用它之前你必须考虑一下。
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.
正如有人之前所说, Proguard 可以删除未使用的代码。
要删除未使用的资源,您可以使用 ADT 16 中的 Android Lint 工具。它不仅可以帮助您删除未使用的资源,同时也能发现潜在的bug。这段话来自其官方网站:
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: