Android Studio打包签名正式版时遇GsonConverterFactory引用错误怎么解决?
解决Android Studio正式版构建时GsonConverterFactory引用未解析的问题
针对调试版正常运行、可生成签名调试包,但正式版构建出现Unresolved reference: GsonConverterFactory或Gson引用未解析的问题,按以下步骤排查解决:
1. 确认依赖引入范围
检查模块级build.gradle(或build.gradle.kts)中,是否将Gson相关依赖添加到全局构建配置,而非仅debugImplementation:
- Groovy示例:
dependencies { implementation libs.retrofit implementation libs.retrofit.gson implementation libs.google.gson implementation libs.okhttp.logging } - Kotlin DSL示例:
dependencies { implementation(libs.retrofit) implementation(libs.retrofit.gson) implementation(libs.google.gson) implementation(libs.okhttp.logging) }
如果依赖仅在debugImplementation下,正式版构建会无法找到相关类。
2. 添加Gson混淆规则
正式版构建默认开启代码混淆,若未配置Gson相关规则,会导致类被混淆后无法识别。在proguard-rules.pro中添加以下规则:
# Gson及Retrofit Gson转换器混淆规则 -keepattributes Signature -keepattributes *Annotation* -keep class com.google.gson.** { *; } -keep class com.squareup.retrofit2.converter.gson.** { *; } -keep class 你的数据模型包名.** { *; } // 替换为实际的实体类包路径,避免模型类被混淆
3. 清理缓存并重构项目
执行以下操作清除缓存并重新构建:
- 点击
Build -> Clean Project - 点击
Build -> Rebuild Project - 点击
File -> Invalidate Caches...,选择Invalidate and Restart重启Android Studio
4. 验证版本兼容性
确认依赖版本匹配:你的配置中Retrofit(2.9.0)与Retrofit-Gson(同版本)、Gson(2.10.1)版本兼容,若后续仍有问题,可尝试将Retrofit升级至稳定新版本,保持Retrofit与Retrofit-Gson版本一致。
内容的提问来源于stack exchange,提问作者Tippu Fisal Sheriff
相关产品推荐
相关产品推荐

