klaxit / hidden-secrets-gradle-plugin

🔒 Deeply hide secrets on Android
MIT License
395 stars 40 forks source link

Errors causes by Proguard obfuscation #31

Closed minichouilleur closed 1 year ago

minichouilleur commented 3 years ago

Hi,

I encounter some conflict between the hidden-secrets-gardle-plugin and proguard obfuscation. I have meet two errors after proguard obfuscation, first is :

"java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String"

This error is due to the native method System.loadLibrary() which is obfuscated.

This error can be fixed by adding this proguard rule :

-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

As it is said by the Proguard documentation (https://www.guardsquare.com/en/products/proguard/manual/examples) :

"If your application, applet, servlet, library, etc., contains native methods, you'll want to preserve their names and their classes' names, so they can still be linked to the native library. The following additional option will ensure that:"

The second error is :

"impossible - java.lang.AssertionError: impossible at java.lang.Enum$1.create(Enum.java:272) at java.lang.Enum$1.create(Enum.java:262) at libcore.util.BasicLruCache.get(BasicLruCache.java:58) at java.lang.Enum.getSharedConstants(Enum.java:289) at java.lang.Enum.valueOf(Enum.java:244) at java.io.ObjectInputStream.readEnum(ObjectInputStream.java:1746) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:374) at android.os.Parcel.readSerializable(Parcel.java:2933) at android.os.Parcel.readValue(Parcel.java:2725) at android.os.Parcel.readArrayMapInternal(Parcel.java:3043) at android.os.BaseBundle.initializeFromParcelLocked(BaseBundle.java:288) at android.os.BaseBundle.unparcel(BaseBundle.java:232) at android.os.BaseBundle.getBoolean(BaseBundle.java:898) ..."

It seems that an enum class is obfuscated and can't be accessed at runtime.

This proguard rule avoid this error :

-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

The problem with this rule, is that it keeps all enum class of the project deobfuscated.

It seems that the enum class that causes this error is one from hidden-secrets-gradle-plugin or one used by it. Because when I remove the plugin, this error disappear.

I would like to identify which enum class causes this problem to avoid the deobfuscation of all my project enum class.

Do you ever encounter this problem ? And do you have an idea of which enum class causes this problem ?

Thank you

ben-j69 commented 3 years ago

Hello @minichouilleur,

I am preparing a new version and would like to solve your issue.

Can you share your proguard file please ?

I tried to reproduce your issue but my default proguard file does not create any issue when building the project and running it.

Thanks

minichouilleur commented 3 years ago

Hi @ben-j69,

Sorry for the late answer.

Here is the proguard file of my project :

-assumenosideeffects class android.util.Log {
    public static boolean isLoggable(java.lang.String, int);
    public static int v(...);
    public static int d(...);
    public static int i(...);
    public static int w(...);
    public static int e(...);
}

-repackageclasses news

-keep class com.*.*.domain.model.** { *; }
-keep class com.*.*.data.remote.model.** { *; }
-keep class com.*.*.ui.model.** { *; }
-keep class com.*.*.extensions.AnalyticHelper

##---------------Begin: proguard configuration for FollowApps ----------
-keep class com.followanalytics.** { *; }
-keep interface com.followapps.android.** { *; }
-keep class me.leolin.shortcutbadger.impl.** { <init>(...); }
##---------------End: proguard configuration for FollowApps ----------

## Used to keep attributes used by hidden secrets lib
-keepattributes SourceFile,LineNumberTable

## Keep native method "System.loadLibrary("secrets")" used by hidden secrets lib
-keepclasseswithmembernames,includedescriptorclasses class * {
    native <methods>;
}

## Used to avoid error shown in my previous comment
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

##---------------Begin: proguard configuration for Gson  ----------
-keepattributes *Annotation*

-dontwarn sun.misc.**

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
##---------------End: proguard configuration for Gson  ----------

Do not hesitate to ask me for other information if necessary.

Have a nice day.

Thanks.

ben-j69 commented 3 years ago

Hello @minichouilleur ,

I have created a new Android app, included Hidden Secret and your proguard file but I had not issue while building. Did you find a way to fix it ?

minichouilleur commented 3 years ago

Hello @ben-j69,

I'm sorry for the answer delay. Have you tried to remove this proguard rule from the file I send you before ?

## Used to avoid error shown in my previous comment
-keepclassmembers enum * {
    public static **[] values();
    public static ** valueOf(java.lang.String);
}

This is the proguard rule we must add to our project to make hidden-secrets-gardle-plugin works. But we want to avoid adding it.

For information, we are using proguardFiles(getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro")

Thanks.

ben-j69 commented 3 years ago

Hello @minichouilleur ,

Sorry but I did not succeed to reproduce your error.

In more there is no Enum in the Hidden Secret code. If you have any other idea feel free to share it.

Thanks.

ispiropoulos commented 1 year ago

@minichouilleur are you sure it's due to proguard? I am getting the first error when I use underscores in key names. Check this: https://github.com/klaxit/hidden-secrets-gradle-plugin/issues/72