Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Obfuscating ORMLite model classes with Proguard

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?

like image 518
meredrica Avatar asked Nov 03 '13 12:11

meredrica


2 Answers

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!

like image 185
Vivek Soneja Avatar answered Nov 12 '22 16:11

Vivek Soneja


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

like image 1
repitch Avatar answered Nov 12 '22 16:11

repitch