Android lint 删除无用图片文件和配置文件

Android lint  删除无用、冗余的  配置文件和 图片资源   

转载请注明  http://blog.csdn.net/aaawqqq?viewmode=contents


Android项目经过长期的迭代开发  项目当中有大量无用的java类和冗余图片

如果不整理将会导致 apk 包比较大  

审查 清理Java类  使用UCDetector  可以查看我的上篇 博文

http://blog.csdn.net/aaawqqq/article/details/46684441


Android lint 是Android SDK 提供的代码检查工具  主要检查 配置文件 资源文件  发现代码问题

我的使用场景  :

减少Android apk 包大小  

使用Android lint 发现无用图片和xml 文件  通过删除冗余资源   


我在此给大家分享的是工具与技术  

具体的知识 大家通过 其它博文去学习  可以查看文章结尾的参考链接  我在这就不复述了   


1) 工具

1.1) Android lint  在  Android sdk tools  当中   如果可以希望大家能配好环境变量

本文主讲 以命令行形式的删除无效资源的批处理


1.2) 另在eclipse当中有lint插件   

此方法

优点:运行简单  在eclipse的 直接显示 

缺点:需要手动删除  当冗余文件数量多的适合 会很伤脑筋


2) 输入

 打开命令行  使用lint命令  

 如图:





lint --check "UnusedResources" /Users/baozi/Dev/android/android > result.txt

  /Users/baozi/Dev/android/android  > result.txt   

  

  /Users/baozi/Dev/android/android  是 工程的路径  (工程名为  android )

  

  生成的扫描结果将会存放在当前目录下的  result.txt  当中

  如我的目录      /Users/baozi/result.txt



3) 输出文件result.txt

打开文件目录   /Users/baozi/result.txt     





4) 根据结果 批量删除对应的文件


本文重点   当你第一次运行时 发现需要数千资源文件需要删除的时候就会伤脑筋

手工逐条删除 并不符合程序猿三大优秀品质    :      懒惰   没有耐心   骄傲


尝试过使用 vim 删除  发现操作起来也相当麻烦  


大家可以参考下面的代码   使用FIle 获取 result.txt 中的文件信息   调用 File .delete();  方法删除   

	/**
	 * 删除 未使用的冗余资源(图片 xml布局)
	 * 
	 * @param b
	 * 
	 *            false 显示资源列表
	 * 
	 *            true 显示资源列表 并删除资源
	 * 
	 * @throws Exception
	 */
	private static void init(boolean b) throws Exception {

		String encoding = "UTF-8"; // 字符格式
		String projectPath = "/Users/baozi/Dev/shihui/android/";//Android工程所在地址
		String filePath1 = "/Users/baozi";//result的所在路径
		File file = new File(filePath1, "result.txt");//获取result.txt 文件 生成地址
		if (file.isFile() && file.exists()) { // 判断文件是否存在
			InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
			BufferedReader bufferedReader = new BufferedReader(read);
			String line = null;
			while ((line = bufferedReader.readLine()) != null) {
				if (line.contains("UnusedResources") && !line.contains("res/value") && !line.contains("appcompat")
						&& !line.contains("res/xml")) {
					// System.out.println(line);
					int end = line.indexOf(":");
					if (end != -1) {
						String file_end = line.substring(0, end);
						String f = projectPath + file_end;
						System.out.println(f);
						if (b) {
							new File(f).delete();
							System.out.println("删除成功");
						}
					}

				}

			}
			read.close();

		}
	}


projectPath  :   Android工程在硬盘中的位置   

filePath1  :  lint 运行结果   result.txt  所在的位置


方法  参数  传入false  仅打印结果   传入true 打印结果 并删除文件


填入正确的地址  就能批量执行删除未使用的 布局 &  图片 资源   (UnusedResources)  

如果想要删除其它操作  请修改 筛选条件  

if (line.contains("UnusedResources") && !line.contains("res/value") && !line.contains("appcompat")

&& !line.contains("res/xml")



使用心得:  循环使用3-6次 能完成 删除全部未使用的资源  但是有些废弃的模块 存在代码以来关系 需要手工判断删除



附1:

使用eclipse自带的 Android lint 插件 审查代码的方式


使用方式:  

右击工程 → Android Tools → Run Lint: Check for Common Error  

结果会在 Lint Warrings 当中显示  和 看logcat 的方式相同





运行结果:




 附2:  参考博文


http://blog.csdn.net/hudashi/article/details/8333349

//Android Lint 检查规则列表
 //介绍比较清晰


结尾附上神兽

//┏┓   ┏┓
//┏┛┻━━━┛┻┓
//┃       ┃  
//┃   ━   ┃
//┃ ┳┛ ┗┳ ┃
//┃       ┃
//┃   ┻   ┃
//┃       ┃
//┗━┓   ┏━┛
//  ┃   ┃   神兽保佑        
//  ┃   ┃   代码无BUG!
//  ┃   ┗━━━┓
//  ┃       ┣┓
//  ┃       ┏┛
//  ┗┓┓┏━┳┓┏┛
//    ┃┫┫ ┃┫┫
//    ┗┻┛ ┗┻┛

每日精进  希望能帮上你


  • 4
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值