Obfuscating ORMLite model classes with Proguard

纵饮孤独 提交于 2019-12-07 06:13:48

问题


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?


回答1:


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!




回答2:


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



来源:https://stackoverflow.com/questions/19753051/obfuscating-ormlite-model-classes-with-proguard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!