47

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.

Dishonered
  • 8,449
  • 9
  • 37
  • 50

2 Answers2

81

I'll try to answer each question, but 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 the minifyEnabled property in your app module, it tries to obfuscate code available from the app module as well as its 3rd party dependencies, and your library modules would be considered 3rd party deps. But it doesn't touch any transitive dependency of your 3rd party dependencies.


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 related to Proguard for your module and its dependencies.

consumer-rules.pro is the file where you declare rules that can be applied to your module by the consumer (whoever would be using your module/library as a dependency -- typically library devs).


3. Should i enable minifyEnabled in my module?

I would suggest that you do (Every dev should on release APKs), but make sure everything is working as expected because the underlying classes.dex changes after applying minifyEnabled. It helps reduce output APK size, optimizes code, obfuscates 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 rules get applied from the consumer-rules file from the library module itself, so when you declare those rules for the library in the app module, it gets applied the same way from consumer-rules, which is basically an indication that the consumer should use these rules when minifying.

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


I hope that makes sense!

tony19
  • 125,647
  • 18
  • 229
  • 307
Jeel Vankhede
  • 11,592
  • 2
  • 28
  • 58
  • 1
    Thanks for the detailed answer. Regarding the second answer , 1) does that mean the consumer-rules.pro should not be used by the library creator but instead is meant for use by its users? – Dishonered Mar 26 '20 at 08:02
  • Exactly, it's like providing rules others can use while using library. For library development, one can use proguard-rules file to declare rules for library just like we do in app module. – Jeel Vankhede Mar 26 '20 at 08:04
  • When you say transitive deps, so let say App module is (A): A -> B -> C -> D. Here A (usually our app module), will only obfuscate code that are in B, but C -> D wouldn't get touched. They will only be obfuscated because B has proguard-rules for them, so when B is obfuscated (in a per-module basis) it will obfuscate C but D won't get obfuscated and so on and so forth? – Neon Warge Sep 12 '20 at 06:48
  • Suppose I have an Android library, should I set both consumer-rules.pro and proguard-rules.pro for it? As others will use it, shouldn't we use only the consumer-rules.pro? And how does it work if I have to offer the library as a dependency via Jitpack (from AAR file)? Does it somehow exist in POM or somewhere else? Do I have to tell the library-user to add the rules manually? – android developer Apr 04 '21 at 14:19
  • 1
    @androiddeveloper You've multiple questions, so I'll try to answer them one by one. When you develop a library it's best practice to have both proguard files. 'proguard-rules.pro' for library is the file where you declare rules for any 3rd party library you use within your library (Just like we do for app module). When you offer your library for public use via any repo center then consumer-rules.pro file also gets shipped, so that end consumer doesn't have to manually end up writing rules on their own proguard file. – Jeel Vankhede Apr 05 '21 at 09:04
  • @androiddeveloper When you ship your library, basically it depends upon packaging how consumer-rules.pro files are treated. If it's packaged with library itself then you don't need to intimate end consumer about rules, but eventually it's again good have those written somewhere on the documentation of the library so that anyone can refer to. Personally I have no idea about packaging structure for Jitpack publication but I've noticed consumer-rules file gets shipped with maven publication for libraries. – Jeel Vankhede Apr 05 '21 at 09:09
  • @JeelVankhede Jitpack uses the POM files too, so maybe it's the same. You say consumer-rules.pro should be set. Is it ok to set both files to be exactly the same? Is it a common thing to do? – android developer Apr 05 '21 at 10:51
  • 1
    @androiddeveloper No, both files are different. Consider like this, consumer-rules file is used by your library user while proguard-rules file is used by your library itself if your library has third party dependency that requires you to add their rules on your library project. – Jeel Vankhede Apr 05 '21 at 11:44
  • @JeelVankhede OK I got to this point now: https://stackoverflow.com/q/66983761/878126 – android developer Apr 07 '21 at 15:04
  • 1
    @androiddeveloper Thank you for the separate question, let me go through it and see how can I help. :) – Jeel Vankhede Apr 07 '21 at 15:09
  • Can anyone explain or point me to a reference that explains how a library dependent gets the consumer-rules.pro? I created a lib with `android { defaultConfig { consumerProguardFiles 'consumer-rules.pro' } }` and assembleRelease and publish that aar to artifactory. I download that .aar and unzip it and nowhere in the unzip can I find a 'consumer-rules.pro' for a dependent to consume. How does a dependent get this file if it does not appear to be in the .aar? – swooby Mar 28 '22 at 16:23
  • I think a much simpler explanation is `proguard-rules.pro` applies rule to the dependencies you use in your module/library (if you are developing a library). While `consumer-rules.pro` applies rule to your module/library itself as a dependency for other module such as app module. However since app module has `proguard-rules.pro` can't we just put it there thus we can disregard `consumer-rules.pro`. – Bitwise DEVS Jun 13 '22 at 05:31
  • @JeelVankhede so it means that `consumer-rules.pro` is like a default rules you want to apply and be used by all consumer of your library is that correct but they can also just put these rule on their app module `proguard-rules.pro` – Bitwise DEVS Jun 13 '22 at 05:35
1

If you are developing a library/module you can use consumer-rules.pro to put rules to the consumer of your library. One example is when I created a common module that contains Moshi + Retrofit and enabled full mode R8. I need to add certain rules to for it to work in the consumer's project, I could either leave that task to consumer or I can put it in the library's consumer-rules.pro to make sure that every usage or all consumer of my library will not need to worry of adding a such rules on their project when using/consuming my library.

Bitwise DEVS
  • 2,858
  • 4
  • 24
  • 67
  • 1
    Please correct me if I am wrong. consumer-rules is used only when you are serving the library to others so as to define rules which will be common to all the clients of my library. – hardik9850 Dec 21 '22 at 01:31
  • Does the consumer need to do anything to consume the consumer-rules.pro file or does Android Studio/gradle automatically pick that up if present? – Glaucus Aug 02 '23 at 16:41
  • 1
    @hardik9850 correct – Bitwise DEVS Aug 02 '23 at 17:20
  • 1
    @Glaucus as a consumer of library you should not manipulate `consumer-rules.pro` since it is prepared by the creator of library. If you ever need to manipulate the consumer-rules.pro as a consumer let say you add the library locally on your project, then you probably just want to do it directly in your modules' regular proguard rules. – Bitwise DEVS Aug 02 '23 at 17:24
  • @BitwiseDEVS Thanks. My question was more in terms of how to consume it, not about modifying it. So if a library has a consumer-rules.pro, does that automatically get merged into the consuming app's proguard-rules.pro at build time? – Glaucus Aug 02 '23 at 18:46
  • 1
    based on my experience consumer rules is automatically applied – Bitwise DEVS Aug 03 '23 at 01:03