I'm facing a really weird problem for days now...
I have a Gradle app with two modules, one main module and one library module.
here is the first problem: even if minifyEnabled is false in the library, it gets obfuscated. it has no effect at all and seems to depend on the main module's settings. I´m using proguard since a while now and I've never experienced such a behavior at all.
Also, the proguard-rules.pro of the library seems to be completely ignored. It doesn't matter what I declare there, it is not used and the result is always the same (always decompiled to view result). It's obfuscated with the default settings.
I've used an invalid proguard file name to see if the file is even touched, and indeed there are errors with the wrong name and it also complaines about syntax errors in the proguard file...
I don't know if it is somehow related to an update of Android Studio, as it has also recommended me to use "minifyEnabled" instead of "runProguard".
How can I manage proguard to use the proguard-rules.pro of the library too?
Edit:
I've made an sample project to clarify my problem
The proguard config of my library
The gradle of my library:
And finally the result I always get. It doesn't matter what I exclude/include in the proguard config
As you can see, the proguard rules work quite well on the main module. It does what it should. But it always fully obfuscates my library to a.a....
It also has completely deleted the Activity of the Library, which shouldn't happen at all
Go to Gradle Scripts and then look for a file called proguard-rules.pro and there you can edit it. The proguard-rules.pro file is where you can add custom ProGuard rules. By default, this file is located at the root of the module (next to the build. gradle file).
R8 gives better output results than Proguard. R8 reduces the app size by 10 % whereas Proguard reduces app size by 8.5 %. The android app having a Gradle plugin above 3.4. 0 or above then the project uses R8 by default with Proguard rules only.
You can obfuscate Android code to provide security against reverse engineering. You can use the Android ProGuard tool to obfuscate, shrink, and optimize your code. Obfuscated code can be more difficult for other people to reverse engineer.
You typically should not enable ProGuard in the library project. ProGuard processes the application and the library together in the application project, which is the most effective approach.
In the library project, you can specify any library-specific ProGuard configuration in build.gradle, e.g.:
defaultConfig { consumerProguardFiles 'proguard-rules.txt' }
This file is then packaged in the library aar as proguard.txt
and automatically applied in the application project.
If you do enable ProGuard in a library project (maybe because you want to distribute the library), then you also have to add the proper configuration for processing the library. The Android Gradle build doesn't seem to do this automatically. You can:
android-sdk/tools/proguard/examples/library.pro
to proguard-project.txt
in your library project.-injars
, -outjars
, -libraryjars
, -printmapping
from the file. The Gradle build process automatically provides these options.Enabling/disabling ProGuard independently for the library project and for the application project works fine for me.
Amazingly, Eric's answer is actually working for me too!
Of course Eric knows what he is talking about, but I have [on and off] been trying to find a clean way to do this automatically in gradle for over a year with no luck until I just found this post today.
I combined a few other SO threads and came up w/ this solution that works, that may also be able to be simplified. Steps 1-4 are optional, but so far it hasn't seemed to have hurt.
/android-sdk/tools/proguard5.2
/android-sdk/tools/proguard
to .../android-sdk/tools/proguard4.7
ln -s .../android-sdk/tools/proguard5.2 .../android-sdk/tools/proguard
android-sdk/tools/proguard/examples/library.pro
to the library project folder and rename to proguard-library.pro
proguard-library.pro
and comment out the -injars
, -outjars
, -libraryjars
, and -printmapping
lines. Edit the library's build.gradle
file to include:
defaultConfig { minifyEnabled true shrinkResources true proguardFiles 'proguard-library.pro' consumerProguardFiles 'proguard-library-consumer.pro' }
You can tweak this to have different behavior for release and debug builds.
proguard-library-consumer.pro
# include in this file any rules you want applied to a # consumer of this library when it proguards itself. -dontwarn junit.** -dontwarn org.junit.** # Make crash call-stacks debuggable. -keepnames class ** { *; } -keepattributes SourceFile,LineNumberTable
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With