如何避免 Android 库项目中未使用的资源和代码进入我的 APK? [英] How to avoid unused resources and code from an Android Library Project getting into my APK?
问题描述
我的 Eclipse 工作区中有一个 Android 库项目,我将其用作独特的工具箱.偶然(1),我刚刚发现库中的资源(xml 动画、xml 布局,甚至可绘制对象!!!)被打包到使用该库的项目的 APK 中,即使我不使用它们.
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.
看完android是否总是打包未使用的资源?,不知是否正常.我怎样才能避免这种情况?拥有不同的图书馆项目是唯一的方法吗?
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?
编辑:我发现,在反编译 .dex 时,未使用的代码也会进入 apk.
EDIT: I've found, decompiling the .dex, that unused code too makes it into the apk.
(1) 我试图为我的应用程序测试一个新图标 /res/drawable/icon.png
,但默认图标会一直出现.我删除了图像,它一直显示默认图标!它必须是库中的 /res/drawable-mdpi/icon.png
.
(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.
推荐答案
新的 Android 构建系统有一个 资源剥离机制 内置,可以作为构建过程的最后一步运行.除了删除 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.
请注意,资源剥离机制与 Proguard(也与构建系统捆绑在一起)结合使用特别有用以及当您在项目中使用库时.思路是:
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 会删除您不使用的类,包括来自库的类.
- 上述过程可以删除对这些库中包含的资源的代码引用.
- 因此可以从 APK 中删除那些未引用的资源,因为没有代码再使用它们.
删除 lint 标识的未使用资源仍然很有用,因为删除它们:
Removing unused resources that lint identifies is still useful because removing them:
- 加快构建速度
- 减轻项目的维护负担.
这篇关于如何避免 Android 库项目中未使用的资源和代码进入我的 APK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!