【问题标题】:Android crash while using GSON Library and ProGuard使用 GSON 库和 ProGuard 时 Android 崩溃
【发布时间】:2015-03-31 12:59:39
【问题描述】:

我在 GSON 库中使用以下 Proguard 配置:

# Gson uses generic type information stored in a class file when working with fields. Proguard 
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature  

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { *; }

# Keep any class that intereracts with GSON
-keep class com.trifecta.engine.api.** { *; }

但在测试导出的 apk 时出现错误:

Caused by: java.lang.AbstractMethodError: abstract method 
"void com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$a.a(com.google.gson.stream.JsonWriter, java.lang.Object)"
       at com.google.gson.internal.bind.ReflectiveTypeAdapterFactory$Adapter.write(ReflectiveTypeAdapterFactory.java:200)
       at com.google.gson.Gson.toJson(Gson.java:546)
       at com.google.gson.Gson.toJson(Gson.java:525)
       at com.google.gson.Gson.toJson(Gson.java:480)
       at com.google.gson.Gson.toJson(Gson.java:460)

我已经在 SO 上研究了这个问题,我遵循了 Proguard 配置:[https://code.google.com/p/google-gson/source/browse/trunk/examples/android-proguard-example/proguard.cfg],但仍然没有成功。 1

【问题讨论】:

    标签: android gson proguard


    【解决方案1】:

    试试这个 sn-p 告诉我

    ## GSON 2.2.4 specific rules ##
    
    # Gson uses generic type information stored in a class file when working with fields. Proguard
    # removes such information by default, so configure it to keep all of it.
    -keepattributes Signature
    
    # For using GSON @Expose annotation
    -keepattributes *Annotation*
    
    -keepattributes EnclosingMethod
    
    # Gson specific classes
    -keep class sun.misc.Unsafe { *; }
    -keep class com.google.gson.stream.** { *; }
    

    【讨论】:

    • 我认为-keepattributes EnclosingMethod-keep class com.google.gson.stream.** { *; } 是新的,虽然我会试一试。谢谢。
    • 哇。 -keepattributes EnclosingMethod 背后有一些魔力,它是唯一缺少的东西。
    • 嗨!你能告诉我在使用 ProGuard 的同时如何处理 JNI 吗?
    • 不理解 JNI?你想要一个 proguard 配置
    • 是的。实际上我在一个项目中使用 PjSip,它使用 Native C 方法和一些 .so 文件。启用 ProGuard 后,网络通话中语音丢失。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-09
    • 2022-08-26
    相关资源
    最近更新 更多