android - 使用 Proguard 后无法让 Ormlite 工作

标签 android proguard ormlite

我正在使用 ormlite 4.24。

在使用 shr​​inkResources true 构建发布版本之后 minifyEnabled true 我的应用因以下错误而失败:

"java.lang.RuntimeException: java.sql.SQLException: SQL statement failed: CREATE TABLE `ac` (`A` VARCHAR , `B` VARCHAR , `C` VARCHAR..."

尝试以下选项后:

# 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.** { *; }

我仍然无法使用发布版本。

我试过使用 Ormlite 版本。 5 包括其他设置,例如:

# 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 classes and members of our entities or OrmLite will not work
-keep class com.myApp.activity.**
-keepclassmembers class com.myApp.activity.** {*;}

# Keep the annotations
-keepattributes Exceptions,InnerClasses,Signature,Deprecated,SourceFile,LineNumberTable,*Annotation*,EnclosingMethod

-libraryjars <java.home>/lib/rt.jar


# 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>();
}

-keepattributes Signature
-keep class com.myApp.activity.database.** { *; }

-keepclassmembers class classpath.** {
  public *;
}

-dontwarn org.slf4j.**
-dontwarn org.apache.log4j.**
-dontwarn org.apache.commons.logging.**
-dontwarn org.apache.commons.codec.binary.**
-dontwarn javax.persistence.**
-dontwarn javax.lang.**
-dontwarn javax.annotation.**
-dontwarn javax.tools.**

但我仍然无法运行我的应用程序。我目前收到以下错误:

"java.lang.IllegalArgumentException: No fields have a DatabaseField annotation in class com.myApp.bean.p"

最佳答案

对于 OrmLite,我使用这个配置:

-keepattributes *Annotation*,Signature

-dontnote com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper
-keepclassmembers class * extends com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper {
    <init>(android.content.Context);
}

-dontnote com.j256.ormlite.field.DatabaseFieldConfig
-keepclassmembers class com.j256.ormlite.field.DatabaseFieldConfig {
    <fields>;
}

-dontnote com.j256.ormlite.dao.Dao
-keepclassmembers class * implements com.j256.ormlite.dao.Dao {
    <init>(**);
    <init>(**, java.lang.Class);
}

-dontnote com.j256.ormlite.android.AndroidLog
-keep class com.j256.ormlite.android.AndroidLog {
    <init>(java.lang.String);
}

-dontnote com.j256.ormlite.table.DatabaseTable
-keep @com.j256.ormlite.table.DatabaseTable class * {
    void set*(***);
    *** get*();
}

-dontnote com.j256.ormlite.field.DatabaseField
-keepclassmembers @interface com.j256.ormlite.field.DatabaseField {
    <methods>;
}

-dontnote com.j256.ormlite.field.ForeignCollectionField
-keepclassmembers @interface com.j256.ormlite.field.ForeignCollectionField {
    <methods>;
}

-keepclasseswithmembers class * {
    @com.j256.ormlite.field.DatabaseField <fields>;
}

-keepclasseswithmembers class * {
    @com.j256.ormlite.field.ForeignCollectionField <fields>;
}

-dontnote org.joda.time.DateTime
-keep,allowobfuscation class org.joda.time.DateTime
-keepclassmembers class org.joda.time.DateTime {
    <init>(long);
    long getMillis();
}

这应该是所有必要的。

关于android - 使用 Proguard 后无法让 Ormlite 工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38901398/

相关文章:

java - 如何解决 OrmLite 中巨大的 byte[] 问题

android - 将 Ormlite 与 Android 的内容提供程序结合使用

android - Android 中 String、StringBuffer 和 StringBuilder 的内存不足异常

android - 如何获得root手机的NFCEE_ADMIN权限?

android - 使用 Python 进行 Unicode URL 编码/解码

android - MonkeyRunner的方法 "device.press"或 "device.touch"没有生效

android - 为什么在 ProGuard 中禁用混淆会破坏优化步骤?

java - ProGuard 混淆、java、Google Gson 和通用集合——如何保留成员?

android - Proguard 警告 : com. example.android.PaymentGateway : can't find referenced field 'int container_pg' in class com. example.android.R$id

android - Ormlite Android 批量插入