问题描述
我在我的android项目中同时使用了Firebase SDK和Firestore SDK。 在构建启用R8的发布应用程序时遇到问题(禁用R8没问题)。
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();
}
}
我创建“购物车”类的新实例,并用所需的数据(例如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,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
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)