How to tackle the R8 optimisation nightmare in a sane way?

Implementing R8 optimizations can always be a nightmare for the dev that has to do it. R8 is a compiler that works together with ProGuard to shrink, minify, and obfuscate your code. It can reduce significantly the APK size by removing classes that are not used at all. It checks the hierarchy of classes and once it sees a class is not being attached to anything, it removes it.

The above, of course, is a double-edged sword as in some cases, it can remove classes that you actually need like request/response classes and many others. But in a recent project, we got like 10 megabytes of reduced APK size (down from 30) which is a huge gain.

The problem comes when you want to tell R8 to keep certain classes that you need either deobfuscated or simply kept within your dex archive. So let’s see what the approach could be in such cases.

Continue reading “How to tackle the R8 optimisation nightmare in a sane way?”