Obfuscating ORMLite model classes with Proguard

2019-05-01 06:28发布

I have a few models that I want to obfuscate in my code. I know that I could just ignore the whole model package but I don't want to do that. I tried a few proguard tweaks and checked all relevant posts to no avail. ORMlite keeps throwing java.lang.RuntimeException: Unable to create application ...App: java.lang.IllegalArgumentException: Foreign field class ....f.p does not have id field. I checked that the annotation is still there with dex2jar and jd, and it is still there.

I have this proguard configuration (and a lot more that obfuscates other parts):

aggressive stuff :

-mergeinterfacesaggressively
-allowaccessmodification
-optimizationpasses 5

-verbose
-dontskipnonpubliclibraryclasses
-dontpreverify
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*

keep information needed by various frameworks:

-keepattributes *Annotation*
-keepattributes Signature
-keepattributes EnclosingMethod

ORMLITE related:

-keep class com.j256.**
-keepclassmembers class com.j256.** { *; }
-keep enum com.j256.**
-keepclassmembers enum com.j256.** { *; }
-keep interface com.j256.**
-keepclassmembers interface com.j256.** { *; }

Am I missing something or is this just not possible?

2条回答
Fickle 薄情
2楼-- · 2019-05-01 07:05

In addition to Vivek Soneja's answer: There is a way to keep entity classes independently from their packages:

-keep @com.j256.ormlite.table.DatabaseTable class * {
  @com.j256.ormlite.field.DatabaseField <fields>;
  @com.j256.ormlite.field.ForeignCollectionField <fields>;
  <init>();
}

It will keep all DatabaseTable annotated classes as well as their DatabaseField and ForeignCollectionField annotated fields

查看更多
地球回转人心会变
3楼-- · 2019-05-01 07:13

As ORMLite uses reflection to save or retain your data, they want un-obfuscated name of entity (ie classes you are using to save or retain data).

This exception is thrown because ORMLite is trying to find the Entity classes for its tables and its not able to find the classes and members with similar name.

Just Ignore your entity classes from getting obfuscated using following code:

-keep class com.xyz.components.**
-keepclassmembers class com.xyz.components.** { *; } 

where com.xyz.components is your package for Entity classes.

I hope this helps!

查看更多
登录 后发表回答