Search code examples
androidandroid-proguard

ProGuard - How to use correctly for Android ? Which classes and libraries to keep?


I am trying to improve my understanding about usage of ProGuard for Android build. Am not very sure what to keep ( not obfuscate ) exactly and what to obfuscate.

Couple of libraries are being used in my App, some of them are listed below

com.android.support:appcompat
com.android.support:design
io.reactivex:rxandroid
io.reactivex:rxjava 
com.jakewharton.rxbinding
com.trello:rxlifecycle

Question 1:

Should I obfuscate above libraries ? they are already open source.. will it not be wise to keep all support libraries using proguard rule:

keep class android.support.** { *; }

Similar way I can do with io.reactivex and jakewharton libraries

Question 2

Other than support and external libraries, I have application specific classes, coded for this application. Is it ok to keep the class names which are mentioned in AndroidManifest.xml and specifically their public members, and let obfuscate rest of the code.

Let me know if I am missing something or my understanding is not correct. just to repeat this is not a question on how to do it technically, but more on what to include and what to exclude for obfuscation/optimization/ shrink.. and more specifically reason behind it ....Definitely it will be espresso tested after build.


Solution

  • Question 1: Should I obfuscate above libraries?

    Yes. If you add -keep <library> rules then the entire library will be included in your APK, which bloats it and might cause problems such as making your app exceed the 64k method limit and require Multidex. It's always a good idea to apply the ProGuard rules provided by the library. You can usually find .pro files in the libraries source code or in websites such as https://github.com/krschultz/android-proguard-snippets.

    Question 2: Is it ok to keep the class names which are mentioned in AndroidManifest.xml and specifically their public members, and let obfuscate rest of the code.

    The Android plugin already does that for you. The plugin scans all classes whose names need to be preserved (e.g. Activities, Services, BroadcastReceivers, Views, etc) and doesn't obfuscate them. This includes everything that you declare in AndroidManifest.xml.