删除Android工程中无用代码和资源

为什么需要删除代码和资源?

  1. 多余的代码很资源会使我们的APP体积变大。
  2. 在代码中申明了一些没有用的的变量,已经new出一个没有使用的类。会造成内存的浪费。

使用什么方法可以实现

首先我们需要使用两个工具:
  • UCDetector (清理多余的代码)
  • AndroidUnusedResources (清理多余的资源文件)

UCDetector 的使用

  1. 首先需要在eclipse中安装这个插件UCDetector插件地址
  2. 安装完成后在eclipse工作空间中右键点击某一个项目会多个UCDetector这个选项
  3. 然后点击UCDetector->Detect Unnecessary Code 这是这个插件开始工作,根据项目的大小需要的时间不等,小伙伴请耐心等待吧!!!
  4. 扫描完成在Problems就会看见如下图所示的内容:
    UCDetector扫描结果显示

  5. 然后点击References双击扫描的结果就会定位到这个类,鼠标放在上面就会出现Delete Code,根据自己的需要进行代码或者变量的删除

AndroidUnusedResources 的使用

  1. 首先下载这个jar包:AndroidUnusedResources下载地址
  2. 将下载好的这个jar包放在需要清理工程的根目录
  3. 然后按住Shift+鼠标右键打开命令行窗体 输入 java -jarAndroidUnusedResources1.6.2 > del.txt 回车,这样这个jar包就开始运行了,这时你会看见你项目的根目录多了一个del.txt的文件。这个文件就是用来保存那些没有用的资源文件的。当扫描完成打开这个del.txt文件会看到如下的格式的内容

    attr: border_inside_color
    E:\workspace1\HBMCB\res\values\attrs.xml

  4. 接下来我们对这个文件里面的数据进行分析,然后写一段程序来帮我们完成这工作量巨大切简单的工作,我自己使用的一段程序,提供给大家参考,有不好的地方希望大家能够谅解

import java.awt.SystemColor;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class UnusedResources {

    private static String title;
    private static String a;

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(
                    new FileReader("data\\del.txt"));
            String line;
            int layoutCount = 0;
            int valuesCount = 0;
            while ((line = reader.readLine()) != null) {
                if (line.contains(":")) {
                    String[] split = line.split(":");
                    title = split[0];
                    a = split[split.length - 1];
                } else {
                    if (line.contains("layout") && line.contains(title)
                            && line.contains(a) && !line.contains("umeng")) {
                        deleteFile(line);
                    } else if (line.contains("values") && line.contains(title)
                            && !line.contains("umeng")) {
                        System.out.println(line);
                        System.out.println(++valuesCount);
                        File file = new File(line);
                        if(file.isFile()&&file.exists()){
                        SAXReader re = new SAXReader();
                        Document document = re.read(file);
                        Element root = document.getRootElement();
                        System.out.println("刪除" + line + "文件中:"+a);
                        removeAttrElement(root, a);
                        writer(document, line);}
                    } else if (line.contains("drawable")
                            && line.contains(title) && line.contains(a)
                            && !line.contains("umeng")) {
                        deleteFile(line);
                    } else if (line.contains("anim") && line.contains(title)
                            && line.contains(a) && !line.contains("umeng")) {
                        deleteFile(line);
                    }
                }
                Thread.sleep(50);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static boolean deleteFile(String fileName) {
        File file1 = new File(fileName);
        System.out.println(file1.getName());
        System.out.println();
        // 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
        if (file1.exists() && file1.isFile()) {
            if (file1.delete()) {
                System.out.println("删除单个文件" + fileName + "成功!");
                return true;
            } else {
                System.out.println("删除单个文件" + fileName + "失败!");
                return false;
            }
        } else {
            System.out.println("删除单个文件失败:" + fileName + "不存在!");
            return false;
        }

    }

    /**
     * 根据属性删除该节点
     * 
     * @param root
     * @param attr
     */
    private static void removeAttrElement(Element root, String attr) {
        for (Iterator it = root.elementIterator(); it.hasNext();) {
            Element element = (Element) it.next();
            for (Iterator it1 = element.attributeIterator(); it1.hasNext();) {
                Attribute attribute = (Attribute) it1.next();
                String text = attribute.getText();
                if (attr.equals(text)) {
                    System.out.println(attr + "成功");
                    root.remove(element);
                    System.out.println(text);
                }

            }
        }
    }

    /**
     * 把document对象写入新的文件
     * 
     * @param document
     * @throws Exception
     */
    public static void writer(Document document, String outFile) {
        XMLWriter writer = null;
        try {

            // 排版缩进的格式
            OutputFormat format = OutputFormat.createPrettyPrint();
            // 设置编码
            format.setEncoding("UTF-8");
            // 创建XMLWriter对象,指定了写出文件及编码格式
            // XMLWriter writer = new XMLWriter(new FileWriter(new
            // File("src//a.xml")),format);
            writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(
                    new File(outFile)), "UTF-8"), format);
            // 写入
            writer.write(document);
            System.out.println("写入文件成功" + outFile);
            // 立即写入
            writer.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭操作
            if (writer != null) {
                try {
                    writer.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

注意事项

  • 先使用UCDetector将多余代码删除,让在使用AndroidUnusedResources删除多余的资源文件。不然代码有可能出现错误
  • 使用UCDetector在删除静态变量的时候需要注意。
  • 建议多次使用AndroidUnusedResources进行项目的扫描,确保删除干净
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值