我想删除所有未使用的布局,字符串,绘图,颜色等从我的Android res目录。是否有任何工具可以给我一个文件列表,我可以从我的存储库中删除特定文件中的元素(例如未使用的字符串条目),这些文件不再使用?


当前回答

这里有非常棒的答案,提供了很好的工具

但如果你打算删除png-drawables(或其他图像文件),你也应该考虑将所有drawable-xxxx文件夹从你的项目中移到一个临时文件夹中,然后重新构建所有文件夹,并查看构建消息列表,它会告诉你哪些文件夹丢失了。

如果你想要获得你正在有效使用的资源的概述,并可能用图标字体或svg资源替换它们,这可能是Android Iconics库的帮助,这可能是特别有用的。

其他回答

当我们定义收缩资源为真时,我们也可以定义哪些资源我们想保留,哪些不保留 我已经在res/raw文件夹中添加了xml文件,名为keep.xml

在进一步生成单签名构建和检入apk分析器工具之前,该工具将显示drawable-xhdpi-v4有messenger_button_send_round_shadow.png,我想在这次测试中删除它

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
       tools:shrinkMode="strict"
       tools:discard="@drawable/com_facebook_button_icon_blue.png,
       @drawable/com_facebook_button_icon_white.png,
       @drawable/com_facebook_button_like_icon_selected.png,
       @drawable/messenger_button_send_round_shadow.png,
       @drawable/messenger_*"  />

通过执行messenger_*所有文件从名称messenger在可绘制文件夹将被删除或其他方式是我已经定义了特定的文件被删除

这样你就可以自己从库中删除文件 你也可以通过@layout/布局名来删除布局 如果该drawable已被layout使用,那么....

shift双击Windows,然后输入“unused”,你会发现一个选项删除未使用的资源,也

 android {
        buildTypes {
            release {
                minifyEnabled true
                shrinkResources true
            }
   }
}

当您设置这些设置时,AS将自动删除未使用的资源。

1 - - - >

Android Studio菜单> Refactor >删除未使用的资源

如下图:

**

OR

**

2——>第二种方式

->按Ctrl Alt Shift i,在Windows中输入“unused resources”(不带引号)

->使用⌘Option Shift i for mac

or

-> cmd + Alt + Shift for mac

由于Eclipse中对ADT的支持已经结束,我们不得不使用Android Studio。

在Android Studio 2.0+使用Refactor >删除未使用的资源…

自从ADT 16以来,你可以使用Android 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.

然而,它有一些问题(不知道它们是否已经修复),如果你想删除数百个据称未使用的资源,我建议在资源删除期间手动编译项目几次,以确保Lint没有删除所需的东西。