Wednesday, April 9, 2014

how to use proguard and guice together

I have been a fan of guice and proguard utilities for a long time. They proved to be useful in many projects I've done so far. The only annoying things about them is that they do not play nice when used together. Guice works in runtime and depends heavily on reflection to do its magic. Proguard runs during app building phase so it removes code that is called reflexively by guice in runtime. There are known workarounds for that. You just need to instruct proguard not to remove classes/methods/annoations that might be needed by guice. You can find example configuration online so it's not so complicated. What always worried me was that such solution prevents proguard from producing smallest possible code.

The solution that I decided to implement and opensource with my brother is deguicifier. It's a small command line tool written in java that takes your guice modules and based on bindings configured there generates code that will bootstrap your app the same way guice would do. That generated code does not use any reflection nor it does not need guice annotations to be present during runtime. It is somewhat similar to wiring code you would write by hand. Thanks to that I'm not forced to use guice related workarounds in my proguard configuration file. I can squeeze my app with proguard as much as possible. If you want to find more details about this great utility look at usage example in readme.