As we iterate through the project, we often enable certain features or modify some of the interface's problems, so the problem is that there is a lot of redundancy. java files, redundant resource files, some redundant interface files, etc. So if the problem arises, then how to solve it, this is the focus of today to solve the problem?
First
Eclipse has a plugin to check for redundant Java files called Ucdetector:
is: Http://sourceforge.net/projects/ucdetector/files/latest/download?source=files
Website address: http://www.ucdetector.org/index.html
Some ways to use: Once downloaded, place the downloaded jar file under the \eclipse\dropins folder and then restart Eclipse to install the plugin.
It is used by this tool that is copied from other websites:
First (used):
The second (set and):
Third (restart Eclipse after Setup):
Fifth one (use):
Of course you can follow the prompts to delete redundant Java files (recommend this, more cautious, in case the class you do not want to delete), in addition to this, there is another more dazzling method:
Https://github.com/jasonross/Android-CU (as sure as GitHub good stuff, too much recommended)
Here is an introduction to the Readme.md file:
CU is the abbreviation for clear unused, which is used to clean up useless code files and resource files in Android projects.
cures.java
is used to clean up resource files, with the lint tool that comes with the ADT SDK, with a relative path of \sdk\tools\lint.bat.
CUSrc.java
For cleaning. java files, the Eclipse plugin ucdetector mate is required.
Use
Purging useless files requires alternating runs CURes.java
and CUSrc.java
until no files can be deleted.
Run Cures.java
- Run the parameters for the Lint.bat file absolute path and Android engineering directory, such as
D:adt/sdk/tools/lint.bat D:/nova
.
String[] dirArray
To delete the relative directory of the resource file, the default is the Res directory. Generally, there values
is no need to delete, so do not add.
- The run results are saved in the current directory, and the file name is the formatted timestamp.
Run Cusrc.java
- Set Ucdetector, ignoring files that do not need to be scanned, such as activity
- Generate a TXT report using a UCDETECTOR scan project
- Running the program requires two parameters, ucdetector the generated report path, the project path, such as
D:/UCDetector/report.txt D:/nova
- The run results are saved in the UnusedJava.txt file in the current directory.
Attention
- When you clear a resource, you cannot be recognized by lint if you use a string to invoke a resource file such as layout, which can cause accidental deletion. When the
- clears the code, if a control such as fragment is called using a string, or when reflection is used, it cannot be recognized by ucdetector, which results in a false deletion.
In fact, there are three Java files in the project, one Cures.java, one Cusrc.java, and cleardrawble.java files .
Come and see how others do it:
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;//Here is a way to clean up the useless res file, using the Lint.bat tool
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;
//Use the Lint.bat tool to retrieve useless resource files in the project
String cmd = Lintpath + "--check unusedresources" + ProjectPath; int filecount = 0, singlecount; Long fileSize = 0; try {
//Generate log files, major records Delete those useless resource files
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;
//Execute Lint useless resource file
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 ();//Delete the code for the unused resource file
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) {
Three lines can be deleted, no use
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 }
Delete the important code of the useless resource file, please note that Lintpath is worth your Lint.bat file address, remember to change ╲╲/, otherwise there will be problems, ProjectPath is worth your Android project address
ARGS[0] args[1] code can be replaced with the address of the STIRNG content
String Lintpath = args[0]; String ProjectPath = args[1]; String[] Dirarray = {"Drawable", "Layout", "Anim", "Color"}; Cures.clearres (Lintpath, ProjectPath, Dirarray); }}
OK, then let's see how to delete useless. java files:
Import Java.io.bufferedreader;import java.io.file;import Java.io.filereader;import Java.io.ioexception;import Java.io.randomaccessfile;public class Cusrc {
//Note Here The code is to clean up useless Java files, the logical sub down to continue to go
private static string Clearunusedjavafile (String infilename, String localprojectpath) throws IOException { File infile = new file (infilename);
//Generate log files for useless code
Randomaccessfile outfile = new Randomaccessfile ("UnusedJava.txt", "RW"); int index =-1;
//Analyze the unused resource file under the SRC folder in the project, Localprojectpath is the absolute path of the Android project
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 ';
//Remove redundant code and generate log file contents
Sb.append (temp). Append ("\ t" + new File (temp). Delete ()). Append ("\ T" + system.currenttimemillis ()). Appen D ("\ 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 Wro Ng "); }} else {System.out.println ("arguments wrong!! "); } }}
Analysis finished, eat to go.
How to clean up redundant class files and resource files and redundant pictures in Android projects with Eclipse