Detailed explanation of Android App slimming (removing resources not used in the project)

Clear unused resources in Android project

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

Clean up resource files

To clean up useless resources, the first thing is to find them. We know that there is a tool called lint in the anroid SDK that can help us check the problems existing in the project. One of its functions is to find unused resources. This step is simple. Directly execute the following commands for the projects to be cleaned up:

lint --check "UnusedResources" [project_path] > result. txt

After executing the above command, all the unusedresources problems in the project will be saved to result Txt, let's take a look at result Txt

You can see that unused layouts and unused values are listed. With this information, the next thing to do is to analyze it. Manual analysis is not realistic, because the 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.

If you look carefully at the content of the generated text, 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 I can easily get which file or even which line has problems. When I deal with it, I only clean up useless files, such as RES / values / arrays XML: 202 doesn't matter. Let's see how to clear unused resource files.

The program is very simple, just a few lines of code, that is, reading result For each line of TXT file, filter out the lines that do not need to be processed according to their own conditions (for example, I just want to clean up anim, drawable and layout, so I filter out the information in RES / value directory and ignore the information related to appcompat). The string before ":" in each line is the file name. Once the file name is found, it can be processed, deleted or printed directly, Or write it to a file to confirm whether to delete it again. After writing the results to a file, we can check whether there are files in the file that are not used but still do not want to delete. If so, the processing method is also very simple. Remove this line or simply mark it, such as marking #, Then read the file and delete the file corresponding to the unmarked line.

It looks simple, but there are several points to note:

1. For some layout files, you may have used them before and used the ID in the layout in the corresponding java file. For example, onclicklistener is set for some ID controls, and onclick switch These IDs are referenced in the case, but the layout is not used in the end. At this time, the layout is unusedresource, but the references to some IDS in the layout in the Java code that previously referenced it have not been cleared. At this time, deleting the layout will report an error. You can choose to clean up the Java code that reported an error, because they are actually dead code. Or clean up some resource files at a time, such as layout and drawable. For each item, you can also clean up a small part at a time according to the rules of file name, such as only cleaning up items in RES / layout_ Of file...

2. Lint's analysis seems not completely accurate or intelligent. For example, a drawable is referenced by only one layout, and the layout is unused. Lint may not find that the drawable is unused, so we need to repeat the previous steps for many times until the count is 0.

3. Lint can only analyze resource files, that is, files in the res directory. If you want to analyze java files, you need other methods. In addition, it is possible that a resource file is referenced by a java file and the java file is unused. In this way, the resource file will escape lint's inspection, so we'd better clean up the java file first and then the resource file.

Clean up java files

First of all, you should find the unused files or use tools. I use ucdetector, that is, unused code detector. I won't say how to use it. Just google it directly.

It may take a long time to install the ucdetector plug-in of eclipse and check the project. I checked it for two hours.. Like lint, the results will be output to a text file, one line for each problem, so only line analysis is needed, such as:

It can be seen that the test results contain a lot of information, such as a class is not used, the visibility of a method is too large, etc. similarly, now only unused class files are processed, regardless of others.

Basically, unused classes can be found through the above code. It is recommended not to delete them directly, but to output the results, because after the results are output, you will find many files you don't want to delete, such as:

Files in some class libraries may also be detected. For this, it is good to filter them directly in the if condition. It is also possible that some of your own files are not used temporarily but do not want to be deleted. It is good to filter them from the results.

summary

There are two steps to clean up resources:

Basically, problems related to unusedresource in the project can be detected through ucdetector and lint. Generally, such problems as method visibility are not used in a method. It's OK not to deal with them. When changing to the corresponding file, it's OK to deal with them manually. The main thing to deal with 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 one problem per line, and the text of each line is regular (the tool must be generated regularly). Just filter out the information we need according to the law

Thank you for reading, hope to help you, thank you for your support to this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>