首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android通用扩展的前卫规则

Android通用扩展的前卫规则
EN

Stack Overflow用户
提问于 2020-11-24 02:26:39
回答 2查看 287关注 0票数 0

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

代码语言:javascript
复制
/**
 * 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)
    } 

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

我已经添加了防护规则-keepattribute签名。尽管如此,这款应用还是在崩溃。

2020-11-24 08:47:28.448 8215-8215/?E/可参数堆栈跟踪错误:可抛出的堆栈跟踪:在libcore.reflect.TypeVariableImpl.resolve(TypeVariableImpl.java:111) at libcore.reflect.TypeVariableImpl.getGenericDeclaration(TypeVariableImpl.java:125) at libcore.reflect.TypeVariableImpl.hashCode(TypeVariableImpl.java:47) at b.c.a.v.a.(TypeToken.java:9) at b.a.e.a.(ListExtension.kt:1) at o.u.u.f(ViewGroupUtilsApi14.java:11)的堆栈跟踪错误:可抛出的java.lang.AssertionError:非法类型变量引用

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

EN

回答 2

Stack Overflow用户

发布于 2020-11-24 13:16:40

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

代码语言:javascript
复制
-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.**
票数 0
EN

Stack Overflow用户

发布于 2020-11-24 14:53:29

代码语言:javascript
复制
##---------------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.** { *; }

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64974191

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档