Obfuscating a module/library

2019-07-20 02:15发布

I have a library that will be consumed by apps. This module has interfaces and helper classes.

How can I obfuscate the whole module but still allow access to all those interfaces and helper classes?

I tried this:

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }

Then I found myself having to do --keep on all my classes in proguard. Is there something I can add to pro guard so that I can keep all those interfaces/helper-classes and still have it as obfuscated?

1条回答
爷、活的狠高调
2楼-- · 2019-07-20 02:53

If you want to keep only the public API of your library from being obfuscated, you need to specify this public API. There are various approaches for libraries:

  1. Have separate package for public API and actual implementations, e.g. com.example.library contains the public API, whereas com.example.library.internal contains the actual implementations that are hidden.

  2. Make the actual implementations package private

The ProGuard rules would then look like this:

-keep public class com.example.library.* { public protected *; }

This will keep only public classes together with their public or protected methods / fields in the com.example.library package.

查看更多
登录 后发表回答