Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run Proguard on multi-module project as "one piece"

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!!!

like image 290
Kirill K Avatar asked Jun 04 '17 12:06

Kirill K


2 Answers

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

like image 191
rolgalan Avatar answered Oct 14 '22 09:10

rolgalan


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.

like image 25
Venator85 Avatar answered Oct 14 '22 07:10

Venator85