android - 通用扩展的 Android proguard 规则
问题描述
我创建了一个 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.getGenericDeclaration(TypeVariableImpl.java:125) 处的 libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111) 处的非法类型变量引用。 reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47) 在 bcava(TypeToken.java:9) 在 baaaea(ListExtension.kt:1) 在 ouuf(ViewGroupUtilsApi14.java:11)
以上是堆栈跟踪。ListExtension 包含问题需要帮助解决此问题中提到的代码。
解决方案
##---------------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.** { *; }
添加保留模型的规则,我认为您的扩展功能在模型类中
推荐阅读
- google-apps-script - DriveApp 在环聊聊天机器人应用脚本中访问哪个帐户
- jenkins - 如何在声明性管道中打印插件变量?
- python - Python 的闭包是如何工作的
- java - 如何在 Ubuntu sublime-text3 中编译和运行 java 包中的文件?
- django - 使用 Django 中的日志记录捕获服务器错误 500(服务器 gunicorn)
- strapi - 如何从strapi.js中的关系中获取数据?
- python - 带有给定变量的 Python Dict 附加列表
- python - 拆分和使用 Excel 列中的值
- java - 当我从 mysql 检索时间时,时间晚了六个小时
- java - 父实体的主键未在子实体中存储为外键