如何在使用eclipse的情况下,清理android项目中的冗余class文件和资源文件以及冗余图片...

在我们迭代项目的过程中,经常会启用某些功能,或者修改某些界面的问题,那么问题来了,这样很容易出现大量的冗余.java文件,冗余资源文件,一些冗余的界面文件等。那么问题既然出现了,那么如何去解决呢,这就是今天着重要去解决的问题?

 

first:

eclipse有个检查冗余java文件的插件,名叫UCDetector:

下载地址为:http://sourceforge.net/projects/ucdetector/files/latest/download?source=files

官网地址:http://www.ucdetector.org/index.html

一些使用方法:下载完后,将下载的jar文件放置在\eclipse\dropins文件夹下面,然后重新启动eclipse即可安装完这个插件。

一下是从其他网站复制的这个工具使用的截图:

阿西吧,不能直接粘贴人家的图片,擦,还是给个链接地址吧:http://www.jb51.net/softjc/123402.html

 

当然你可以根据提示,删除冗余的java文件(推荐这种,比较谨慎点,万一那个类你不想删呢);除了这种,还有另外一种更加炫计的方法:

https://github.com/jasonross/Android-CU(果然github上好多好东东,过多推荐)

一下是readme.md文件中的简介:

CU是clear unused的缩写,本项目主要用来清理Android工程中无用的代码文件和资源文件。

CURes.java用于清理资源文件,借助于ADT SDK自带的lint工具,相对路径为\sdk\tools\lint.bat。

CUSrc.java用于清理.java文件,需要Eclipse插件UCDetector配合。

使用

清除无用文件,需要交替运行CURes.javaCUSrc.java,直到没有可删除文件为止。

运行CURes.java

  • 运行参数为lint.bat文件绝对路径和android工程目录,如 D:adt/sdk/tools/lint.bat D:/nova
  • String[] dirArray为要删除资源文件的相对目录,默认为res目录下。一般来说,values不需要删除,故不添加。
  • 运行结果保存在当前目录下,文件名为格式化后的时间戳。

运行CUSrc.java

  • 设置UCDetector,忽略不需要扫描的文件,如Activity
  • 使用UCDetector扫描项目生成txt报告
  • 运行程序需要两个参数,UCDetector生成的报告路径,项目的路径,如D:/UCDetector/report.txt D:/nova
  • 运行结果会保存在当前目录下的UnusedJava.txt文件中。

注意

  • 清除资源时,如果使用字符串形式调用layout等资源文件,无法被lint识别,会造成误删。
  • 清除代码时,如果使用字符串形式调用fragment等控件或者使用反射时,无法被UCDetector识别,会造成误删。

其实项目中有三个java文件,一个CURes.java,一个CUSrc.java,还有ClearDrawble.java文件

来来来,先看下人家是怎么干的:

 CURes.java

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileWriter;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.InputStreamReader;
  7. import java.text.SimpleDateFormat;
  8. import java.util.Date;
  9. public class CURes {
  10. public static final String separator = File.separator;
  11.   //这里是清理无用res文件的方法,使用到了lint.bat工具
    public static void clearRes(String lintPath, String projectPath, String[] dirArray) {
  12. Process process = null;
  13. InputStream is = null;
  14. InputStreamReader isr = null;
  15. BufferedReader br = null;
  16. FileWriter fw = null;
        //使用到了lint.bat工具检索项目中无用的资源文件  

       String cmd = lintPath + " --check UnusedResources " + projectPath;
  17. int fileCount = 0, singleCount;
  18. long fileSize = 0;
  19. try {
       //生成日志文件,主要记录删除那些无用的资源文件
    SimpleDateFormat formatter = new SimpleDateFormat("CURes-yyyy-MM-dd-HH-mm-ss");
  20. Date curDate = new Date(System.currentTimeMillis());
  21. String dateStr = formatter.format(curDate);
  22. fw = new FileWriter(dateStr + ".txt", true);
  23. Runtime runtime = Runtime.getRuntime();
  24. String line = null;
  25. do {
  26. singleCount = 0;
              //执行lint无用资源文件

      
    process = runtime.exec(cmd);
  27. is = process.getInputStream();
  28. isr = new InputStreamReader(is);
  29. br = new BufferedReader(isr);
  30. while ((line = br.readLine()) != null) {
  31. boolean needDel = false;
  32. for (String dir : dirArray) {
  33. if (line.startsWith("res" + separator + dir)) {
  34. needDel = true;
  35. break;
  36. }
  37. }
  38. if (needDel) {
  39. int index = line.indexOf(":");
  40. if (index > 0) {
  41. String filePath = projectPath + separator + line.substring(0, index);
  42. ++fileCount;
  43. ++singleCount;
  44. File file = new File(filePath);
  45. fileSize += file.length();
  46. //删除无用资源文件的代码
    boolean success = file.delete();
  47. System.out.println(filePath + " " + success);
  48. fw.write(filePath + " " + success + "\n");
  49. fw.flush();
  50. }
  51. }
  52. }
  53. } while (singleCount != 0);
  54. String result = "delete file " + fileCount + ",save space " + fileSize / 1024 + "KB.";
  55. System.out.println(result);
  56. fw.write(result);
  57. fw.flush();
  58. } catch (IOException e) {
  59. e.printStackTrace();
  60. } finally {
  61. if (br != null) {
  62. try {
  63. br.close();
  64. } catch (IOException e) {
  65. e.printStackTrace();
  66. }
  67. }
  68. if (fw != null) {
  69. try {
  70. fw.close();
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. }
  75. if (process != null) {
  76. process.destroy();
  77. }
  78. }
  79. }
  80. public static void main(String[] args) {


  81. //一下三行可以删除,没用
    if (args.length < 2) {
  82. System.out.println("Please config program arguments. Correct arguments contain both absolute path of lint.bat and that of android project.");
  83. return;
  84. }

  85. //删除无用资源文件的的重要代码,请注意,lintPath值得是你的lint.bat文件地址,记得把╲╲换成/,否则会出现问题,projectPath是值得你的android项目地址
      
         //一下args[0] args[1]代码都可以替换为stirng内容的地址   
    String lintPath = args[0];
  86. String projectPath = args[1];
  87. String[] dirArray = { "drawable", "layout", "anim", "color" };
  88. CURes.clearRes(lintPath, projectPath, dirArray);
  89. }
  90. }

  

ok,然后我们看看如何删除无用.java文件的:

  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import java.io.RandomAccessFile;
  6. public class CUSrc {

  7.   //注意此处代码是清理无用java文件的,逻辑分下往下继续走
    private static String clearUnusedJavaFile(String inFileName, String localProjectPath)
  8. throws IOException {
  9. File infile = new File(inFileName);

  10.      //生成无用代码的日志文件
    RandomAccessFile outfile = new RandomAccessFile("UnusedJava.txt", "rw");
  11. int index = -1;


  12.     //分析项目中src文件夹下面的无用资源文件,localProjectPath为android项目的绝对路径   
    String path = localProjectPath + "/src/";
  13. BufferedReader bf = new BufferedReader(new FileReader(infile));
  14. String content = "";
  15. StringBuilder sb = new StringBuilder();
  16. while ((content = bf.readLine()) != null) {
  17. index = content.indexOf(".<init>");
  18. if (index != -1
  19. && (content.indexOf("\tClass") == (content.indexOf(")") + 1) || content.indexOf("\tInterface") == (content.indexOf(")") + 1))) {
  20. String temp = path + content.substring(0, index).replace('.', '/') + ".java";
  21.   

      
      

              //删除冗余代码,并生成日志文件内容
       sb.append(temp).append("\t" + new File(temp).delete()).append(
  22. "\t" + System.currentTimeMillis()).append("\r\n");
  23. }
  24. }
  25. outfile.seek(outfile.length());
  26. outfile.writeBytes(sb.toString());
  27. bf.close();
  28. outfile.close();
  29. return sb.toString();
  30. }
  31. public static void main(String[] args) {
  32. // TODO Auto-generated method stub
  33. if (args.length == 2) {
  34. String inputFile = args[0];
  35. String localProjectPath = args[1];
  36. try {
  37. String str = clearUnusedJavaFile(inputFile, localProjectPath);
  38. System.out.print(str);
  39. } catch (IOException e) {
  40. // TODO Auto-generated catch block
  41. System.out.print("something wrong");
  42. }
  43. } else {
  44. System.out.println("arguments wrong!!");
  45. }
  46. }
  47. }

  

分析完毕,吃饭去喽。

2016年3月31日23:54:35更新

阿里有关apk瘦身的博文,分享链接:

http://www.cnblogs.com/alisecurity/p/5341218.html

发布了162 篇原创文章 · 获赞 51 · 访问量 15万+
曾经_s关注
曾经_s

8篇文章

排名:千里之外

A08110123关注
A08110123

470篇文章

排名:千里之外

highcoder1关注
highcoder1

37篇文章

排名:千里之外

笑一笑没什么大不了关注
笑一笑没什么大不了

91篇文章

排名:千里之外

没有更多推荐了,

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客
客服 举报 举报 返回
顶部