Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between consumer-rules.pro and proguard-rules.pro in Android?

Tags:

I have create a module in Android to use in my main app and there seem to be two of these files consumer-rules.pro and proguard-rules.pro.

I would like to know the folllowing things

  1. Will all of the modules code be obfuscated by the rules of the main modules pro-guard rules even if the module does not specify any rules?
  2. What is the difference between consumer-rules.pro and proguard-rules.pro in Android?
  3. Should i enable minifyEnabled in my module?
  4. I noticed that i can add proguard rules for my module in the main module , so does that mean pro-guard rules in the module are overriden in the main module?

Please advise.

like image 902
Dishonered Avatar asked Mar 26 '20 06:03

Dishonered


People also ask

What is ProGuard Rules Pro in Android?

ProGuard is a tool to help minify, obfuscate, and optimize your code. It is not only especially useful for reducing the overall size of your Android application as well as removing unused classes and methods that contribute towards the intrinsic 64k method limit of Android applications.

Where is ProGuard rules pro?

Android Studio adds the proguard-rules.pro file at the root of the module, so you can also easily add custom ProGuard rules specific to the current module. You can also add ProGuard files to the getDefaultProguardFile directive for all release builds or as part of the productFlavor settings in the build.

How do you write ProGuard rules in Android?

When you create a new project or module using Android Studio, the IDE creates a <module-dir>/proguard-rules.pro file for you to include your own rules. You can also include additional rules from other files by adding them to the proguardFiles property in your module's build. gradle file.

What does Minifyenabled do?

minify is an Android tool that will decrease the size of your application when you go to build it. It's extremely useful as it means smaller apk files! It detects any code or libraries that aren't being used and ignores them from your final apk.


1 Answers

I would try to answer each questions, even though let me know if something doesn't make sense!

1. Will all of the modules code be obfuscated by the rules of the main modules pro-guard rules even if the module does not specify any rules?

Obfuscation doesn't work that way. When you enable minify property in your app module it tries to obfuscate code available from app module as well as it's 3rd party dependencies & your library modules would be considered as 3rd party deps. here, but it doesn't touches any transitive dependency of your 3rd party dependency.


2. What is the difference between consumer-rules.pro and proguard-rules.pro in Android?

proguard-rules.pro is the file where you declare rules to related to proguard for you module & it's dependencies.

consumer-rules.pro is the file where you declare rules that can be applied on your module from consumer (whoever would be using your module/library as dependency, usually used by library devs.)


3. Should i enable minifyEnabled in my module?

I would suggest that you should (Every dev must do on release apk), but make sure everything is working as expected because underlying classes.dex changes after applying minify. It helps reducing output apk size, optimizes code, obfuscate class files and much more...


4. I noticed that i can add proguard rules for my module in the main module , so does that mean pro-guard rules in the module are overriden in the main module?

No, basically library rule gets applied from consumer-rules file from library module itself, so when you declare those rules for library in app module it gets applied the same way from consumer-rules which is basically indication that consumer should use these rules when minifying.

proguard-rules.pro of library is the place where you declare rules for 3rd party dependencies of your library (Which is considered as transitive dependency for your app module/main module) & it doesn't get overridden by app module rules.


I hope that makes sense!

like image 157
Jeel Vankhede Avatar answered Oct 10 '22 00:10

Jeel Vankhede