Is there a tool to find unused resources in an And

2019-03-11 14:08发布

I'd like to find any unused resources in my Android project - this includes strings, ids, drawables, integers etc.

Does a tool to do this currently exist (preferably for Eclipse)?

5条回答
不美不萌又怎样
2楼-- · 2019-03-11 14:37

If you use IntelliJ, which has Android support in the free community edition, you can do this by opening the generated R.java file (gen/R.java). The unused resources will be marked with a warning for not being referenced anywhere in your project.

I'd be surprised if Eclipse doesn't do the same thing.

查看更多
别忘想泡老子
3楼-- · 2019-03-11 14:38

android-unused-resources is a Java tool that will detect unused resources, and tell you where they are located. It processes Java and XML files, so it avoids the problem in the accepted answer.

It's not perfect, but as long as you don't dynamically load resources (getIdentifier(java.lang.String, java.lang.String, java.lang.String)), It shouldn't tell you to delete any that are actually being used (although you'll get a compiler error even if that happens).

查看更多
三岁会撩人
4楼-- · 2019-03-11 14:39

A friendly note: The IntelliJ idea ONLY works for resources that are not referenced in JAVA code. So if you have 1500 resources and only 20 are referenced directly from your java code, you end up with 1480 unused warnings in that file.

I'm seeing things marked as unused that I can clearly see are in use in various layouts. So keep that in mind ... don't go on a deleting spree.

查看更多
迷人小祖宗
5楼-- · 2019-03-11 14:51

Andrei Buneyeu is right to Android Lint is the way to go. To specifically answer the question for anyone else looking, the command is:

lint --check UnusedResources <path to project>

There are a lot more options in lint that you can see with lint --list.

查看更多
SAY GOODBYE
6楼-- · 2019-03-11 15:03

Update to ADT 16 and use Android Lint. It is really amazing tool.

Android Lint is a new tool for ADT 16 (and Tools 16) which scans Android project sources for potential bugs.

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.
查看更多
登录 后发表回答