从 Android 项目中删除所有未使用的资源
我想从我的 Android res 目录中删除所有未使用的布局、字符串、绘图、颜色等。是否有任何工具可以为我提供文件列表,并且我可以从存储库中删除不再使用的特定文件中的元素(例如未使用的字符串条目)?
I want to remove all unused layouts, strings, drawables, colors, etc from my Android res directory. Are there any tools that will give me a list of files and I can remove from my repository and elements within specifics files (e.g. unused string entries) that are no longer used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(17)
您可以轻松地从 Android Studio 中搜索未使用的资源。只需按 CtrlAltShifti 并输入“未使用的资源”(不带引号) )。这将执行 lint。运行 lint 命令(以及 IDE 中的其他内容)的超级简单方法。
或者
在 Android Studio 菜单 > 重构 > 删除未使用的资源...
选择您要删除的资源。您可以通过右键单击资源项来排除要保留的资源。
使用进行重构一次性删除所有资源。
更新: 对于 mac,使用 ⌘OptionShifti
You can easily search for unused resources from Android Studio. Just press CtrlAltShifti and type "unused resources" (without quotes). That will execute lint. Super easy way to run lint commands (and other stuff from IDE).
OR
In Android Studio Menu > Refactor > Remove Unused Resources...
Select the resources you want to remove. You can exclude resources you want to keep by right-clicking on the resource item.
Use Do Refactor to remove all Resources at once.
Update: use ⌘OptionShifti for mac
从 ADT 16 开始,您可以使用 Android Lint。这真是一个了不起的工具。
但是,它有一些问题(不知道它们是否已经修复),如果您想删除数百个据称未使用的资源,我建议在资源删除期间多次手动编译项目,以确保 Lint 不会删除需要的东西。
Since ADT 16 you can use Android Lint. It is really amazing tool.
However, it has some issues (don't know if they're already fixed) and if you want to delete hundreds of supposedly unused resources I'd recommend to manually compile project several times during resource removing to be sure that Lint didn't remove something needed.
由于Eclipse中对ADT的支持已经结束,我们必须使用Android Studio。
在 Android Studio 2.0+ 中使用
Refactor >删除未使用的资源...
Since Support for the ADT in Eclipse has ended, we have to use Android Studio.
In Android Studio 2.0+ use
Refactor > Remove Unused Resources...
Android 的 Gradle 构建系统支持“资源缩减”:在构建时自动删除打包应用程序中未使用的资源。除了删除项目中运行时实际不需要的资源之外,这还会删除您所依赖的库中的资源(如果您的应用程序实际上不需要这些资源)。
要启用此功能,请在您的 gradle 文件中添加这一行 ShrinkResources true 。
请在此处查看官方文档,
http://tools.android.com /tech-docs/new-build-system/resource-shrinking
The Gradle build system for Android supports "resource shrinking": the automatic removal of resources that are unused, at build time, in the packaged app. In addition to removing resources in your project that are not actually needed at runtime, this also removes resources from libraries you are depending on if they are not actually needed by your application.
To enable this add the line shrinkResources true in your gradle file.
Check the official documentation here,
http://tools.android.com/tech-docs/new-build-system/resource-shrinking
如果您在运行 lint 时使用多种风格,请小心。根据您选择的风格,Lint 可能会给出错误的未使用资源。
Beware if you are using multiple flavours when running lint. Lint may give false unused resources depending on the flavour you have selected.
我们开源了一个工具,可以根据 lint 输出删除 Android 项目中所有未使用的资源。
可以在这里找到:https://github.com/KeepSafe/android-resource-remover
We open source a tool that removes all the unused resources in your android project based on the lint output.
It can be found here: https://github.com/KeepSafe/android-resource-remover
1-->在
,如下图所示:
**
**
2-->第二种方式
-> Ctrl Alt Shift i 并在 Windows 中输入“未使用的资源”(不带引号)
->对于 Mac 使用 ⌘ Option Shift i
或
-> Mac 版为 cmd + Alt + Shift
1--> In
As Shown in Image Below:
**
**
2--> Second Way
-> Ctrl Alt Shift i and type "unused resources" (without quotes) in Windows
-> use ⌘ Option Shift i for mac
or
-> cmd + Alt + Shift for mac
在 Android Studio 中,
分析 ->按名称运行检查
(Mac 的快捷键为 shift+command+option+i ,Windows/Linux 上的快捷键为 Ctrl + Shift + A)。
并输入“未使用的资源”。
通过这种方式,您可以删除未使用的资源、变量、符号、本地化、库等。
从列表中选择需要检查的项目,然后选择检查范围->确定
In Android Studio,
Analyze -> Run Inspection by Name
(Shortcut For Mac shift+command+option+i , Ctrl + Shift + A on Windows/Linux).
And type 'unused resources'.
This way you can remove unused resources, variables ,symbols ,localization ,libraries ..etc.
Select the desired inspection from the list, then select inspection scope ->OK
在Android Studio 2.0及以上版本中
在菜单中选择“重构”-->单击“删除未使用的资源...”
(或)
也可以使用快捷方式
按Ctlr+Alt+Shift+i
将出现一个对话框,然后输入 unused ,
你会发现有很多选项
选择并删除未使用的资源
In Android Studio 2.0 and above
in menu select Refactor-->click on Remove Unused Resources...
(or)
shortcut also available
Press Ctlr+Alt+Shift+i
one dialog box will apper, then type unused ,
you will find number of options
select and remove unused resources
Android Wear开发者请注意:“删除未使用的资源”会删除您声明功能名称的xml文件(res/values/wear.xml),手机将无法连接到手表。我花了几个小时试图找出我的应用程序中的这个错误。
Attention Android Wear developers: "Remove Unused Resources" will delete the xml file where you declare the capability name (res/values/wear.xml) and the phone won't be able to connect to the watch. I spent hours trying to figure out this bug in my app.
检查 string.xml。
这很简单(至少在我的 Eclipse 版本中)
在 Eclipse for Android 中(我的版本是 v22.6.2-1085508)
现在,当您打开 strings.xml 时,您将看到未使用的字符串突出显示。
您可以修复其他潜在问题。
To check string.xml.
It's easy (at least in my version of Eclipse)
In Eclipse for Android (I have version v22.6.2-1085508)
Now when you open strings.xml, you will see that unused string are highlighted.
You can fix other potential issues.
也许有用
Andround Unused Resources 是一个 Java 应用程序,它将扫描您的项目以查找未使用的资源。未使用的资源不必要地占用空间、增加构建时间并使 IDE 的自动完成列表变得混乱。
要使用它,请确保您的工作目录是 Android 项目的根目录,然后运行:
java -jar AndroidUnusedResources.jar
https://code.google.com/p/android-unused-resources/
Maybe useful
Andround Unused Resources is a Java application that will scan your project for unused resources. Unused resources needlessly take up space, increase the build time, and clutter the IDE's autocomplete list.
To use it, ensure your working directory is the root of your Android project, and run:
java -jar AndroidUnusedResources.jar
https://code.google.com/p/android-unused-resources/
当我们将收缩资源定义为true时,我们还可以定义我们想要保留哪些资源以及不保留哪些资源
我已在名为 keep.xml 的 res/raw 文件夹中添加了 xml 文件,
然后进一步生成单个签名构建并签入 apk 分析器工具,该工具将显示 drawable-xhdpi-v4 具有 messenger_button_send_round_shadow.png 我想
通过执行 以下操作删除它Messenger_* 从可绘制文件夹中的名称Messenger开始的所有文件都将被删除,或者其他方式是我定义了要删除的特定文件
,这样您就可以自己从库中删除文件
您还可以通过 @layout/layout name 删除布局
如果该可绘制对象已被布局使用,等等......
When we define shrink resources true that time we can also define which resources we wanna keep and which don't
I have added xml file in res/raw folder named keep.xml
before going further generate a single signed build and check in apk analyser tool which will show drawable-xhdpi-v4 has messenger_button_send_round_shadow.png which i want to remove for this test
by doing messenger_* all files starting from name messenger in drawable folder will be removed or other way around is i have defines specific file to be removed
so that way you can remove files from library it self
you can also remove layouts by @layout/layout name
if that drawable has been used by layout and so....
shift
双击Windows然后输入“unused”,你会发现一个选项删除未使用的资源
,当你打开这些设置时,AS会自动删除未使用的资源资源。
shift
double click on Windows then type "unused", you will find an optionRemove unused Resources
,also
when you set these settings on, AS will automatically remove unused resources.
这里有非常好的答案,建议好的工具
但是如果您打算删除 png-drawables(或其他图像文件),您还应该考虑将所有drawable-xxxx文件夹从项目中移到临时文件夹,然后全部重建,并查看构建消息列表,它会告诉您缺少哪些内容。
如果您想大致了解正在有效使用的资源,并可能用图标字体或 svg 资源替换它们,这可能特别有用,可能需要借助 Android Iconics 库。
There really excellent answers in here suggesting good tools
But if you are intending to remove png-drawables (or other image files), you should also consider moving all the drawable-xxxx folders out of your project into a temporary folder, then do a rebuild all, and take a look at the build message list which will tell you which ones are missing.
This can be specially useful if you want to get an overview of which resources you are effectively using and maybe replace them with an icon font or svg resources, possibly with the help of the Android Iconics library.
Android Assets Viewer 旨在帮助直观地检查 .apk 中打包的图形(作为可绘制对象)(如果您愿意,您也可以上传 res 文件夹的 zip 文件):
http://www.cellebellum.net/AndroidAssetsViewer/
因此,为了删除未使用的资源,这可能与 Lint 检查配合使用,以帮助确保该有的一切都在那里。它还可以帮助您识别可绘制对象的其他一些潜在问题:错误的桶、错误的图形等。
Android Assets Viewer is designed to help visually inspect the graphics packed (as drawables) within your .apk (you can also just upload a zip file of your res folder if you prefer):
http://www.cellebellum.net/AndroidAssetsViewer/
So for removing unused resources, this might work well in tandem with the Lint checks to help make sure everything that should be there is. It also helps you identify a few other potential problems with your drawables: wrong bucket, wrong graphic, etc.
在 Android Studio 中运行 Lint 并找到所有未使用的资源后,您可以从“检查”选项卡中单击其中之一。它提供了有关该问题的一些详细信息以及解决该问题的一些选项。其中之一是删除所有未使用的资源。选择该选项将删除所有未使用的资源。
After you run Lint in Android Studio and find all the unused resources, you can click on one of them from the Inspection tab. It provides some detail about the issue and a few options to fix it. One of them is Remove All Unused Resources. Selecting that option deletes all the unused resources.