如何清理Android项目?
我使用了我们过去创建的 Android 应用程序的源代码作为另一个应用程序的基础(很多功能是相似的,但是原始应用程序的大约 1/3 功能被删除)。
我首先复制源代码树并将新项目导入 Eclipse 工作区。之后,我删除了一些布局和可绘制文件以及一些 .java
文件 - 这些文件显然在新应用程序中不需要。
最后,我编译并测试了新的应用程序。现在我想通过删除几乎所有未使用的内容来对项目进行更彻底的清理。
查找新应用程序中未使用的所有 XML 和图像文件、Java 文件、字符串和颜色资源的最佳方法是什么?我所说的“未使用”是指它们没有被任何正在执行的应用程序代码引用(从主要活动开始,转到从中引用的所有其他文件,从这些文件引用的文件 - 等等)。
该项目包含数百个 Drawable(其中许多都有 HDPI、MDPI 和 LDPI 版本)、大约一百个布局以及十几个不同包中的近千个 Java 文件。因此,一一检查并不是一种选择。
2012 年 3 月编辑: android SDK 的 lint
工具的最新版本可以很好地识别未使用的资源。问题是从我收到的数百条其他消息中过滤有关未使用资源的消息。当我在中型项目上运行 link 时,我收到了 3,000 多个警告,其中大多数警告我不打算采取任何措施,因为 lint 无法分析我在代码中使用 XML 布局所做的操作。
I used source code of an android app that we created in the past as a basis for another app (a lot of the functionality is similar, however about 1/3 of the functionality of the original app is removed).
I started by copying the source tree and importing a new project into Eclipse workspace. After that, I deleted some layout and drawable files and a few .java
files - the ones that obviously are not needed in a new app.
Finally, I have compiled and tested the new app. Now I would like to do a more thorough cleanup of the project by deleting pretty much everything that's not used.
What is the best way to find all XML and image files, Java files, strings and colour resources that are not used in the new app? By "not used", I mean that they are not referenced from any of the executing application code (starting from main activity, go to all other files referred from it, files referred from those files - and so on).
The project contains hundreds of Drawables (many of them have versions in HDPI, MDPI and LDPI), around a hundred or so layouts and close to a thousand Java files in a dozen different packages. Therefore, checking them one by one is not an option.
Edit March 2012: The latest version of android SDK's lint
tool identifies unused resources quite well. The issue is to filter the messages about unused resources from hundreds of other messages I got. When I ran link on my medium-sized project, I got over 3,000 warnings from it, most of which I intend to do nothing about, because lint cannot analyse what I do in the code with my XML layouts.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请查看 http://code.google.com/p/android-unused-resources/
它是开源的,正在积极开发中,并且由优秀的开发人员创建/维护。
Check out http://code.google.com/p/android-unused-resources/
It's open source, in active development, and created/maintained by a great developer.
AFAIK,没有自动化工具可以做到这一点。我也曾寻找一种方法来检测未使用的资源,并得出结论,没有可用的工具。另外,你正在谈论未使用的Java文件,我认为这更难完成(很难实际检测到实际使用了哪些活动/服务/接收者/提供者等)。
我的建议是在项目树中查找您怀疑不再使用的资源和类,并删除那些不再出现在任何地方的资源和类。
AFAIK, there's no automated tool for this. I too have searched for a way to detect unused resources and came to the conclusion there's no tool for that. Also, you are talking about unused Java files, which I think is even harder to accomplish (it's hard to actually detect which activities / services / receivers / providers are actually used, etc.).
What I suggest is grepping through the project tree for the resources and classes that you suspect are not used anymore and remove those that do not appear anywhere.