Find unused classes in Android codebase

Photo by Ed Leszczynskl on Unsplash

Problem

Solution

Since Proguard and R8 removed unused classes, I followed the instructions here and generated and generated an usage.txt file

  • Add -printusage to your proguard.pro file
  • Run ./gradlew app:minifyReleaseWithProguard or ./gradlew app:minifyReleaseWithR8

But the usage.txt file is big and I can clearly see some classed that are used, not dead code.

My interpretation is that usage.txt lists both removed and obsfucated classes.

So I came up with the following algorithm

  • Go through usage.txt line by line, filter out noises
  • See if the class name is found in mapping.txt, if it’s not, the class is removed by Proguard/ R8

To make the search for class name in mapping.txt, I stored mapping.txt’s record in a binary tree.

Give M lines of text in usage.txt and N lines of text in mapping.txt (N is pretty big; it’s 214543 lines in my small dating app; M is minuscule compared to N), the time complexity of my algorithm is M * logN

Update Dec 17 2020

Insert and search time complexity would both be O(1) while space complexity would remain only O(N).

Conclusion

Hiring Android engineer shorturl.at/bivJO Clean coder, walker & biker. Hater of inefficiency

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store