Android中未使用的字符串和图像资源

问题描述 投票:33回答:8

我的项目有大量的String和ImageResources,其中许多我认为是未使用的。有什么方法可以找出我的代码当前没有引用这些资源中的哪些?

android
8个回答
76
投票

在Android Studio(试用到2.1.3版)中,您只需转到主菜单 - >分析 - >按名称运行检查...,然后选择未使用的资源

(它为你运行Android Lint。)


8
投票

在Android Studio中,有一个可用的快捷方式。

按Ctrl + Alt + Shift + i(或Mac上的Cmd + Alt + Shift + i)

这将打开一个对话框,您可以在其中键入“未使用”以查看许多选项。

我通常输入“未使用的资源”并获得包括布局,字符串,绘图等的列表。


5
投票

在Android Studio 2.0及更高版本中

在菜单中选择Refactor - >单击Remove Unused Resources ...


2
投票

改进@Ramesh R答案,最好的方法是右键单击res文件夹,选择Refactor并删除未使用的资源。

这个工具太棒了,我对他的开发人员xD的掌声;


1
投票

你可以使用lint

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.

1
投票

是的,日食中的ADT包括android lint

请参阅android lint


1
投票

你可以使用this library 将它放到项目文件夹并从命令行运行jar。


0
投票

关于您想要删除的图像资源,您还应该考虑将所有drawable-xxxx文件夹从项目中移出到一个临时文件夹中,然后进行全部重建,并查看构建消息列表,它将告诉您哪个有些人不见了。

如果您想要了解有效使用哪些资源并使用图标字体或svg资源替换它们,可能需要Android Iconics库的帮助,这可能特别有用。

© www.soinside.com 2019 - 2024. All rights reserved.