一键删除android下面无用资源

项目需求一改再改,UI一调再调,结果就是项目中一堆已经用不到但却没有清理的垃圾资源,不说工程大小问题,对新进入项目的人或看其他模块的代码的人来说,这些没清理的资源可能也可能会带来困扰,所以最好还是清理掉这些垃圾,对于一个稍微大一点的工程来说,手工清理明显是不现实的,这就需要一个方法做这些事情。

本人最怕码字,上面内容引入http://www.cnblogs.com/angeldevil/p/3725358.html

关于android lint的使用,如果不了解的请自行去了解。

下面是我的清除代码,主要就是使用dom 节点解析来删除无用资源,需要引入dom.jar ,建议到这个仓库去下载http://search.maven.org/,速度还可以。代码没有做优化,临时写的。

import java.io.BufferedReader;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.FileReader;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NamedNodeMap;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.w3c.dom.Text;

import org.xml.sax.SAXException;

public class CleanResources {

public static void clearResources(String projectPath, String resultPath)

throws IOException {

BufferedReader reader = new BufferedReader(new FileReader(resultPath));

String line;

int count = 0;

while ((line = reader.readLine()) != null) {

if (line.contains("UnusedResources") && !line.contains("appcompat")

&& !line.contains("res\\values")) {

count++;

int end = line.indexOf(":");

if (end != -1) {

String file = line.substring(0, end);

String f = projectPath + file;

System.out.println(f);

new File(f).delete();

}

}

}

}

public static void doDocCheck(String projectPath, String resultPath)

throws InterruptedException, IOException {

String cmd = "cmd /c lint --check \"UnusedResources\" " + projectPath

+ " >" + resultPath;

Process process = Runtime.getRuntime().exec(cmd);

System.out.println(cmd);

String ls;

BufferedReader bufferedReader = new BufferedReader(

new InputStreamReader(process.getInputStream()));

while ((ls = bufferedReader.readLine()) != null) {

System.out.println(ls);

}

process.waitFor();

}

public static void checkResource(String resultPath, String checkPath)

throws IOException {

BufferedReader reader = new BufferedReader(new InputStreamReader(

new FileInputStream(resultPath), "gbk"));

String line;

StringBuffer sbf = new StringBuffer();

sbf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

sbf.append("<resources>\n");

while ((line = reader.readLine()) != null) {

if (line.contains("<color") || line.contains("<string")) {

sbf.append(line);

if (line.contains("<string-array")) {

sbf.append("</string-array>");

}

sbf.append("\n");

}

}

sbf.append("</resources>");

// System.out.println(sbf.toString());

writeFile(checkPath, sbf.toString());

}

public static void writeFile(String fileAbsultPath, String content)

throws IOException {

OutputStream os = new FileOutputStream(fileAbsultPath);

os.write(content.getBytes());

os.close();

}

public static void prepairClear(String clearPath, String checkPath)

throws ParserConfigurationException, SAXException, IOException {

File fileParent = new File(clearPath);

File checkFile = new File(checkPath);

if (fileParent.isDirectory()) {

for (File file : fileParent.listFiles()) {

clear(file, checkFile);

}

} else {

clear(fileParent, checkFile);

}

}

public static void clear(File clearFile, File checkFile)

throws ParserConfigurationException, SAXException, IOException {

DocumentBuilderFactory builderFactory = DocumentBuilderFactory

.newInstance();

DocumentBuilder builder = builderFactory.newDocumentBuilder();

Document document = builder.parse(clearFile);

Element rootElement = document.getDocumentElement();

NodeList childNodes = rootElement.getChildNodes();

DocumentBuilderFactory builderFactory2 = DocumentBuilderFactory

.newInstance();

DocumentBuilder builder2 = builderFactory2.newDocumentBuilder();

Document document2 = builder2.parse(checkFile);

Element rootElement2 = document2.getDocumentElement();

NodeList childNodes2 = rootElement2.getChildNodes();

for (int i = 0; i < childNodes.getLength(); i++) {

Node childNode = childNodes.item(i);

if (childNode.getNodeType() == Node.ELEMENT_NODE) {

for (int j = 0; j < childNodes2.getLength(); j++) {

Node childNode2 = childNodes2.item(j);

if (childNode2.getNodeType() == Node.ELEMENT_NODE) {

if (childNode

.getAttributes()

.getNamedItem("name")

.getNodeValue()

.equals(childNode2.getAttributes()

.getNamedItem("name").getNodeValue())) {

rootElement.removeChild(childNode);

}

}

}

}

}

StringBuffer sbf = new StringBuffer();

sbf.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

nodeTranserse(rootElement, sbf);

sbf.append("</" + rootElement.getNodeName() + ">");

System.out.println(sbf.toString());

writeFile(clearFile.getAbsolutePath(), sbf.toString());

}

public static void nodeTranserse(Node node, StringBuffer sbf) {

String rNodeName = node.getNodeName();// 当前遍历元素名称

if (node.getNodeType() == Node.ELEMENT_NODE) { // 为节点类型,输出节点名称

sbf.append("<" + rNodeName);

// System.out.print("<" + rNodeName);

if (node.hasAttributes()) {

NamedNodeMap map = node.getAttributes();

int len = map.getLength();

for (int i = 0; i < len; i++) {

sbf.append(" ");

Node attr = map.item(i);

sbf.append(attr.getNodeName() + "=\"" + attr.getNodeValue()

+ "\"");

}

}

sbf.append(">");

// System.out.print(sbf.toString());

}

if (node.getNodeType() == Node.TEXT_NODE) { // 文本类型,输出文本

sbf.append(((Text) node).getWholeText());

// System.out.print(((Text) node).getWholeText());

}

NodeList allNodes = node.getChildNodes();// 获取所要遍历节点的子节点

int size = allNodes.getLength();

if (size > 0) {

for (int j = 0; j < size; j++) {

Node childNode = allNodes.item(j);

nodeTranserse(childNode, sbf);

if (childNode.getNodeType() == Node.ELEMENT_NODE) {

// 每遍历完一个标签,输出结束标签

sbf.append("</" + childNode.getNodeName() + ">");

// System.out.print("</" + childNode.getNodeName() + ">");

}

}

}

}

public static void main(String[] args) throws IOException,

ParserConfigurationException, SAXException, InterruptedException {

// 项目根目录

String projectPath = "E:\\workspace\\Bless\\";

// 检测问题件

String resultPath = projectPath + "result.xml";

// 检测文件比对未使用结果

String checkPath = projectPath + "check.xml";

// 指定删除无用节点values目录

String clearPath = projectPath + "res\\values\\";

// lint 检测 并将结果保存在项目根目录result.xml

doDocCheck(projectPath, resultPath);

// 删除无忧drawable layout anim menu

clearResources(projectPath, resultPath);

// 检测value 目录项未使用的clor string string-array等,并保存在项目根目录check.xml

checkResource(resultPath, checkPath);

// 删除values 目录项无用的节点

prepairClear(clearPath, checkPath);

}

}

大家凑活看吧。

时间: 2024-08-09 14:47:14

一键删除android下面无用资源的相关文章

一个自动清理Android项目无用资源的工具类

此工具在我的github上.地址:https://github.com/NashLegend/AndroidResourceCleaner 很多人都知道androidunusedresources.jar这个工具,它可以把Android项目中无用的资源列出来.然而它所做的也就止于此了,在列出所有的无用资源以后,开发者们还得手动删除这些文件,这实在是一个没技术含量却又烦人的体力活,但是作为程序员,自然是有解决办法的,我们为什么不写一个程序,让程序来实现这个功能呢? 这个功能要实现的功能应该是这样的

lint工具删除无用资源最佳示例

lint删除无用图片最佳示例 1.使用lint命令生成报告 lint --check UnusedResources --html D:\95xiu510_bqt\95xiu5.1.0\mlint_bqt.txt D:\95xiu510_bqt\95xiu5.1.0 执行结果 产生的报告文件 2.处理生成的报告文件 首先,更改后缀名为.html(目的是去除里面的HTML标记) 其次,找到这里,点击展开 最后,复制上面.html文件中的内容到一个新的.txt文件中 3.根据要删除的文件类型设置筛选

快速清除Andorid项目中无用资源

在做项目的时候,随着项目不断迭代,项目包越来越大,同时项目中无用的资源文件或数据越来越多,不仅导致发出的APP包很大,多达20M,还影响程序的性能及产品体验.我们通常做的方法就是自动手工找出或是用一些jar包工具(如androidunusedresources.jar)来找出项目中无用资源,然后手工删除,工作量可想而知,做过这个活的都知道其中的苦. 今天看到了一篇自动删除Android项目中的无用资源的文章:http://nashlegend.blog.51cto.com/5635342/165

Android开发之资源文件存储

本文介绍在Android开发中关于资源文件的存储操作.对于Android资源也是非常重要的,主要包括文本字符串(strings).颜色(colors).数组(arrays).动画(anim).布局(layout).图像和图标(drawable).音频视频(media)和其他应用程序使用的组件. 在Android开发中,资源文件是我们使用频率最高的,无论是string,drawable,还是layout,这些资源都是我们经常使用到的,而且为我们的开发提供了很多方便,不过我们平时接触的资源目录一般都

〖Android〗依据资源信息,Mock Android资源

1 #!/bin/bash - 2 #=============================================================================== 3 # 4 # FILE: mock_res.sh 5 # 6 # USAGE: ./mock_res.sh 7 # 8 # DESCRIPTION: 9 # 10 # OPTIONS: --- 11 # REQUIREMENTS: --- 12 # BUGS: --- 13 # NOTES: ---

android系统cpu资源相关查询

android系统cpu资源相关查询 我们都知道android是基于linux系统内核的,在linux系统中我们查看系统资源消耗情况,一个可以直接通过命令行的top命令来看,里面有cpu具体的使用情况,当然在android系统上也还是保留了的.top还是很有用,那在android应用上怎么表现出来呢? 在[设置]应用中,在开发者选项里面就有一项现成的看cpu使用情况的,具体情况的显示是显示在ui的系统层,而不是activities,是始终高于activities的,保障在任何应用界面里都可以显示

利用命令行删除Android系统自带应用的方法

一般来说,手机厂家都会在手机中内置许多应用,而这些应用是使用一般的应用程序管理无法删除的.当然,现在有一些APP,如360和豌豆荚,在获取了系统的root权限之后是可以删除自带应用的.但是如果我不想让一个app来获取我的root权限呢?有没有方便.快捷的方法呢? 当然有,那就是利用shell命令.当然,首先要安装当前手机的驱动程序,否则无法进行调试. 在如何删除Android系统中的内置应用一文中作者也介绍了利用命令行删除系统应用的方法,但是个人感觉太麻烦了,其实有更简单的方法,只要三步即可:a

android中的资源

一.概括地讲,android中的资源是指非代码部分,比如图片.MP3.字符串.xml文件等.在一个android工程中,和src源文件夹并列的有两个文件夹,分别叫做res和assets,都是用来保存资源文件的. 不同点:1.res中的资源可以通过R资源类直接访问.这种方式比较常用. res中有包含各种子文件夹,对资源进行分类: anim(xml动画文件).drawable(图片),layout(布局文件).menu(菜单).raw(二进制文件).values(常量值).xml(xml文件).2.

python实现人人网留言获取与一键删除

最近有点着迷Python,学习基本语法之后,首先从爬虫开始,看了<使用python登录人人网并发表状态>一文后,很感兴趣,然后又曾经苦于人人网聊天记录删除的繁琐,于是决定写一个聊天记录一键删除的小脚本,好啦,废话不多说: #encoding:utf-8 import urllib2, urllib, cookielib, re, string class spider(): def __init__(self, email, password): self.email = email self