Android proguard for Gson

Problem

GSON混淆后执行错误 java.lang.ClassCastException: com.google.gson.internal.LinkedTreeMap cannot be cast to

Solution

I read from gson source through https://github.com/google/gson/blob/master/examples/android-proguard-example/proguard.cfg, here is the content.

##—————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
-keep class sun.misc.Unsafe { *; }
#-keep class com.google.gson.stream.* { ; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.* { ; }

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

总结一下

  1. 是不要混淆GsonJar包中的类
  2. 实现 java.io.Serializable的实体类中所有的属性名称也不能被混淆。
  3. 以及为了序列化和反序列化,writeObject(); readObject(); writeReplace(); readResolve();亦不可被混淆

http://xiaodongdong.blog.51cto.com/7049299/1353704
编写Android Proguard File

plugins { id 'com.android.application' } android { namespace 'com.example.kucun2' compileSdk 33 defaultConfig { applicationId "com.example.kucun2" minSdk 24 targetSdk 33 versionCode 1 versionName "1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' } } compileOptions { sourceCompatibility JavaVersion.VERSION_17 targetCompatibility JavaVersion.VERSION_17 } buildFeatures { viewBinding true } } dependencies { implementation "androidx.activity:activity:1.7.2" implementation "androidx.activity:activity-ktx:1.7.2" implementation 'com.google.code.gson:gson:2.8.9' implementation 'androidx.appcompat:appcompat:1.6.1' implementation 'com.google.android.material:material:1.11.0' implementation 'androidx.constraintlayout:constraintlayout:2.1.3' implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.6.1' implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.6.1' implementation 'androidx.navigation:navigation-fragment:2.4.1' implementation 'androidx.navigation:navigation-ui:2.4.1' testImplementation 'junit:junit:4.13.2' androidTestImplementation 'androidx.test.ext:junit:1.1.3' androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' // 网络请求 implementation 'com.squareup.retrofit2:retrofit:2.9.0' implementation 'com.squareup.retrofit2:converter-gson:2.9.0' // 表格控件 implementation 'io.github.cymchad:BaseRecyclerViewAdapterHelper:4.0.3' // implementation 'io.github.cymchad:BaseRecyclerViewAdapterHelper:4.1.7' implementation 'androidx.recyclerview:recyclerview:1.3.2' // 刷新控件 implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0' // Material Design implementation 'com.google.android.material:material:1.11.0' // 对话框 implementation 'com.github.gabriel-TheCode:AestheticDialogs:1.3.6' implementation 'at.favre.lib:bcrypt:0.9.0' // Android专用BCrypt库 implementation 'androidx.room:room-runtime:2.4.3' annotationProcessor 'androidx.room:room-compiler:2.4.3' } 2 issues were found when checking AAR metadata: 1. Dependency 'androidx.activity:activity-ktx:1.8.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 7.4.2 is 33. Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on). 2. Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it to compile against version 34 or later of the Android APIs. :app is currently compiled against android-33. Also, the maximum recommended compile SDK version for Android Gradle plugin 7.4.2 is 33. Recommended action: Update this project's version of the Android Gradle plugin to one that supports 34, then update this project to use compileSdkVerion of at least 34. Note that updating a library or application's compileSdkVersion (which allows newer APIs to be used) can be done separately from updating targetSdkVersion (which opts the app in to new runtime behavior) and minSdkVersion (which determines which devices the app can be installed on).
最新发布
06-05
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值