Clean Contacts injectors are now proguard friendly

  < 1

One of the awesome proguard features is the ability to strip the non-used code that you are packaging to the final apk, is really a nice feature because it removes a lot of size of our final apks and also strips the method number for the dex count (remember the 65k dex method limit)

The problem is when you rely in reflection to do some stuff, in my case when I do a reflection class lookup to get the correct ViewInjector or OutputInjector in CleanContacts. I have an Annotation Processor that is generating some classes, later in the production code you have a call like this: ViewInjector.inject(this); Well, this call is looking for another class with a suffix like “HomePresenter_ViewInjector.class”.

When proguard is running it strips the generated classes because it doesn’t know that we are using via reflection, so we have to flag this classes with a configuration to tell proguard that those classes are really needed in the final code.

The trick I used is to create an Internal Annotation called: “DoNotStrip” and apply this annotation to the generated classes, methods and constructors. Then, we marked those things that we don’t want that proguard strips it, so we have to configure proguard also to don’t strip flagged members in that way: -keepclassmembers class * { @me.panavtec.presentation.common.DoNotStrip *; } With this simple trick our code is obfuscated and that generators are keep during the striping phase.

You can see the whole commit here: Github – Obfuscation commit

Did you like this post? You can support my work and help me writting more useful posts: