Proguard with OrmLite on Android

前端 未结 8 2007
无人及你
无人及你 2020-11-27 17:46

How should I use proguard with ormlite library on Android?

Trying this:

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


        
相关标签:
8条回答
  • 2020-11-27 17:58

    In my case this did the trick:

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

    and

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

    With obfucation and optimizations.

    0 讨论(0)
  • 2020-11-27 17:59

    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

    0 讨论(0)
  • 2020-11-27 18:04

    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 <init>(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 <fields>;
        @com.j256.ormlite.field.ForeignCollectionField <fields>;
        # Add the ormlite field annotations that your model uses here
        <init>();
    }
    
    0 讨论(0)
  • 2020-11-27 18:06

    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?

    0 讨论(0)
  • 2020-11-27 18:07

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

    • Proguard support request around ORMLite
    • ORMLite proguard discussion #1
    • ORMLite proguard discussion #2

    You may be missing:

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

    and/or

    -keepattributes *Annotation*
    
    0 讨论(0)
  • 2020-11-27 18:16

    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.

    0 讨论(0)
提交回复
热议问题