Search code examples
parcelableparceler

Error with Parcelable / Parceler when minifyEnabled true


I am getting the following error when I set minifyEnabled true:

Unable to find generated Parcelable class for com.codeworks.myapp.FirestoreModel, verify that your class is configured properly and that the Parcelable class com.codeworks.myapp.FirestoreModel$$Parcelable is generated by Parceler.

I placed the following code as shown in the parceler.org website in proguard-rules.pro:

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

... and added the @Keep annotation in the FirestoreModel and the Fragment's class:

@Parcel
@Keep
class FirestoreModel{
...
}

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

and then added some pro-guard rules, or as I understood it from the examples from stackoverflow.com (because I can't understand the rules from official documentation):

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

I am still getting the following error:

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)

Apparently, the variable names of the onViewCreated function on MyFragment are being renamed despite the pro-guard rules.

The error seems to point to this lines:

    // 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.: I am using FirestoreRecyclerAdapter, I thought it was the best way since I was dealing with Cloud Firestore and Firebase Storage data.


Solution

  • I don't know what went wrong or what I've missed. I switched to @Parcelize for now. The code is a bit longer, but at least it works. Also, there is no need to use:

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

    ...in the proguard-rules.pro. All I need is @Keep annotation in the data class. I've spent a day and a half on research and trial and error, but, well, c'est la vie.