You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

minifyEnabled设为true时出现NullPointerException,RecyclerView项无内容如何解决?

问题解决方案
  • 修正ProGuard混淆规则,当前规则仅覆盖了Menu外部类,未包含其内部类Prodotti,内部类在ProGuard规则中需要用$符号关联外部类,更新规则如下:
# 保留Menu所有内部类及成员
-keep class it.gabtamagnini.visualposmobile.models.Menu$* { *; }
-keepclassmembers class it.gabtamagnini.visualposmobile.models.Menu$* { *; }

如果仅需要保护Prodotti这一个内部类,也可以精准指定:

-keep class it.gabtamagnini.visualposmobile.models.Menu$Prodotti { *; }
-keepclassmembers class it.gabtamagnini.visualposmobile.models.Menu$Prodotti { *; }

如果项目使用了Gson、FastJson等序列化/反序列化库,还需要补充对应库的通用混淆规则,避免序列化时字段映射失败。

  • 优化代码空安全逻辑,避免NPE风险,将equals调用替换为java.util.Objects.equals(JDK7及以上支持),该方法会自动处理空对象场景:
public Menu.Prodotti getItem(Menu.Prodotti prodotto) {
    for (Menu.Prodotti item : items) {
        if (Objects.equals(item.codice, prodotto.codice) && item.jump == prodotto.jump) {
            return item;
        }
    }
    return null;
}
  • 可以使用@Keep注解简化规则配置,如果项目引入了androidx.annotation:annotation依赖,直接给Menu.Prodotti类添加@Keep注解,即可告知R8/ProGuard保留该类及其所有成员,无需手动编写ProGuard规则。

  • 验证混淆规则是否生效,构建Release包后,在项目路径build/outputs/mapping/release/mapping.txt中搜索Prodotti类,查看其codice、jump等字段是否保留了原名称,确认没有被重命名或删除。

内容的提问来源于stack exchange,提问作者NiceToMytyuk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 22:36:00