Android(安卓)lint 自动检测并删除无用资源
Android lint 自动检测并删除无用资源
最近做项目优化,其中一项是减少apk包大小,使用到Android 提供的 Lint 检查工具。安装包从原来的 8M 减小大现在的 5M 左右,主要就是一些不再使用资源文件,layout ,drawable 下的图片,asset 里面的资源。先说优化过程,后面再解释原理。
环境配置:
1.Android Studio 1.4
2.lint 24.3.4
3.android-resource-remover
检测过程
1.配置build 文件
// android 选项 下面 添加lint 检测设置lintOptions { //build release 版本 时 开启lint 检测 checkReleaseBuilds true //lint 遇到 error 时继续 构建 abortOnError false}
2.lint检测
在 Android Studio 终端选项下 执行 命令
gradle lint
在 yoru_project_dirctory/build/outputs/ 会生成 两个文件 lint-result.xml, lint-result.html 和文件夹 lint-result-files. 最重要的是 lint-result.xml 文件,里面包含了我们要解析的信息,包含项目中不再使用的资源文件信息。
3.执行 命令
android-resource-remover --xml lint-result.xml
android-resource-remover 安装和使用, 请查考github 上的说明。
执行完这个命令,项目中不再使用的资源文件,包含 string ,color ,value等,全都被删除掉,是不是感觉超级方便。
一般的项目,这样使用都是没问题的,但是如果你的项目中第三方jar ,jar中有使用到资源文件,这些文件也会被一起删除。有没有办法解决这个问题呢 ? 当然有,方法如下。
找到 在jar 中 调用的layout 文件 在根目录 下添加如下属性
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="wrap_content" <--最关键的一条属性--> tools:ignore="all"><ImageView android:id="@+id/ixintui_custom_notif_icon" android:layout_width="48dp" android:layout_height="48dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:layout_marginTop="8dp" android:layout_marginBottom="8dp" android:contentDescription="@android:string/unknownName"/><TextView android:id="@+id/ixintui_custom_notif_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toLeftOf="@+id/ixintui_custom_notif_time" android:layout_toRightOf="@+id/ixintui_custom_notif_icon" android:layout_marginTop="10dp" android:layout_marginLeft="4dp" android:textAppearance="@android:style/TextAppearance.Medium" android:singleLine="true" android:ellipsize="marquee" android:fadingEdge="horizontal" tools:ignore="all" /></RelativeLayout>
最重要的一个属性 tools:ignore = “all” 其中 all 也可以是其他的lint check id ,最常用的还有 “UnUsedResource” 。
参考资料
lint guard
android-resource-remover
Android lint 删除无用图片文件和配置文件
更多相关文章
- 使用Jenkins自动打包android
- Android内存泄漏检测工具使用手册
- Android-内存映射mmap
- dex 转为 jar
- Android(安卓)ndk移植c库libpng
- android的ndk开发入门示例
- Android解决TextView setText显示乱码
- Android(安卓)studio--Android开发添加外来字体库
- APICloud SuperWebView融合Android原生平台