我想删除所有未使用的布局,字符串,绘图,颜色等从我的Android res目录。是否有任何工具可以给我一个文件列表,我可以从我的存储库中删除特定文件中的元素(例如未使用的字符串条目),这些文件不再使用?
当前回答
Android的Gradle构建系统支持“资源收缩”:自动删除未使用的资源,在构建时,在打包的应用程序中。除了删除项目中在运行时实际上不需要的资源外,这也会删除你所依赖的库中的资源,如果它们不是你的应用程序实际需要的话。
要启用此功能,在gradle文件中添加行shrinkResources true。
android {
...
buildTypes {
release {
minifyEnabled true //Important step
shrinkResources true
}
}
}
查看这里的官方文件,
http://tools.android.com/tech-docs/new-build-system/resource-shrinking
其他回答
自从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没有删除所需的东西。
当我们定义收缩资源为真时,我们也可以定义哪些资源我们想保留,哪些不保留 我已经在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使用,那么....
Android Assets Viewer的设计是为了帮助视觉检查图形包装(作为绘图)在你的。apk(你也可以只是上传一个压缩文件你的res文件夹,如果你喜欢):
http://www.cellebellum.net/AndroidAssetsViewer/
因此,对于删除未使用的资源,这可能与Lint检查一起工作,以帮助确保应该在那里的所有东西。它还可以帮助你识别绘图的其他一些潜在问题:错误的桶,错误的图形,等等。
shift双击Windows,然后输入“unused”,你会发现一个选项删除未使用的资源,也
android {
buildTypes {
release {
minifyEnabled true
shrinkResources true
}
}
}
当您设置这些设置时,AS将自动删除未使用的资源。
也许有用 Andround Unused Resources是一个Java应用程序,它会扫描你的项目中未使用的资源。未使用的资源会不必要地占用空间,增加构建时间,并使IDE的自动完成列表变得混乱。
要使用它,确保你的工作目录是你的Android项目的根目录,并运行:
java -jar AndroidUnusedResources.jar
https://code.google.com/p/android-unused-resources/
推荐文章
- 警告:API ' variable . getjavacompile()'已过时,已被' variable . getjavacompileprovider()'取代
- 安装APK时出现错误
- 碎片中的onCreateOptionsMenu
- TextView粗体通过XML文件?
- 如何使线性布局的孩子之间的空间?
- DSL元素android.dataBinding。enabled'已过时,已被'android.buildFeatures.dataBinding'取代
- ConstraintLayout:以编程方式更改约束
- PANIC: AVD系统路径损坏。检查ANDROID_SDK_ROOT值
- 如何生成字符串类型的buildConfigField
- Recyclerview不调用onCreateViewHolder
- Android API 21工具栏填充
- Android L中不支持操作栏导航模式
- 如何在TextView中添加一个子弹符号?
- PreferenceManager getDefaultSharedPreferences在Android Q中已弃用
- 在Android Studio中创建aar文件