Proguard with OrmLite on Android

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

How should I use proguard with ormlite library on Android?

Trying this:

-keep class com.j256.** -keepclassmembers class com.j256.** -keep enum com.j256.** -keepclassmembers enum com.j256.** -keep interface com.j256.** -keepclassmembers interface com.j256.** 

But I get:

03-23 20:23:54.518: E/AndroidRuntime(3032): java.lang.RuntimeException: Unable to start activity ComponentInfo{cz.eman.android.cepro/cz.eman.android.cepro.activity.StationsOverviewActivity}: java.lang.IllegalStateException: Could not find constructor that takes a Context argument for helper class class kb

I also tried to add this:

But I get another classmembers errors.

回答1:

Thank you a lot for posts like this that help us to advance step by step.

I've came up with other solution after i have tried the last one without success:

# OrmLite uses reflection -keep class com.j256.** -keepclassmembers class com.j256.** { *; } -keep enum com.j256.** -keepclassmembers enum com.j256.** { *; } -keep interface com.j256.** -keepclassmembers interface com.j256.** { *; } 

I hope it can help someone.



回答2:

I don't have the solution but here are a couple of references to help:

You may be missing:

-keepclassmembers class * {    public (android.content.Context);  }  

and/or

-keepattributes *Annotation* 


回答3:

The accepted answer was not enough for my case, so I enhanced it and this is what I ended up with:

# OrmLite uses reflection -keep class com.j256.** -keepclassmembers class com.j256.** { *; } -keep enum com.j256.** -keepclassmembers enum com.j256.** { *; } -keep interface com.j256.** -keepclassmembers interface com.j256.** { *; }  # Keep the helper class and its constructor -keep class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper -keepclassmembers class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper {   public (android.content.Context); }  # Keep the annotations -keepattributes *Annotation*  # Keep all model classes that are used by OrmLite # Also keep their field names and the constructor -keep @com.j256.ormlite.table.DatabaseTable class * {     @com.j256.ormlite.field.DatabaseField ;     @com.j256.ormlite.field.ForeignCollectionField ;     # Add the ormlite field annotations that your model uses here     (); } 


回答4:

A small addition to the configuration above - if you're trying to serialize / deserialize Joda's DateTime objects via ORMLite, you probably need this as well:

-keepclassmembers class **DateTime {     (long);     long getMillis(); } 

...since ORMLite's DateTimeType does everything via reflection.



回答5:

In addittion to default necessary for reflection:

# OrmLite uses reflection -keep class com.j256.** -keepclassmembers class com.j256.** { *; } -keep enum com.j256.** -keepclassmembers enum com.j256.** { *; } -keep interface com.j256.** -keepclassmembers interface com.j256.** { *; } 

I needed to keep all my Entity classes:

-keep class com.example.db.Entities.** { *; } 

Otherwise entity classes are stripped out. I use predefined DB(generated earlier).

Is there an easier/better way to obfuscate. Maybe I'm keeping too many classes?



回答6:

In my case this did the trick:

-keepattributes SourceFile,LineNumberTable,Signature,InnerClasses,*Annotation* 

and

-keepclassmembers class * {public (android.content.Context);} -keep class com.j256.** { *; } 

With obfucation and optimizations.



回答7:

A small addition for the latest version OrmLite 5.

You may want to add these rows to hide some new warnings:

-dontwarn com.j256.ormlite.android.** -dontwarn com.j256.ormlite.logger.** -dontwarn com.j256.ormlite.misc.** 

Warnings are like these:

Warning:com.j256.ormlite.android.OrmliteTransactionalProcessor: can't find referenced class javax.lang.model.SourceVersion

Warning:com.j256.ormlite.logger.Slf4jLoggingLog: can't find referenced class org.slf4j.LoggerFactory

Warning:com.j256.ormlite.misc.JavaxPersistenceImpl: can't find referenced class javax.persistence.Column



回答8:

I've came up with such solution (maybe will work for somebody too).

Made such changes to proguard.cfg:

  • Added -dontobfuscate option

  • Appended ,!code/allocation/variable to -optimization option

APK file size using such configuration reduced from 580 kB to 250 kB.

Though, no obfuscation is performed.



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