Java Android R8在发送到Firebase时复制自定义模型类的字段

Java Android R8在发送到Firebase时复制自定义模型类的字段,java,android,firebase,proguard,android-r8,Java,Android,Firebase,Proguard,Android R8,我在android项目中同时使用Firebase SDK和Firestore SDK。 我在构建启用R8的发布应用程序时遇到了一个问题(禁用R8没有问题) 我有一个名为“Cart”的模型类,下面是代码: public class Cart implements Serializable { private int productCount; private double totalPrice; private Map<String, CartProduct

我在android项目中同时使用Firebase SDK和Firestore SDK。 我在构建启用R8的发布应用程序时遇到了一个问题(禁用R8没有问题)

我有一个名为“Cart”的模型类,下面是代码:

    public class Cart implements Serializable {

    private int productCount;
    private double totalPrice;
    private Map<String, CartProductItem> items;
    private String storeId;
    private String userNote;
    //
    private List<CartProductItem> itemsList;

    public Cart() {
        totalPrice = 0;
        productCount = 0;
    }

    public Cart(Cart cart) {
        this.productCount = cart.productCount;
        this.totalPrice = cart.totalPrice;
        this.storeId = cart.storeId;
        this.userNote = cart.userNote;
        this.items = new HashMap<>();
        this.items.putAll(cart.items);
        this.itemsList = new ArrayList<>();
        this.itemsList.addAll(cart.itemsList);
    }

    public void updateCart(Cart cart) {
        productCount = cart.productCount;
        totalPrice = cart.totalPrice;
        if (items != null) {
            items.clear();
            items.putAll(cart.items);
        } else {
            items = cart.items;
        }

        itemsList = null;
        fillList();
    }

    public void resetValues() {
        productCount = 0;
        totalPrice = 0;
        try {
            items.clear();
        } catch (Exception ee) {
            items = null;
        }
        try {
            itemsList.clear();
        } catch (Exception e) {
            itemsList = null;
        }
        storeId = null;
    }

    public void fillList() {
        if (itemsList == null) {
            itemsList = new ArrayList<>();
        } else {
            itemsList.clear();
        }
        if (items != null) {
            for (String key : items.keySet()) {
                CartProductItem item = items.get(key);
                item.setId(key);
                itemsList.add(item);
            }
        }
        if (itemsList == null || itemsList.size() == 0) {
            productCount = 0;
        }
    }

    @PropertyName("products_count")
    public int getProductCount() {
        return productCount;
    }

    @PropertyName("products_count")
    public void setProductCount(int productCount) {
        this.productCount = productCount;
    }

    @PropertyName("total_price")
    public double getTotalPrice() {
        return totalPrice;
    }

    @PropertyName("total_price")
    public void setTotalPrice(double totalPrice) {
        this.totalPrice = totalPrice;
    }

    @PropertyName("items")
    public Map<String, CartProductItem> getItems() {
        return items;
    }

    @SuppressWarnings("unused")
    @PropertyName("items")
    public void setItems(Map<String, CartProductItem> items) {
        this.items = items;
    }

    @Exclude
    public List<CartProductItem> getItemsList() {
        return itemsList;
    }

    @Exclude
    public String getStoreId() {
        return storeId;
    }

    @Exclude
    public void setStoreId(String storeId) {
        this.storeId = storeId;
    }

    @Exclude
    public String getUserNote() {
        return userNote;
    }

    @Exclude
    public void setUserNote(String userNote) {
        this.userNote = userNote;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Cart cart = (Cart) o;
        return productCount == cart.productCount && Double.compare(cart.totalPrice, totalPrice) == 0 && Objects.equals(itemsList, cart.itemsList);
    }

    @NonNull
    @Override
    public String toString() {
        return "Cart{" +
                "\n products_count: " + productCount +
                "\n total_price: " + totalPrice +
                "\n itemsList: " + items.toString() +
                '}';
    }

    public void putCartProductItem(CartProductItem item) {
        if (items ==null) {
            items = new HashMap<>();
        }
        items.put(item.getId(), item);
        productsCountAndTotalPrice();
    }

    public void removeCartProductItem(CartProductItem item) {
        if (items != null && items.containsKey(item.getId())) {
            if (item.getQuantity() < 1) {
                item.setQuantity(1);
            }
            productCount-= item.getQuantity();
            totalPrice-= item.getTotalItemPrice();
            items.remove(item.getId());
        }
    }

    private void productsCountAndTotalPrice() {
        double totalPrice = 0;
        int count = 0;
        for (CartProductItem item : items.values()) {
            totalPrice += item.getTotalItemPrice();
            count += item.getQuantity();
        }
        this.totalPrice = totalPrice;
        this.productCount = count;
    }

    public boolean containsRequiredData() {
        return productCount > Constants.ZERO
                && totalPrice >= Constants.ZERO
                && !TextUtils.isEmpty(storeId)
                && !Utils.isMapEmpty(items);
    }

    @Exclude
    public int getCartItemsCount() {
        return Utils.isMapEmpty(items) ? Constants.ZERO : items.size();
    }
}
公共类购物车实现可序列化{
私有int产品计数;
私人双总价;
私人地图项目;
私有字符串storeId;
私有字符串userNote;
//
私人物品清单;
公共购物车(){
总价=0;
productCount=0;
}
公共购物车(购物车){
this.productCount=cart.productCount;
this.totalPrice=cart.totalPrice;
this.storeId=cart.storeId;
this.userNote=cart.userNote;
this.items=new HashMap();
this.items.putAll(cart.items);
this.itemsList=新的ArrayList();
this.itemsList.addAll(cart.itemsList);
}
公共作废更新Cart(购物车){
productCount=cart.productCount;
totalPrice=cart.totalPrice;
如果(项!=null){
items.clear();
items.putAll(cart.items);
}否则{
项目=购物车。项目;
}
itemsList=null;
填充列表();
}
public void resetValues(){
productCount=0;
总价=0;
试一试{
items.clear();
}捕获(异常ee){
items=null;
}
试一试{
itemsList.clear();
}捕获(例外e){
itemsList=null;
}
storeId=null;
}
公共无效填充列表(){
if(itemsList==null){
itemsList=新的ArrayList();
}否则{
itemsList.clear();
}
如果(项!=null){
for(字符串键:items.keySet()){
CartProductItem=items.get(键);
项目.设置ID(键);
项目列表。添加(项目);
}
}
if(itemsList==null | | itemsList.size()==0){
productCount=0;
}
}
@PropertyName(“产品数量”)
public int getProductCount(){
返回产品计数;
}
@PropertyName(“产品数量”)
公共void setProductCount(int productCount){
this.productCount=productCount;
}
@PropertyName(“总价”)
公共双getTotalPrice(){
返回总价;
}
@PropertyName(“总价”)
公共无效设置总价(双倍总价){
this.totalPrice=totalPrice;
}
@PropertyName(“项目”)
公共地图getItems(){
退货项目;
}
@抑制警告(“未使用”)
@PropertyName(“项目”)
公共无效集合项(映射项){
这个项目=项目;
}
@排除
公共列表getItemsList(){
返回项目列表;
}
@排除
公共字符串getStoreId(){
返回storeId;
}
@排除
公共无效设置存储ID(字符串存储ID){
this.storeId=storeId;
}
@排除
公共字符串getUserNote(){
返回用户注释;
}
@排除
public void setUserNote(字符串userNote){
this.userNote=userNote;
}
@凌驾
公共布尔等于(对象o){
如果(this==o)返回true;
如果(o==null | | getClass()!=o.getClass())返回false;
购物车=(购物车)o;
return productCount==cart.productCount&&Double.compare(cart.totalPrice,totalPrice)==0&&Objects.equals(itemsList,cart.itemsList);
}
@非空
@凌驾
公共字符串toString(){
返回“Cart{”+
“\n产品计数:”+productCount+
“\n总价:”+总价+
“\n itemsList:”+items.toString()+
'}';
}
公共无效putCartProductItem(CartProductItem项目){
if(items==null){
items=newhashmap();
}
items.put(item.getId(),item);
productsCountAndTotalPrice();
}
公共作废removeCartProductItem(CartProductItem){
if(items!=null&&items.containsKey(item.getId()){
if(item.getQuantity()<1){
项目.设置数量(1);
}
productCount-=item.getQuantity();
totalPrice-=item.getTotalItemPrice();
items.remove(item.getId());
}
}
私有无效产品目录和总价(){
双倍总价=0;
整数计数=0;
对于(CartProductItem:items.values()){
totalPrice+=item.getTotalItemPrice();
count+=item.getQuantity();
}
this.totalPrice=totalPrice;
this.productCount=计数;
}
公共布尔值包含所需数据(){
返回productCount>Constants.ZERO
&&totalPrice>=Constants.ZERO
&&!TextUtils.isEmpty(storeId)
&&!Utils.isMapEmpty(项目);
}
@排除
public int getCartItemsCount(){
返回Utils.isMapEmpty(items)?Constants.ZERO:items.size();
}
}
我创建了我的“Cart”类的一个新实例,用所需的数据(比如totalPrice…)填充它 当我将此对象发送到Firebase实时数据库时,结果如下所示:

而不是:

我的完整自定义proguard-rules.pro文件如下所示:

    # This is a configuration file for ProGuard.
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-verbose

# Optimization is turned off by default. Dex does not like code run
# through the ProGuard optimize and preverify steps (and performs some
# of these optimizations on its own).
#-dontoptimize
#-dontpreverify

# If you want to enable optimization, you should include the
# following:
-optimizations !code/simplification/arithmetic,!code/simplification/cast,!field/*,!class/merging/*
-optimizationpasses 2
-allowaccessmodification
#
# Note that you cannot just include these flags in your own
# configuration file; if you are including this file, optimization
# will be turned off. You'll need to either edit this file, or
# duplicate the contents of this file and remove the include of this
# file from your project's proguard.config path property.

-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgent
-keep public class * extends android.preference.Preference
-keep public class * extends android.app.Fragment

# For native methods, see http://proguard.sourceforge.net/manual/examples.html#native
-keepclasseswithmembernames class * {
native <methods>;
}

-keep public class * extends android.view.View {
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
public void set*(...);
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}

-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}

-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}

# For enumeration classes, see http://proguard.sourceforge.net/manual/examples.html#enumerations
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}

-keepnames class * implements android.os.Parcelable {
    public static final android.os.Parcelable$Creator *;
    public static final ** CREATOR;
}

-keepclassmembers class **.R$* {
public static <fields>;
}

-keepnames class * implements java.io.Serializable
-keepclassmembers class * implements java.io.Serializable {
    static final long serialVersionUID;
    private static final java.io.ObjectStreamField[] serialPersistentFields;
    !static !transient <fields>;
    private void writeObject(java.io.ObjectOutputStream);
    private void readObject(java.io.ObjectInputStream);
    java.lang.Object writeReplace();
    java.lang.Object readResolve();
}

-keep class android.support.v4.app.** { *; }
-keep interface android.support.v4.app.** { *; }
-keep class com.actionbarsherlock.** { *; }
-keep interface com.actionbarsherlock.** { *; }
# The support library contains references to newer platform versions.
# Don't warn about those in case this app is linking against an older
# platform version. We know about them, and they are safe.
-dontwarn android.support.**
-dontwarn com.google.ads.**
-dontwarn androidx.appcompat.widget.**
-dontwarn com.squareup.okhttp.**

-keep class project.iobird.menutiumandroid.models.** { *; }
-keep class project.iobird.menutiumandroid.RichLinkPreview.** { *; }
-keep interface project.iobird.menutiumandroid.RichLinkPreview.** { *; }

-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.GeneratedAppGlideModule
-keep public enum com.bumptech.glide.load.resource.bitmap.ImageHeaderParser$** {
    **[] $VALUES;
    public *;
}
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep class com.bumptech.glide.GeneratedAppGlideModuleImpl

-keep class butterknife.** { *; }
-dontwarn butterknife.internal.**
-keep class **$$ViewBinder { *; }

-keepclasseswithmembernames class * {
    @butterknife.* <fields>;
}

-keepclasseswithmembernames class * {
    @butterknife.* <methods>;
}

-keepattributes SourceFile,LineNumberTable
-keep public class * extends java.lang.Exception
-keep public class * extends java.lang.annotation.Annotation

#related to Firebase
# Keep custom model classes
#-keep class com.google.firebase.example.fireeats.java.model.** { *; }
-keep class com.google.android.gms.** { *; }
-keep class com.google.firebase.** { *; }
-keep class com.google.firebase.iid.FirebaseInstanceId { zza(...); }

# https://github.com/firebase/FirebaseUI-Android/issues/1175
-dontwarn okio.**
-dontwarn retrofit2.Call
-dontnote retrofit2.Platform$IOS$MainThreadExecutor
#-keep class androidx.recyclerview.widget.RecyclerView { *; }

#Crashlytics measurements
-keep class com.google.android.gms.measurement.** { *; }
-dontwarn com.google.android.gms.measurement.**

#Apache commons math3
-keep class org.apache.commons.math3.** { *; }
-dontwarn org.apache.commons.math3.**

-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.BaseOutcomingMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.OutcomingTextMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.OutcomingImageMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.BaseIncomingMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.IncomingTextMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.IncomingImageMessageViewHolder {
    public <init>(android.view.View, java.lang.Object);
    public <init>(android.view.View);
}

-assumenosideeffects class com.android.volley.VolleyLog {
    public static void v(...);
    public static void d(...);
    public static void e(...);
    public static void wtf(...);
}

##---------------Begin: proguard configuration for Gson  ----------
# Gson uses generic type information stored in a class file when working with fields. Proguard
# removes such information by default, so configure it to keep all of it.
-keepattributes Signature

# For using GSON @Expose annotation
-keepattributes *Annotation*

# Gson specific classes
-dontwarn sun.misc.**
#-keep class com.google.gson.stream.** { *; }

# Application classes that will be serialized/deserialized over Gson
-keep class com.google.gson.examples.android.model.** { <fields>; }

# Prevent proguard from stripping interface information from TypeAdapter, TypeAdapterFactory,
# JsonSerializer, JsonDeserializer instances (so they can be used in @JsonAdapter)
-keep class * extends com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapter
-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer

# Prevent R8 from leaving Data object members always null
-keepclassmembers,allowobfuscation class * {
@com.google.gson.annotations.SerializedName <fields>;
}
##---------------End: proguard configuration for Gson  ----------

#Add below line into your proguard-rules.pro to output a full report of all the rules that R8 applies when building your project.
-printconfiguration ./R8-generated/full-r8-config.txt
-printusage ./R8-generated/r8-usage.txt
#这是ProGuard的配置文件。
# http://proguard.sourceforge.net/index.html#manual/usage.html
-dontusemixedcaseclassnames
-DontskipnonPublicLibraryClass
-冗长的
#默认情况下,优化处于关闭状态。Dex不喜欢代码运行
#通过ProGuard优化和预验证步骤(并执行一些
#这些优化的一部分)。
#-dontoptimize
#-不要翻转
#如果要启用优化,则应包括
#以下:
-优化!代码/简化/算术,!代码/简化/转换,!字段/*,!班级/我