2014-03-15

[Android] Find unused resources in a project

Most of the time we may need to delete unnecessary images or other resource files before we deploy our project. Searching them one by one and deleting them will be a pain.So here is a simple procedure to find them automatically using Android Lint and remove them by a shell script.

1. Find resources

You can run a Lint check from the IDE you are using. But if your project has a pile of lint warnings it may seem impossible to find the unused resources. Therefore we need to run it using console.

First you have to go to the android sdk tools directory using  :

cd “path to android sdk tools directory”

Ex:

Then check whether the lint is working properly by running this command:



If you experience an exception similar to “exception in thread "main" java.lang.NoClassDefFoundError: lombok/ast/AstVisitor”
go to  \android-sdk-windows\tools\lib and rename lombok-ast-0.2.2.jar (or with a different version) to new file name “lombok-ast.jar”
Try again and it should show the version of Lint

Now check for unused resources by

lint --check UnusedResources  --fullpath “path to project folder (where AndroidManifest.xml is)” > resources.txt

Ex:

It will take some time depending on the number of project files. After that a file named “resources.txt” will generate in the console working directory. (If you run Lint from \android-sdk-windows\tools , the file will be located there)

2. Remove unused resources

Open that “resources.txt” file and go through the unused file list and delete resource files one by one. If you feel lazy for that execute following java file/ jar file and generate batch/shell and run it. (I did that to automate process)

To execute jar file run this on console
OS_ID : 0=windows(generate unused.bat) and 1=linux(generate unused.sh)
lint_check_resource_output_file : resources.txt file

Ex:
java –jar lintreader.jar 0 resources.txt

This will generate unused.bat or unused.sh files depending on the OS_ID you provide.

Execute this generated file in the console and unused android project resources will be deleted automatically.

To execute batch file in windows simply type in command prompt
To execute shell script in linux simply type in console