Run Proguard on multi-module project as “one piece

2019-04-25 12:04发布

TLDR: How to pass proguard mapping to javac to compile against obfuscated library ?

That's long, but I don't see how to make it shorter:

Background: I have the following product setup:

Android Studio project - Library module - (sub)module Core - (sub)module A - (sub)module B - (sub)module C - Sample App module - ... Other modules

Each of library submodules A, B, C reference classes in Core, but A, B, C independent among themselves. Conceptually similar to Play Services where user can only have code and required sub-module. Each of the library submodules has external APIs but also many internal classes The goal is to be able to distribute Core, A, B, C as independent aar-s.

Goal: obfuscate all sub-modules together leaving only public APIs exposed but package and distribute them separately in obfuscated/optimized form.

Issue: I do not see how to use it with straightforward gradle configuration. Custom build system is needed here, Unless there is a any known solution?

What is needed here is:

  • Java-Compile all sub-modules together
  • Proguard-obfuscate them together
  • Run all the rest of the build process separately

Where is normal build process is for each module is: - Compile -> do everything incl. obfuscation -> package

Straight forward setup fails at the compilation of the first submodule after Core. If it only would be possible to pass Proguard mapping.txt as an input to Java compiler... But I could not find such an option. Any ideas?

Thanks!!!

2条回答
放荡不羁爱自由
2楼-- · 2019-04-25 12:12

Issue: I do not see how to use it with straightforward gradle configuration. Custom build system is needed here, Unless there is a any known solution?

So, you're looking for a "custom build system" since you don't know how to do it in gradle. But if this could be solved in gradle, you'd like to use it, right?

To proguarding libraries with gradle, you have 2 options:

  • Run Proguard for each library independently: In each module, set minifyEnabled true, and use proguardFiles function to set your proguard.txt file.
  • Run Proguard just in your app module: In each module, set minifyEnabled false, and use consumerProguardFiles function to pass the proguard.txt file from each library to the main module.

Here is explained in detail with code examples, so I'm not going to replicate the answer: https://stackoverflow.com/a/42758629/1516973

查看更多
Melony?
3楼-- · 2019-04-25 12:26

I've had the same problem, and I've written this post on my personal website.

In essence, what you are looking for is the maven-publish Gradle plugin, that will allow you to package your library modules to AAR and conveniently manage and distribute them, while retaining the information on their dependencies.

You can configure this plugin to package a specific buildType for which you will enable proguard obfuscation.

查看更多
登录 后发表回答