android 代码混淆 gson,Android RecyclerView和Gson混淆问题

RecyclerView 配置混淆

release打包后发现RecyclerView不显示数据, 推测是反射没有加混淆问题, 具体置如下

# When layoutManager xml attribute is used, RecyclerView inflates

#LayoutManagers' constructors using reflection.

-keep public class * extends androidx.recyclerview.widget.RecyclerView$LayoutManager {

public (android.content.Context, android.util.AttributeSet, int, int);

public ();

}

-keepclassmembers class androidx.recyclerview.widget.RecyclerView {

public void suppressLayout(boolean);

public boolean isLayoutSuppressed();

}

复制代码

b136cc6b5a661f9fb8852b3388d34155.png

源码查证

RecyclerView反射创建LayoutManager的过程 :

//androidx.recyclerview.widget.RecyclerView

/**

* Instantiate and set a LayoutManager, if specified in the attributes.

*/

private void createLayoutManager(Context context, String className, AttributeSet attrs,

int defStyleAttr, int defStyleRes){

if (className != null) {

className = className.trim();

if (!className.isEmpty()) {

className = getFullClassName(context, className);

try {

ClassLoader classLoader;

if (isInEditMode()) {

// Stupid layoutlib cannot handle simple class loaders.

classLoader = this.getClass().getClassLoader();

} else {

classLoader = context.getClassLoader();

}

Class extends LayoutManager> layoutManagerClass =

Class.forName(className, false, classLoader)

.asSubclass(LayoutManager.class);

Constructor extends LayoutManager> constructor;

Object[] constructorArgs = null;

try {

constructor = layoutManagerClass

.getConstructor(LAYOUT_MANAGER_CONSTRUCTOR_SIGNATURE);

constructorArgs = new Object[]{context, attrs, defStyleAttr, defStyleRes};

} catch (NoSuchMethodException e) {

try {

constructor = layoutManagerClass.getConstructor();

} catch (NoSuchMethodException e1) {

e1.initCause(e);

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Error creating LayoutManager " + className, e1);

}

}

constructor.setAccessible(true);

setLayoutManager(constructor.newInstance(constructorArgs));

} catch (ClassNotFoundException e) {

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Unable to find LayoutManager " + className, e);

} catch (InvocationTargetException e) {

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Could not instantiate the LayoutManager: " + className, e);

} catch (InstantiationException e) {

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Could not instantiate the LayoutManager: " + className, e);

} catch (IllegalAccessException e) {

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Cannot access non-public constructor " + className, e);

} catch (ClassCastException e) {

throw new IllegalStateException(attrs.getPositionDescription()

+ ": Class is not a LayoutManager " + className, e);

}

}

}

}

复制代码

Gson混淆配置

##---------------Begin: proguard configuration for Gson ----------

# 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

-dontwarn sun.misc.**

#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson

-keep class com.google.gson.examples.android.model.** { ; }

-keep class com.xxx.client.bean.Cctv{ ; }

-keep class com.xxx.client.bean.CctvItem{ ; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,

# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)

-keep class * extends com.google.gson.TypeAdapter

-keep class * implements com.google.gson.TypeAdapterFactory

-keep class * implements com.google.gson.JsonSerializer

-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null

-keepclassmembers,allowobfuscation class * {

@com.google.gson.annotations.SerializedName ;

}

##---------------End: proguard configuration for Gson ----------

复制代码

其中以下这两句用来防止Gson解析的实体被混淆 Gson().fromJson(jsonCCTV, Cctv::class.java) :

-keep class com.xxx.client.bean.Cctv{ ; }

-keep class com.xxx.client.bean.CctvItem{ ; }

复制代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值