Android应用清除无用资源

2017-06-15  本文已影响0人  冯文丹

随着项目的迭代,很多以前用到的资源现在被废弃,变成了无用资源。这些资源,尤其是图片资源,会使安装包apk变得很大。

无用资源清理在apk优化中,效果最明显的,同时也是最简单的。安装包减小4-5M不是梦。

工具

  1. Android Lint
  2. Google官方提供的 android-unused-resources工具(点击下载)

使用方法

Android Lint Eclipse和Android Studio都自带,不做过多介绍,这里主要说说第二种工具。
之所以找到第二种工具,是因为我们这个历史悠久的项目是基于eclipse的,并且坑爹的是,由于项目自身的原因,lint一直报错。

经试验android-unused-resource工具好像不能用在Android Studio项目中。

检测过程特别简单:

  1. AndroidUnusedResources1.6.2.jar 放到需要清理的android项目的根目录下
  2. 命令行切换到该目录下 cd xxx
  3. 执行jar
    1. java -jar AndroidUnusedResources1.6.2.jar
    2. 或者导出结果到文件 java -jar AndroidUnusedResources1.6.2.jar > unused_resource.txt
  4. 耐心等待几分钟(时间视项目大小、电脑配置而定),得到分析结果

结果示例

各种资源都有可能是无用的,赶紧开始打扫吧~

Running in: E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo
156 resources found


Not generating usage matrices. If you would like them, create a directory named 'resource-matrices' in the base of your project.

104 unused resources were found:
attr  : ptrAdapterViewBackground
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\attrs.xml
attr  : ptrDrawable
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\attrs.xml

...

color : activity_blue
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\colors.xml
color : activity_gray
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\colors.xml
color : activity_green
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\colors.xml
 

dimen : about_string_margin_top
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values-sw600dp\dimens.xml
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\dimens.xml
dimen : activity_attend_size_padding_left
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values-sw600dp\dimens.xml
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\dimens.xml
dimen : activity_attend_size_padding_right
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values-sw600dp\dimens.xml
E:\workProjects\myDemo\client\client2.0\MyDemo.0\src\Demo\res\values\dimens.xml
drawable  : ic_cancel_ash_disable
D:\client\client2.0\MyDemo.0\src\Demo\res\drawable-xhdpi\ic_cancel_ash_disable.png
drawable  : ic_collection
D:\client\client2.0\MyDemo.0\src\Demo\res\drawable-hdpi\ic_collection.png
D:\client\client2.0\MyDemo.0\src\Demo\res\drawable-xhdpi\ic_collection.png
D:\client\client2.0\MyDemo.0\src\Demo\res\drawable-xxhdpi\ic_collection.png
...

id: all_circle_content_view
D:\client\client2.0\MyDemo.0\src\Demo\res\layout\Demo_all_circles_fragment.xml
id: all_tool_bar
D:\client\client2.0\MyDemo.0\src\Demo\res\layout\Demo_all_circles_fragment.xml

...

layout: Demo_circle_banner_image_layout
D:\client\client2.0\MyDemo.0\src\Demo\res\layout\Demo_circle_banner_image_layout.xml
layout: Demo_recommend_circle_item_layot
D:\client\client2.0\MyDemo.0\src\Demo\res\layout\Demo_recommend_circle_item_layot.xml

...

menu  : action_bar
D:\client\client2.0\MyDemo.0\src\Demo\res\menu\action_bar.xml
menu  : action_bar_search
D:\client\client2.0\MyDemo.0\src\Demo\res\menu\action_bar_search.xml
...

string: circle_to_manage
D:\client\client2.0\MyDemo.0\src\Demo\res\values-de\strings.xml
D:\client\client2.0\MyDemo.0\src\Demo\res\values-en\strings.xml
D:\client\client2.0\MyDemo.0\src\Demo\res\values-es\strings.xml

...

style : Theme_MusicDefault
D:\client\client2.0\MyDemo.0\src\Demo\res\values\styles.xml
style : divider_black_15
D:\client\client2.0\MyDemo.0\src\Demo\res\values\styles.xml

...

styleable : DividerLineGridView_android_footerDividersEnabled
styleable : DividerLineGridView_android_headerDividersEnabled
styleable : FastInGridView
D:\client\client2.0\MyDemo.0\src\Demo\res\values\attrs.xml
上一篇下一篇

猜你喜欢

热点阅读