Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Android 使用 R8 minify 时报告 fastjson1 相关 class 缺失导致编译失败 #2466

Closed
layou233 opened this issue Apr 20, 2024 · 3 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@layou233
Copy link

问题描述

Android 使用 R8 minify 时报告 fastjson1 相关 class 缺失导致编译失败。观察源码发现似乎存在几处兼容 fastjson1 的逻辑(见日志),可能引发了该问题。使用前几个版本时没有遇到过这些问题,不清楚是否 fastjson2 或者 gradle/android 插件更新引发的新错误。

环境信息

"com.alibaba.fastjson2:fastjson2:2.0.49.android5"

"com.android.application" version 8.3.1

源码 https://github.com/layou233/MCPingForAndroid

重现步骤

在 build.gradle 中启用 minifyEnabled 然后使用 Gradle assembleRelease task 进行编译即可触发。没有自行添加任何 proguard rule。

相关日志输出

Missing classes detected while running R8. Please add the missing classes or apply additional keep rules that are generated in <MyProjectDirectory>/app/build/outputs/mapping/release/missing_rules.txt.

Missing class com.alibaba.fastjson.JSONArray (referenced from: java.lang.Class com.alibaba.fastjson2.JSONFactory.getClassJSONArray1x())
Missing class com.alibaba.fastjson.JSONObject (referenced from: java.lang.Object com.alibaba.fastjson2.JSONFactory$FJ1ObjectInnerSupplier.apply(java.lang.Object) and 3 other contexts)

附加信息

目前我的临时解决方案是添加一行 ProGuard 规则:

# Ignore fastjson v1 references to make minifier happy.
-dontwarn com.alibaba.fastjson.*

因为我的项目没有依赖 fastjson1 ,我直接忽略掉了 fastjson1 相关警告,可以正常编译,并且在实机上简单测试发现应用运行正常。

@layou233 layou233 added the bug Something isn't working label Apr 20, 2024
@wenshao
Copy link
Member

wenshao commented Apr 20, 2024

为了优化兼容旧版时的首次性能,目前的实现是这样的。你是否能把你的解决办法写下来,我加入文档中。

@layou233
Copy link
Author

layou233 commented Apr 20, 2024

解决方法已在附加信息一栏。我推测此问题仅在不使用 fastjson1 的时候会有,如果编译包含 fastjson1 应该不会找不到 class。

如果不使用反射相关方法实现兼容的话,可能确实只能通过此方法解决。

希望日后能有更好的解决方法!😄

@wenshao wenshao added this to the 2.0.50 milestone May 12, 2024
@wenshao wenshao added the fixed label May 12, 2024
@wenshao
Copy link
Member

wenshao commented May 12, 2024

https://github.com/alibaba/fastjson2/releases/tag/2.0.50
2.0.50已发布,请用新版本

@wenshao wenshao closed this as completed May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants