Release APK构建报错:存在大小写冲突的Getter或字段
解决Firestore序列化冲突:Found two getters or fields with conflicting case sensitivity
嘿,我一眼就看出你这个报错的问题所在了!这个Found two getters or fields with conflicting case sensitivity for property: allowaddorder错误,本质是Firebase Firestore的序列化机制在处理字段和getter方法时,会把它们统一转成小写来匹配,结果发现了重复的属性标识。
举个例子,你的UIDOrg类里:
- 公共字段叫
AllowAddOrder,转成小写是allowaddorder - 对应的getter方法是
getAllowAddOrder(),Firebase会自动去掉get前缀再转小写,结果也是allowaddorder
这就导致Firebase误以为你定义了两个相同的属性,直接抛出冲突错误了。
接下来给你两个靠谱的解决方案,选一个适合你的就行:
方案1:用@PropertyName注解强制指定属性名
直接在冲突的字段和对应的getter方法上加上@PropertyName注解,明确告诉Firebase这个属性的统一名称,这样就不会因为大小写转换搞混了。修改后的代码片段如下:
import android.support.annotation.Keep; import com.google.firebase.firestore.IgnoreExtraProperties; import com.google.firebase.firestore.PropertyName; // 别忘了加这个导入 import com.google.firebase.Timestamp; @IgnoreExtraProperties @Keep public class UIDOrg { // 给字段加注解指定属性名 @PropertyName("allowAddOrder") public Boolean AllowAddOrder; // 对应的getter也要加相同的注解 @PropertyName("allowAddOrder") public Boolean getAllowAddOrder() { return AllowAddOrder; } // 其他所有字段和对应的getter都要这么处理,比如Org字段: @PropertyName("org") public String Org; @PropertyName("org") public String getOrg() { return Org; } // ... 剩下的字段和getter都按这个模式添加注解,构造函数保持不变 }
方案2:遵循JavaBean命名规范调整命名
Firebase的序列化器是严格遵循JavaBean规范的,所以你可以把字段名改成小驼峰格式(比如把AllowAddOrder改成allowAddOrder),这样getter方法getAllowAddOrder()就和字段名完美对应了,Firebase也不会再识别出冲突。修改后的示例代码:
import android.support.annotation.Keep; import com.google.firebase.firestore.IgnoreExtraProperties; import com.google.firebase.Timestamp; @IgnoreExtraProperties @Keep public class UIDOrg { // 字段名改成小驼峰 public String org; public Timestamp lastEdit; public String rootCollection; public Boolean userRoleDelivery, userRoleFulfilment, userRoleOrder, userRoleVerification, allowEditOrder, allowAddOrder; public Long localNotificationReminderTime; // getter方法对应小驼峰字段,逻辑不变 public Long getLocalNotificationReminderTime() { return localNotificationReminderTime; } public Boolean getUserRoleDelivery() { return userRoleDelivery; } // ... 剩下的getter都对应调整字段名 public java.util.Date getLastEdit() { return lastEdit.toDate(); } // 构造函数的参数和赋值也要对应改成小驼峰 public UIDOrg(String org, Timestamp lastEdit, String rootCollection, Boolean userRoleDelivery, Boolean userRoleFulfilment, Boolean userRoleOrder, Boolean userRoleVerification, Boolean allowEditOrder, Boolean allowAddOrder, Long localNotificationReminderTime) { this.org = org; this.lastEdit = lastEdit; this.rootCollection = rootCollection; this.userRoleDelivery = userRoleDelivery; this.userRoleFulfilment = userRoleFulfilment; this.userRoleOrder = userRoleOrder; this.userRoleVerification = userRoleVerification; this.allowEditOrder = allowEditOrder; this.allowAddOrder = allowAddOrder; this.localNotificationReminderTime = localNotificationReminderTime; } public UIDOrg() { } }
额外提醒
- 不管选哪个方案,都要确保所有字段和对应的getter都处理到位,别漏了任何一个,不然还会出现类似的大小写冲突报错。
- 结合你Release版本的混淆配置(
minifyEnabled true),除了@Keep注解,建议在ProGuard规则里加上这两行,避免Firestore相关的序列化代码被混淆掉:-keep class com.google.firebase.firestore.** { *; } -keep class 你的包名.UIDOrg { *; } // 把这里的“你的包名”换成UIDOrg类的实际完整包名
内容的提问来源于stack exchange,提问作者H Wong
相关产品推荐
相关产品推荐

