当minifyEnabled为true时,Parcelable / Parceler错误

时间:2020-04-21 00:37:03

标签: parcelable parceler

设置 minifyEnabled 时出现以下错误: true

找不到为com.codeworks.myapp.FirestoreModel生成的Parcelable类,请验证您的类是否配置正确,并且Parceler生成了Parcelable类com.codeworks.myapp.FirestoreModel $$ Parcelable。

我在 proguard-rules.pro 中的 parceler.org 网站中显示了以下代码:

# Parceler configuration
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
-keep class org.parceler.Parceler$$Parcels

...,并在 FirestoreModel 和Fragment的类中添加了 @Keep 批注:

@Parcel
@Keep
class FirestoreModel{
...
}

@Keep
class MyFragment: Fragment() {
...
}

,然后添加一些保护规则,或者据我从stackoverflow.com的示例中了解的(因为我无法从官方文档中了解规则):

-keepnames class com.codeworks.myapp.MyFragment { *; }
-keepnames class com.codeworks.myappFirestoreModel { *; }

我仍然遇到以下错误:

2020-04-21 08:07:38.554 28188-28188/com.codeworks.myapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.codeworks.myapp, PID: 28188
    h.a.d: Unable to find generated Parcelable class for com.codeworks.myappFirestoreModel, verify that your class is configured properly and that the Parcelable class com.codeworks.myappFirestoreModel$$Parcelable is generated by Parceler.
        at h.a.e$b.a(:154)
        at h.a.e.a(:73)
        at h.a.e.a(:57)
        at com.codeworks.myapp.MyFragment$onViewCreated$1.a(:103)
        at com.codeworks.myapp.MyFragment$onViewCreated$1.a(:68)
        at com.firebase.ui.firestore.FirestoreRecyclerAdapter.b(:158)
        at androidx.recyclerview.widget.RecyclerView$g.a(:7065)
        at androidx.recyclerview.widget.RecyclerView$g.a(:7107)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6012)
        at androidx.recyclerview.widget.RecyclerView$v.a(:6279)
        at androidx.recyclerview.widget.RecyclerView$v.b(:6118)
        at androidx.recyclerview.widget.RecyclerView$v.d(:6114)
        at androidx.recyclerview.widget.LinearLayoutManager$c.a(:2303)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1627)
        at androidx.recyclerview.widget.LinearLayoutManager.a(:1587)
        at androidx.recyclerview.widget.LinearLayoutManager.e(:665)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(:4134)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayout(:3851)
        at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(:1897)
        at androidx.recyclerview.widget.RecyclerView$a.run(:414)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:972)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:727)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:907)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7464)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:549)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:955)

很显然,尽管有 pro-guard 规则,但 MyFragment onViewCreated 函数的变量名仍被重命名。

错误似乎指向以下行:

    // Here... at com.codeworks.myapp.MyFragment$onViewCreated$1.a(:68)
    adapter = object : FirestoreRecyclerAdapter<DpMemoModel, DpViewHolder>(options) {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FirestoreViewHolder{

            val view : View = LayoutInflater.from(parent.context).inflate(R.layout.firestore_cardview, parent, false)

            return FirestoreViewHolder(view)
        }
        override fun onBindViewHolder(
            holder: FirestoreViewHolder,
            position: Int,
            model: FirestoreModel
        ) {
...
            // ...and Here: **at com.codeworks.myapp.MyFragment$onViewCreated$1.a(:103)**
            val wrapped : Parcelable = Parcels.wrap(model)

            holder.cardView.setOnClickListener {

                val intent= Intent(activity, FirestoreInfoActivity::class.java)
                intent.putExtra("parcel", wrapped)
                startActivity(intent)
            }

P.S .:我正在使用FirestoreRecyclerAdapter,我认为这是自处理Cloud Firestore和Firebase Storage数据以来的最佳方法。

1 个答案:

答案 0 :(得分:0)

我不知道出了什么问题或错过了什么。我现在切换到 @Parcelize 。该代码会更长一些,但至少可以正常工作。另外,也无需使用:

# Parceler configuration
-keep interface org.parceler.Parcel
-keep @org.parceler.Parcel class * { *; }
-keep class **$$Parcelable { *; }
-keep class org.parceler.Parceler$$Parcels

...在 proguard-rules.pro 中。我需要的只是数据类中的 @Keep 批注。我花了一天半的时间进行研究,反复试验,但 c'est la vie