# ============================================================ # baselibrary 库模块 consumer-rules.pro # 传递给依赖本库的 App 模块的混淆规则(必须存在!) # build.gradle -> defaultConfig -> consumerProguardFiles "consumer-rules.pro" # ============================================================ # 保留注解、泛型签名、运行时注解(网络层反射核心属性) -keepattributes Signature,*Annotation*,AnnotationDefault,RuntimeVisibleAnnotations,RuntimeVisibleParameterAnnotations,InnerClasses,EnclosingMethod # ============================== # BaseNetworkApi:反射获取实际 Service 类型的泛型基类(核心!) # 修复:java.lang.Class cannot be cast to java.lang.reflect.ParameterizedType # ============================== -keep class com.abbidot.baselibrary.network.base.BaseNetworkApi { *; } -keep class * extends com.abbidot.baselibrary.network.base.BaseNetworkApi { *; } # 关键:保留所有继承 BaseNetworkApi 的子类的父类泛型参数声明(防止 R8 擦除 actualTypeArguments) -keep,allowshrinking class * extends com.abbidot.baselibrary.network.base.BaseNetworkApi { (...); } -keepclassmembers class * extends com.abbidot.baselibrary.network.base.BaseNetworkApi { ; ; } # 保留 IService 接口 -keep interface com.abbidot.baselibrary.network.base.IService { *; } # ============================== # BaseResponse:所有网络响应的泛型基类(Gson 反序列化依赖) # ============================== -keep class com.abbidot.baselibrary.network.base.BaseResponse { *; } # 保持所有 BaseResponse 子类的泛型签名不被擦除 -keepclassmembers class * extends com.abbidot.baselibrary.network.base.BaseResponse { ; (...); } # ============================== # Gson TypeAdapter:StringNullAdapter / DoubleDefault0Adapter # 通过 GsonBuilder.registerTypeAdapter 反射实例化 # ============================== -keep class com.abbidot.baselibrary.network.interceptor.StringNullAdapter { *; } -keep class com.abbidot.baselibrary.network.interceptor.DoubleDefault0Adapter { *; } -keepclassmembers class com.abbidot.baselibrary.network.interceptor.StringNullAdapter { (); } -keepclassmembers class com.abbidot.baselibrary.network.interceptor.DoubleDefault0Adapter { (); } # ============================== # OkHttp Interceptor:CommonRequestInterceptor / CommonResponseInterceptor # ============================== -keep class com.abbidot.baselibrary.network.interceptor.CommonRequestInterceptor { *; } -keep class com.abbidot.baselibrary.network.interceptor.CommonResponseInterceptor { *; } -keepclassmembers class com.abbidot.baselibrary.network.interceptor.CommonRequestInterceptor { (); } -keepclassmembers class com.abbidot.baselibrary.network.interceptor.CommonResponseInterceptor { (); } # ============================== # 异常类(反射抛出时需保留类名) # ============================== -keep class com.abbidot.baselibrary.network.exception.** { *; } # ============================== # Retrofit 3.0 / OkHttp 5(Kotlin-first 新版本,额外加固规则) # ============================== -keep,allowobfuscation,allowshrinking interface retrofit2.Call -keep,allowobfuscation,allowshrinking class retrofit2.Response -keep,allowobfuscation,allowshrinking class kotlin.coroutines.Continuation -keep,allowobfuscation,allowshrinking class retrofit2.KotlinExtensions # 所有带 Retrofit http 注解的接口方法必须保留(用于动态代理反射) -if interface * { @retrofit2.http.* ; } -keep,allowobfuscation interface * { ; } # OkHttp 5 消除内部平台警告 -dontwarn okhttp3.internal.platform.** -dontwarn org.conscrypt.** -dontwarn org.bouncycastle.** -dontwarn org.openjsse.** -dontwarn org.codehaus.mojo.animal_sniffer.* # ============================== # Gson(配合 Retrofit 3.0 converter-gson 使用) # ============================== -keep class com.google.gson.** { *; } -dontwarn com.google.gson.** # TypeToken 匿名内部类的泛型签名必须保留 -keep class com.google.gson.reflect.TypeToken { *; } -keep class * extends com.google.gson.reflect.TypeToken { *; } -keepclassmembers class * extends com.google.gson.reflect.TypeToken { (...); java.lang.reflect.Type getType(); } -keepnames class com.google.gson.internal.** { *; } # SerializedName / Expose 注解字段保留 -keepclassmembers,allowshrinking,allowobfuscation class * { @com.google.gson.annotations.SerializedName ; @com.google.gson.annotations.Expose ; } # ============================== # 自定义 View / LayoutInflater 反射构造函数保留 # 所有通过 XML inflate 的自定义 View 必须保留 (Context, AttributeSet) 构造 # ============================== -keepclassmembers class * extends android.view.View { public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int); public (android.content.Context, android.util.AttributeSet, int, int); void set*(***); *** get*(); } -keepclassmembers class * extends android.view.ViewGroup { public (android.content.Context, android.util.AttributeSet); public (android.content.Context, android.util.AttributeSet, int); public (android.content.Context, android.util.AttributeSet, int, int); } # baselibrary 内部自定义 View -keep class com.abbidot.baselibrary.widget.** { *; } -keepclassmembers class com.abbidot.baselibrary.widget.** { public (...); ; ; } # ============================== # 常量类(EventName/ConState/MMKVKey 等):伴生对象 const val 虽被 inline,但类名/成员名保留方便反射查找 # ============================== -keep class com.abbidot.baselibrary.constant.** { *; } -keepclassmembers class com.abbidot.baselibrary.constant.** { ; ; public static final *; } # ============================== # XEventBus 事件总线(核心:发送/接收/观察者包装类必须保留) # ============================== -keep class com.abbidot.baselibrary.eventbus.** { *; } -keep class com.abbidot.baselibrary.eventbus.core.** { *; } -keepclassmembers class * { @com.abbidot.baselibrary.constant.EventName ; } # ============================== # BaseRecyclerAdapter 列表适配器和 ViewHolder # ============================== -keep class com.abbidot.baselibrary.list.** { *; } -keepclassmembers class com.abbidot.baselibrary.list.** { public (...); ; ; } # ============================== # baselibrary.util:AppUtils/LogUtil/MMKVUtil/Utils(双重保险:app 模块里也写了一份) # ============================== -keep class com.abbidot.baselibrary.util.** { *; } -keepclassmembers class com.abbidot.baselibrary.util.** { public (...); ; ; } # ============================== # Parcelable / kotlinx-parcelize:跨模块 Intent 传值必备 # ============================== # 注意:@Parcelize 是 SOURCE/BINARY 级编译期注解,运行时不存在,禁止写 -keep @kotlinx.parcelize.Parcelize -keep class * implements android.os.Parcelable { *; } -keepclassmembers class * implements android.os.Parcelable { public static final android.os.Parcelable$Creator *; public static final android.os.Parcelable$ClassLoaderCreator *; (android.os.Parcel); (android.os.Parcel, java.lang.ClassLoader); void writeToParcel(android.os.Parcel, int); int describeContents(); } # kotlin-parcelize 运行时生成的内部辅助类($Creator、$Parceler) -keep class **$Creator { *; } -keep class **$Parceler { *; } -keepclassmembers class **$Creator { ; ; } -keepclassmembers class **$Parceler { ; ; } -dontwarn kotlinx.parcelize.** # ============================== # ViewModel:跨模块 by viewModels() 反射创建 # ============================== -keep class * extends androidx.lifecycle.ViewModel { *; } -keepclassmembers class * extends androidx.lifecycle.ViewModel { public (...); protected (...); ; ; } # ============================== # Kotlin 协程 / suspend 函数:Continuation 签名不被擦除 # ============================== -keep class kotlin.coroutines.Continuation { *; } -keep class kotlin.coroutines.intrinsics.** { *; } -keep class kotlinx.coroutines.** { *; } -dontwarn kotlinx.coroutines.** -dontwarn kotlin.coroutines.** # ============================== # Kotlin 伴生对象 / 单例 INSTANCE:EventName.Companion / ConState.Companion 等 # ============================== -keepclassmembers class **$Companion { ; ; public static final *; static *; } -keep class **$Companion { *; } -keepclassmembers class * { public static final ** INSTANCE; public static final ** Companion; public static ** getINSTANCE(); public static ** getCompanion(); }