how to apply proguard at particular part of code or class or Activity in android

1.1k Views Asked by At

we are enabling proguard in android project for all packages regularly. so,

we are using keep functionality and exclude some particular classes or packages or Library aar's from obfuscate(proguard) in Android.

Here my exact query,

Instead of applying obfuscate/Proguard to entire android project,

can we apply or obfuscate to particular class or code part at android studio project.

Is there any possibilities to apply proguard at willingness places in codebase?

Please help to get solutions. Thanks Advance.

1

There are 1 best solutions below

0
On

It can be achieved, however, not easily out-of-the-box.

Obfuscating just the part of the classes in the app

Basically, the simplest approach would be to group classes which should and should not be obfuscated in different packages.

For example:

  • org.app --> Here you put your classes which do not have to be obfuscated.
  • org.obfuscated.app --> Here you put your classes which do need to be obfuscated.

Now, in proguard-rules.pro (default name of Proguard configuration file) you would add something like this:

-keep class org.app.**

This will keep all the classes located in the specified package and all sub-packages non-obfuscated.

On the other hand, the package org.obfuscated.app will remain obfuscated since it was not explicitly excluded from obfuscation in the Proguard configuration.

The only downside I see at the moment is that you'll have to "duplicate" your original package topology in org.obfuscated.app package.

For more details please consult Proguard configuration manual page

Partial class obfuscation

As for partial class obfuscation, I believe the Proguard does not have such functionality supported.