启用proguard后片段无法正常工作

Fragment not working properly after enable the progaurd

我有包含 recyclerview 的片段。如果未启用 progaurd,则一切正常。如果我启用 progaurd,则来自 API 的数据不会设置。如果我放置断点,它不会到达构造函数,但片段正在设置,recyclerview 显示空数据。

我添加了progaurd规则也请查看

class NotificationListFragment : Fragment {

    private lateinit var notificationListViewModel: NotificationListViewModel
    private lateinit var notificationListFragmentBinding: NotificationListFragmnetBinding
    private var roles: String = ""
    constructor()
    constructor(roles: String) {
        this.roles = roles
    }
    override fun onCreateView(
            inflater: LayoutInflater,
            container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        notificationListViewModel = NotificationListViewModel(requireActivity().application, requireActivity(), roles)
        notificationListFragmentBinding = DataBindingUtil.inflate(inflater, R.layout.notification_list_fragmnet, container, false)
        notificationListFragmentBinding.notificationListViewModel = notificationListViewModel
        notificationListFragmentBinding.lifecycleOwner = this
        setHasOptionsMenu(true)
        getNotificationList()
        return notificationListFragmentBinding.root
    }

    private fun getNotificationList() {
        notificationListViewModel.notificationListData.observe(viewLifecycleOwner, Observer {
            if (!it.isNullOrEmpty() && it.size > 0) {
                Log.d("TAG", "value::" + it.size)
                val notificationListAdapter = NotificationListAdapter(it, requireActivity(), notificationListViewModel.refresh)
                rvNotificationList.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
                rvNotificationList.adapter = notificationListAdapter
            }
        })
    }
    interface Refresh {
        fun refreshRecyclerView();
    }
}

am calling this fragment from my main activity like this

setFragment(new NotificationListFragment(Constant.name));



private void setFragment(Fragment fragment) {
        try {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.main_container_wrapper_sales, fragment);
            fragmentTransaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Progaurd 规则

-keepattributes *Annotation*
# Preserve R.*.* things.
-keepclassmembers class **.R$* {
public static <fields>;
}
# Preserve things required for parcelable classes.
-keepclassmembers class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator CREATOR;
}
# Preserve serializable things.
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
# Preserve enums.
-keepclassmembers class * extends java.lang.Enum {
public static **[] values();
public static ** valueOf(java.lang.String);
}
# Preserve all .class method names.
#-keepclassmembernames class * {
#java.lang.Class class$(java.lang.String);
#java.lang.Class class$(java.lang.String, boolean);
#}
# Preserve all native method names and the names of their classes.

-keepclasseswithmembernames class * {
native <methods>;
}
# Ignore support lib warnings.
-dontwarn android.support.**
# Preserve khosla labs libs.
-keep public class com.khoslalabs.** { *; }
-keep class com.khoslalabs.vikycapi.** { *; }

# Preserve support libs.
-keep interface android.support.v4.** { *; }
-keep interface android.support.v7.** { *; }
-keep class android.support.** { *; }
# Preserve khosla labs libs.
#Glide
-keep public class * implements com.bumptech.glide.module.GlideModule
-keep public class * extends com.bumptech.glide.module.AppGlideModule
-keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
  **[] $VALUES;
  public *;
}

# Retrofit does reflection on generic parameters. InnerClasses is required to use Signature and
# EnclosingMethod is required to use InnerClasses.
-keepattributes Signature, InnerClasses, EnclosingMethod

# Retrofit does reflection on method and parameter annotations.
-keepattributes RuntimeVisibleAnnotations, RuntimeVisibleParameterAnnotations

# Retain service method parameters when optimizing.
-keepclassmembers,allowshrinking,allowobfuscation interface * {
    @retrofit2.http.* <methods>;
}

# Ignore annotation used for build tooling.
-dontwarn org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement

# Ignore JSR 305 annotations for embedding nullability information.
-dontwarn javax.annotation.**

# Guarded by a NoClassDefFoundError try/catch and only used when on the classpath.
-dontwarn kotlin.Unit

# Top-level functions that can only be used by Kotlin.
-dontwarn retrofit2.KotlinExtensions
-dontwarn retrofit2.KotlinExtensions$*

# With R8 full mode, it sees no subtypes of Retrofit interfaces since they are created with a Proxy
# and replaces all potential values with null. Explicitly keeping the interfaces prevents this.
-if interface * { @retrofit2.http.* <methods>; }
-keep,allowobfuscation interface <1>
-keep class com.address_package.** { *; }
-keep class com.squareup.okhttp.** { *; }
-keep interface com.squareup.okhttp.** { *; }

-dontwarn com.squareup.okhttp.**
-dontwarn okio.**
-keepattributes Signature
-keepattributes *Annotation*
-keep class okhttp3.** { *; }
-keep interface okhttp3.** { *; }

-dontwarn okhttp3.**
-keep class com.squareup.** { *; }
-keep interface com.squareup.** { *; }
-keep class retrofit2.** { *; }
-keep interface retrofit2.** { *;}
-keep interface com.squareup.** { *; }
-dontwarn retrofit2.**
-keepattributes Exceptions
-keep class retrofit2.http.** { *; }

-keepclasseswithmembers class * {
    @retrofit2.http.* <methods>;
}

-dontwarn rx.**
-dontwarn retrofit2.**
-dontwarn okio.**

-keep class com.ex.app.dto.** {*;}
#-keep class com.ex.app.sales.adapters.** {*;}
-keep class com.ex.app.ezetap.pojo.** {*;}
-keep class com.ex.app.dto.CommonPojos.** { *; }
-keep class com.ex.app.deployment.dto.** { *; }
-keep class com.ex.app.fr.dtofr.** { *; }
-keep class com.ex.app.sales.dtosales.** { *; }
-keep class com.ex.app.deployment_tl.dto.** { *; }
-keep class com.ex.app.dynamic_module.dto.** { *; }
#-keep class com.ex.app.common.service.** { *; }

#-keep class android.widget.Toast.** {*;}

-assumenosideeffects class android.util.Log {
    public static *** d(...);
    public static *** v(...);
    public static *** i(...);
    public static *** w(...);
    public static *** e(...);
}

-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# And if you use AsyncExecutor:
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

-dontwarn com.squareup.okhttp.**

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

# Glide Transformations
-dontwarn jp.co.cyberagent.android.gpuimage.**

-keep class android.support.v8.renderscript.** { *; }

-dontwarn jp.co.cyberagent.android.gpuimage.**

-keepattributes *Annotation*
# in order to provide the most meaningful crash reports, add the following line:
-keepattributes SourceFile,LineNumberTable
# If you are using custom exceptions, add this line so that custom exception types are skipped during obfuscation:
-keep public class * extends java.lang.Exception

-keepclassmembers class net.sourceforge.zbar.ImageScanner { *; }
-keepclassmembers class net.sourceforge.zbar.Image { *; }
-keepclassmembers class net.sourceforge.zbar.Symbol { *; }
-keepclassmembers class net.sourceforge.zbar.SymbolSet { *; }

-keepnames class android.arch.lifecycle.ViewModel
-keepclassmembers public class * extends android.arch.lifecycle.ViewModel { public <init>(...); }
-keepclassmembers class com.lebao.app.domain.** { public <init>(...); }
-keepclassmembers class * { public <init>(...); }

-if class androidx.appcompat.app.AppCompatViewInflater
-keep class com.google.android.material.theme.MaterialComponentsViewInflater {
    <init>();
}


-keep class me.dm7.barcodescanner.** {
  *;
}

-keep class net.sourceforge.zbar.** {
  *;
}

-keepclassmembers class net.sourceforge.zbar.ImageScanner { *; }
-keepclassmembers class net.sourceforge.zbar.Image { *; }
-keepclassmembers class net.sourceforge.zbar.Symbol { *; }
-keepclassmembers class net.sourceforge.zbar.SymbolSet { *; }

我发现了问题。及其解决。当启用 progaurd 时,firebase class 文件被删除,所以我添加了一个规则,然后它被解决了。

-keep class com.google.firebase.** { *; }