在我们迭代项目的过程中,经常会启用某些功能,或者修改某些界面的问题,那么问题来了,这样很容易出现大量的冗余.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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date; public class CURes { public static final String separator = File.separator;   //这里是清理无用res文件的方法,使用到了lint.bat工具
public static void clearRes(String lintPath, String projectPath, String[] dirArray) {
Process process = null;
InputStream is = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileWriter fw = null;
    //使用到了lint.bat工具检索项目中无用的资源文件      String cmd = lintPath + " --check UnusedResources " + projectPath;
int fileCount = 0, singleCount;
long fileSize = 0; try {
   //生成日志文件,主要记录删除那些无用的资源文件
SimpleDateFormat formatter = new SimpleDateFormat("CURes-yyyy-MM-dd-HH-mm-ss");
Date curDate = new Date(System.currentTimeMillis());
String dateStr = formatter.format(curDate);
fw = new FileWriter(dateStr + ".txt", true); Runtime runtime = Runtime.getRuntime();
String line = null; do {
singleCount = 0;
          //执行lint无用资源文件   
process = runtime.exec(cmd);
is = process.getInputStream();
isr = new InputStreamReader(is);
br = new BufferedReader(isr); while ((line = br.readLine()) != null) {
boolean needDel = false;
for (String dir : dirArray) {
if (line.startsWith("res" + separator + dir)) {
needDel = true;
break;
}
}
if (needDel) {
int index = line.indexOf(":");
if (index > 0) {
String filePath = projectPath + separator + line.substring(0, index);
++fileCount;
++singleCount;
File file = new File(filePath);
fileSize += file.length();
//删除无用资源文件的代码

boolean success = file.delete();
System.out.println(filePath + " " + success);
fw.write(filePath + " " + success + "\n");
fw.flush();
}
}
} } while (singleCount != 0); String result = "delete file " + fileCount + ",save space " + fileSize / 1024 + "KB.";
System.out.println(result);
fw.write(result);
fw.flush(); } catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fw != null) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (process != null) {
process.destroy();
}
}
} public static void main(String[] args) { //一下三行可以删除,没用
if (args.length < 2) {
System.out.println("Please config program arguments. Correct arguments contain both absolute path of lint.bat and that of android project.");
return;
}

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

  

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.RandomAccessFile; public class CUSrc {

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

     //生成无用代码的日志文件
RandomAccessFile outfile = new RandomAccessFile("UnusedJava.txt", "rw");
int index = -1;     //分析项目中src文件夹下面的无用资源文件,localProjectPath为android项目的绝对路径   
String path = localProjectPath + "/src/";
BufferedReader bf = new BufferedReader(new FileReader(infile));
String content = "";
StringBuilder sb = new StringBuilder(); while ((content = bf.readLine()) != null) { index = content.indexOf(".<init>");
if (index != -1
&& (content.indexOf("\tClass") == (content.indexOf(")") + 1) || content.indexOf("\tInterface") == (content.indexOf(")") + 1))) {
String temp = path + content.substring(0, index).replace('.', '/') + ".java";
     
             //删除冗余代码,并生成日志文件内容
   sb.append(temp).append("\t" + new File(temp).delete()).append(
"\t" + System.currentTimeMillis()).append("\r\n");
} }
outfile.seek(outfile.length());
outfile.writeBytes(sb.toString());
bf.close();
outfile.close(); return sb.toString();
} public static void main(String[] args) {
// TODO Auto-generated method stub
if (args.length == 2) {
String inputFile = args[0];
String localProjectPath = args[1];
try {
String str = clearUnusedJavaFile(inputFile, localProjectPath);
System.out.print(str);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.print("something wrong");
} } else {
System.out.println("arguments wrong!!");
} } }

  

分析完毕,吃饭去喽。

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

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

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

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

  1. [VS] - "包含了重复的“Content”项。.NET SDK 默认情况下包括你项目中的“Content”项。

    copy to :http://www.cnblogs.com/jinzesudawei/p/7376916.html VS 2017 升级至  VS 2017 v15.3 后,.Net Core 1 ...

  2. maven仓库失效的情况下搭建maven项目

    maven仓库失效的情况下搭建maven项目 1,在有maven仓库的情况下mvn clean package 2,整个项目拷贝到没有的环境下 3,ls |xargs -t -I a cp a/pom ...

  3. android XMl 解析神奇xstream 一: 解析android项目中 asset 文件夹 下的 aa.xml 文件

    简介 XStream 是一个开源项目,一套简单实用的类库,用于序列化对象与 XML 对象之间的相互转换. 将 XML 文件内容解析为一个对象或将一个对象序列化为 XML 文件. 1.下载工具 xstr ...

  4. [转]eclipse下编写android程序突然不会自动生成R.java文件和包的解决办法

    原网址 : http://www.cnblogs.com/zdz8207/archive/2012/11/30/eclipse-android-adt-update.html 网上解决方法主要有这几种 ...

  5. 什么情况下才要重写Objective-C中的description方法

    特别注意: 千万不要在description方法中同时使用%@和self,同时使用了%@和self,代表要调用self的description方法,因此最终会导致程序陷入死循环,循环调用descrip ...

  6. 存在网路的情况下重命名SDE中数据图层错误(The orphan junction feature class cannot be renamed)

    运行环境为ArcGIS9.3,VS2008. 问题描述:数据通过SDE存储在Oracle10g数据库中,数据集中存在几何网络,在存在网络的情况下通过程序对其中的数据图层进行重命名,弹出"Th ...

  7. 怎样在不对控件类型进行硬编码的情况下在 C#vs 中动态添加控件

    文章ID: 815780 最近更新: 2004-1-12 这篇文章中的信息适用于: Microsoft Visual C# .NET 2003 标准版 Microsoft Visual C# .NET ...

  8. Android——eclipse下运行android项目报错 Conversion to Dalvik format failed with error 1解决

    在eclipse中导入android项目,项目正常没有任何错误,但是运行时候会报错,(clean什么的都没用了.....)如图: 百度大神大多说是jdk的问题,解决: 右键项目-Properties如 ...

  9. eclipse下建立 android 项目,相关文件夹介绍

    今天开始进入ANDROID开发,之前一直做些JAVA的WEBSERVICE之类的文件,第一次从头开始整理ANDROID项目,我会把最近遇到的问题做一一梳理. 现在来说一下建立ANDROID项目后产生的 ...

随机推荐

  1. 怎样正确设置remote_addr和x_forwarded_for

    怎样正确设置remote_addr和x_forwarded_for 2013-04-20 做网站时经常会用到remote_addr和x_forwarded_for这两个头信息来获取客户端的IP,然而当 ...

  2. 请教下关于CKEditor富文本编辑框设置字体颜色的问题

    CKEDITOR.editorConfig = function( config ){ config.plugins = 'about,a11yhelp,basicstyles,bidi,blockq ...

  3. 腾讯微博OAuthV2认证实现第三方登录

    1)添加相关jar包: httpmime.jar 以下信息是必须的 //!!!请根据您的实际情况修改!!! 认证成功后浏览器会被重定向到这个url中 必须与注册时填写的一致 private Strin ...

  4. python字符串中的中文处理

    python字符串中的字符串默认并非是unicode,如果在字符创中使用Unicode字符,如中文字符,必须要经过转换, 方式1: text = u"中文" 方式2: text = ...

  5. APP测试中的头疼脑热:测试人员如何驱动开发做好自测

    如今,随着移动互联网的浪潮越翻越涌,移动APP测试工作的现状已经成了那本"家家难念"的经.不管公司大小,不管测试哪种类型的APP,让广泛测试者苦不堪言的就属重复性最多,测试工作量最 ...

  6. linux中tar 打包指定路径文件

    linux中tar打包指定路径文件www.111cn.net 编辑:yahoo 来源:转载在linux系统中打包与解压文件我都可以使用tar命令来解决,只要使用不同的参数就可以实现不同的需要了,下面来 ...

  7. hdu_3182_Hamburger Magi(状压DP)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3182 题意:有n个汉堡,做每个汉堡需要消耗一定的能量,每个汉堡对应一定的价值,且只能做一次,并且做当前 ...

  8. debian上安装lua编辑器

    Debian服务器上安装lua 1)下载压缩包 wget http://www.lua.org/ftp/lua-5.1.4.tar.gz 2)解压文件 tar  zxvf lua-5.1.4.tar. ...

  9. C# WebBrowser函数互相调用

    在使用C#开发winform程序过程中,我们经常会碰到嵌入了一个WebBrowser的浏览器控件.很多时候,我们需要在程序里控制网页的显示方式,或者调用网页当中的某个JS函数,反过来,也有可能网页也需 ...

  10. List列表 OrderBy

    一个条件排序情况 list.OrderBy(item => tem.State); 多个条件的情况下 list.OrderBy(item => new {item.State, item. ...