SoFunction
Updated on 2025-04-10

Android APP slimming (clearing resources not used in the project) Detailed explanation

Clear unused resources in Android projects

The project requirements are changed and the UI is adjusted again and again. As a result, there are a bunch of garbage resources in the project that are not available but have not been cleaned. Not to mention the project size problem, for new people entering the project or those who are looking at the code of other modules, these uncleared resources may also cause trouble, so it is best to clean up these garbage. For a slightly larger project, manual cleaning is obviously unrealistic, which requires a method to do these things.

Clean up resource files

To clean up useless resources, the first thing to do is of course find them. We know that there is a tool in the Android SDK called lint, which can help us view problems in the project. One of the functions is to find useless resources. This step is simple. Directly execute the following commands for the project that needs to be cleaned:

lint --check "UnusedResources" [project_path] >

After executing the above command, all the questions about UnusedResources in the project will be saved. Let's take a look at the content first.

res/values/:202: Warning: The resource .msg_my_friend_category_items appears to be unused [UnusedResources]
^M
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
res/layout/back_up_level_list.xml: Warning: The resource .back_up_level_list appears to be unused [UnusedResources]
res/layout/backup_list.xml: Warning: The resource .backup_list appears to be unused [UnusedResources]
res/layout/backup_listview_item.xml: Warning: The resource .backup_listview_item appears to be unused [UnusedResources]

You can see information such as unused layout and unused values. With this information, the next thing you need to do is analyze this information. Manual analysis is not realistic because this file may be very large. For example, after I execute the above command, the file has 2212 lines. Of course, this kind of thing is left to the computer to solve.

Look carefully at the content in the generated text and you will find that the results are output by line, each problem is a separate line, and the content in each line is also very regular.

file_path[:line]: Warning: info [UnusedResources}

So it is still very convenient to get which file or even which line has problems. When I processed, I only cleaned up the useless files, such as the above res/values/:202, and I didn't care. Let's see how to clear unused resource files.

String projectPath = "***";
BufferedReader reader = new BufferedReader(new FileReader("/home/angeldevil/"));
String line;
int count = 0;
while((line = ()) != null) {
 if (("UnusedResources") && !("res/value") && !("appcompat")) {
  count++;
  int end = (":");
  if (end != -1){
   String file = (0, end);
   String f = projectPath +file;
   (f);
   new File(f).delete();
  }
 }
}

The program is very simple. It only has a few lines of code, which is to read each line of the file and filter out lines that do not need to be processed according to the conditions you need (for example, I only want to clean up anim, drawable and layout, so I filter out the information in the res/value directory, and ignore the information related to appcompat). The string before each line ":" is the file name. It is easy to process after finding the file name. It is deleted directly, or printed out, or write it to a file to confirm whether it is to be deleted again. After writing the result to a file, we can check whether the file has files that are not used but still don't want to delete. If so, the processing method is also very simple. Remove this line or simply make a mark, such as typing # in the previous step, and then read this file and delete the file corresponding to the line that is not marked.

It looks simple, but there are a few things to note:

1. Some layout files may have used them before and used the id in the layout layout in the corresponding Java files. For example, setting onClickListener for controls for certain IDs and referring these IDs in the switch...case of onClick, but in the end, this layout is UnusedResource. However, the references to certain IDs in the layout in the Java code that referenced it have not been cleared yet. Deleting this Layout will report an error. You can choose to clean up the errored Java code because they are actually Dead Code. Or clean up a part of the resource files every time, such as cleaning layout first and then cleaning drawable, for each item, you can also clean up a small part of each item according to the file name rules, such as cleaning only files starting with item_of in res/layout. . .

The analysis seems to be incompletely accurate, or not intelligent enough. For example, if a drawable is only referenced by a layout, and this layout is Unused, lint may not find that the drawable is Unused, which requires us to repeat the previous steps many times until the count is 0.

You can only analyze resource files, that is, files in the res directory. If you want to analyze Java files, other methods are needed. Moreover, it is possible that a resource file is referenced by a Java file, and this Java file is Unused, so this resource file will escape the inspection of lint, so we'd better clean the Java file first and then clean the resource file.

Clean Java files

First of all, we need to find unused files or use tools. I use UCDetector, that is, Unused Code Detector. I won’t talk about the usage method, just Google it.

Installing Eclipse's UCDetector plugin to perform checks on projects, this may take a long time, and I checked it for two hours at that time. . Like lint, the result will be output to a text file, which is also one line for each question, so just line analysis is enough, for example:

com..SampleAdapter.(:18) Class "SampleAdapter" has 0 references SampleAdapter 
com..SampleAdapter.(:56) Change visibility of Member class "" to private - May cause compile errors!  

It can be seen that the detection results contain a lot of information, such as a certain class not being used, a certain method has too much visibility, etc. Similarly, only unused class files are processed now, and nothing else is left.

String reportPath = "**/ucdetector_reports/UCDetectorReport_001.txt";
BufferedReader reader = new BufferedReader(new FileReader(reportPath));
String line;
int count = 0;
while((line = ()) != null) {
 if (("Class") && ("has 0 references") && !("Method")[ && other conditions]) {
  count++;
  int end = (".<init>");
  if (end != -1){
   String className = (0, end);
   (className);
  }
 }
}

You can basically find unused classes through the above code. It is recommended not to delete them directly but output the results, because after the result is output, you will find many files that you do not want to delete, such as:

com..(:31) 
Class "DiscCacheUtil" has 0 references DiscCacheUtil 
 Sergey Tarasevich (nostra13[at]gmail[dot]com)

Files in some class libraries may also be detected. For this reason, just filter them out directly in the if condition. Some of your own files may not be used for the time being but do not want to delete them, just filter them out from the results.

Summarize

There are two steps to clean up resources:

  1. Find unused resources
  2. Clean these resources as needed

passUCDetector and lintBasically, problems related to UnusedResource in the project can be detected. Generally, such as method visibility, a certain method does not use such problems. It is fine if it is not processed. If it is changed to the corresponding file, it will be manually processed. The main thing is that some files or classes are not used. If there is a detection report, just analyze the report. This kind of report generally reports a problem per line and the text of each line is regular (the tool generation must be regular). Just filter out the information we need according to the rules.

Thank you for reading, I hope it can help you. Thank you for your support for this site!