[BUG]
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 613
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
[Android/Kotlin] R8 移除 Kotlin Metadata 后,非 Boolean 的 is* 属性无法解析,
### 环境信息
*请填写以下信息:*
- Android,minSdk 21
- `com.alibaba.fastjson2:fastjson2:2.0.64.android5`
- `com.alibaba.fastjson2:fastjson2-kotlin:2.0.50`
- `org.jetbrains.kotlin:kotlin-reflect:1.9.25`
- Kotlin 编译器:1.9.25
- AGP:8.7.3,内置 R8:8.7.18
- 全局已开启 `JSONReader.Feature.SupportSmartMatch`
### 重现步骤
Kotlin 中名称以 `is` 开头、但类型不是 `Boolean` 的属性,在 Debug 包可以正常解析,但在开启混淆的 Release 包中会保持默认值。
data class RecommendPlayListItem(
@field:JSONField(name = "is_in")
var is_in: Int = 0
)
服务端json
{
"is_in": 1
}
期望结果:
item.is_in == 1
实际结果(Release 包):
item.is_in == 0
当前混淆规则包括:
-keep class kotlin.Metadata { *; }
-keepattributes RuntimeVisibleAnnotations,Signature
工程全局还配置了 -dontshrink。在当前 R8 8.7.18 下,即使配置了上述规则,最终 Release APK 中的 Kotlin Metadata 仍会被移除。
将注解标记到 setter 上可以修复:
@set:JSONField(name = "is_in")
var is_in: Int = 0
这样 fastjson2 能以 is_in 匹配并调用原有 set_in(),不会绕过 setter。
但工程中可能存在较多同类 Kotlin 属性,逐字段添加注解维护成本较高。
当 Kotlin Metadata 在 Release 包中不可用时,fastjson2 是否有全局配置或官方推荐的扩展方式,可兼容非 Boolean 的 isXxx / is_xxx 属性?
项目是这个较老的项目,不能开启FieldBased,之前有较多的地方有逻辑在set函数中。
未开启 FieldBased 时,@field:JSONField 标记在 Kotlin 私有 backing field 上不生效,是否符合 fastjson2 的预期设计?
Contributor guide
Research direction
Start by reproducing the Release-only failure with R8 and the shown RecommendPlayListItem example. Trace fastjson2's Kotlin property and JSONField handling, comparing @field and @set behavior when Kotlin Metadata is absent and FieldBased is disabled. Done means identifying a supported global configuration or extension path, or documenting the limitation with a regression test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- android, java, kotlin
- Domain
- backend-api-design, mobile
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Active
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100