【问题标题】:Android proguard rule for generic extension通用扩展的 Android proguard 规则
【发布时间】:2020-11-23 18:26:39
【问题描述】:

我创建了一个 gson 扩展来序列化和反序列化对象,如下代码。

/**
 * To serialize the object to json string
 */

    fun Any.toGson(): String {
        return Gson().toJson(this)
    }

/**
 * To deserialize the json string to object of type <T>
 */

    fun <T>String.toObject() : T{
        return Gson().fromJson(this, object : TypeToken<T>() {}.type)
    } 

当我在发布模式下构建项目时,应用程序由于 proguard 规则而崩溃。

我添加了 proguard 规则 -keepattribute 签名。应用程序仍然崩溃。

2020-11-24 08:47:28.448 8215-8215/? E/Paramthrowable Stacktrace 错误:Throwable java.lang.AssertionError:非法类型变量引用 在 libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111) 在 libcore.reflect.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:125) 在 libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47) 在 b.c.a.v.a.(TypeToken.java:9) 在 b.a.a.a.e.a.(ListExtension.kt:1) 在 o.u.u.f(ViewGroupUtilsApi14.java:11)

以上是堆栈跟踪。 ListExtension 包含问题中提到的代码 需要帮助来解决此问题。

【问题讨论】:

  • 发生了什么崩溃?请添加日志。

标签: android kotlin gson proguard kotlin-extension


【解决方案1】:

在你的 pro-guard 文件中试试这个

-keepattributes SourceFile,LineNumberTable

-keepattributes *Annotation*
# Do not obfuscate classes with Injected Constructors
-keepclasseswithmembernames class * { @javax.inject.Inject <init>(...); }
# Do not obfuscate classes with Injected Fields
-keepclasseswithmembernames class * { @javax.inject.Inject <fields>; }
# Do not obfuscate classes with Injected Methods
-keepclasseswithmembernames class * { @javax.inject.Inject <methods>; }

-keep class **__Factory { *; }
-keep class **__MemberInjector { *; }

# Specifies that string constants that correspond to class names should be obfuscated as well.
-adaptclassstrings com.example.your_package_name.**

【讨论】:

    【解决方案2】:
    ##---------------Begin: proguard configuration for Gson  ----------
    
    -keepattributes Signature
    
    # 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 mypersonalclass.data.model.** { *; }
    

    为模型添加保留规则,我认为您的扩展功能在模型类中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-11
      • 2018-02-05
      • 1970-01-01
      • 2020-01-06
      • 2015-08-26
      • 2017-07-08
      相关资源
      最近更新 更多