Android R8 duplicates fields of custom models classes when sent to Firebase

405 views Asked by At

I am using both Firebase SDK and Firestore SDK into my android project. I am facing a problem when building a release app with R8 enabled (no problem with R8 disabled).

I have a Model class named "Cart", below is the code :

    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();
    }
}

I create a new instance of my "Cart" class, fill it with needed data (like totalPrice ...) When i send this object to Firebase realtime database the result is like below :

enter image description here

instead of :

enter image description here

My full custom proguard-rules.pro file is as following :

    # 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
0

There are 0 answers