有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我在安卓项目中同时使用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();
    }
}

我创建了我的“Cart”类的一个新实例,用所需的数据(比如totalPrice…)填充它 当我将此对象发送到Firebase实时数据库时,结果如下所示:

enter image description here

而不是:

enter image description here

我的全定制程序规则。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 安卓.app.Activity
-keep public class * extends 安卓.app.Application
-keep public class * extends 安卓.app.Service
-keep public class * extends 安卓.content.BroadcastReceiver
-keep public class * extends 安卓.content.ContentProvider
-keep public class * extends 安卓.app.backup.BackupAgent
-keep public class * extends 安卓.preference.Preference
-keep public class * extends 安卓.app.Fragment

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

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

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

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

-keepclassmembers class * extends 安卓.app.Activity {
public void *(安卓.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 安卓.os.Parcelable {
    public static final 安卓.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 安卓.support.v4.app.** { *; }
-keep interface 安卓.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 安卓.support.**
-dontwarn com.google.ads.**
-dontwarn 安卓x.appcompat.widget.**
-dontwarn com.squareup.okhttp.**

-keep class project.iobird.menutium安卓.models.** { *; }
-keep class project.iobird.menutium安卓.RichLinkPreview.** { *; }
-keep interface project.iobird.menutium安卓.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.安卓.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 安卓x.recyclerview.widget.RecyclerView { *; }

#Crashlytics measurements
-keep class com.google.安卓.gms.measurement.** { *; }
-dontwarn com.google.安卓.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>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.OutcomingTextMessageViewHolder {
    public <init>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.OutcomingImageMessageViewHolder {
    public <init>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.BaseIncomingMessageViewHolder {
    public <init>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.IncomingTextMessageViewHolder {
    public <init>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}
-keep class * extends com.stfalcon.chatkit.messages.MessageHolders.IncomingImageMessageViewHolder {
    public <init>(安卓.view.View, java.lang.Object);
    public <init>(安卓.view.View);
}

-assumenosideeffects class com.安卓.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.安卓.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

共 (0) 个答案