如何避免Android库项目中未使用的资源和代码进入我的APK? [英] How to avoid unused resources and code from an Android Library Project getting into my APK?

查看:230
本文介绍了如何避免Android库项目中未使用的资源和代码进入我的APK?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Eclipse工作区中有一个Android Library Project,我将其用作唯一的工具箱.一次偶然的机会(1),我发现该库中的资源(xml动画,xml布局,甚至可绘制对象!!!)都打包到了使用该库的项目的APK中,即使我不使用它们./p>

阅读 Android总是打包未使用的资源吗? ,我想知道这是否正常.如何避免这种情况?唯一的方法是拥有不同的图书馆项目吗?

编辑:我发现,在对.dex进行反编译时,未使用的代码也会使其进入apk.

(1)我正在尝试为我的应用测试新图标/res/drawable/icon.png,但是默认图标会一直显示.我删除了图像,并一直显示默认图标!它必须是库中的/res/drawable-mdpi/icon.png .

解决方案

新的Android构建系统具有 Proguard (也与构建系统捆绑在一起)一起使用时特别有用以及在项目中使用库时.这个想法是:

  • Proguard会删除您不使用的类,包括那些来自库的类.
  • 上述过程可以删除对那些库中包含的资源的代码引用.
  • 由于没有代码可以使用这些未引用资源,因此它们可以从APK中剥离.

删除lint标识的未使用资源仍然很有用,因为删除它们:

  • 加快构建速度
  • 减轻项目的维护负担.

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.

解决方案

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.

这篇关于如何避免Android库项目中未使用的资源和代码进入我的APK?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆