5

When creating a APK with proguard enabled, the following exception is thrown when using the FirebaseRecyclerAdapter from the Firebase-UI library (com.firebaseui:firebase-ui:0.3.0):

java.lang.RuntimeException: java.lang.NoSuchMethodException: <init> [class android.view.View]
                                                                              at com.firebase.ui.FirebaseRecyclerAdapter.onCreateViewHolder(FirebaseRecyclerAdapter.java:168)

The debug version (without proguard) works fine. Who has a working proguard config for Firebase-UI?

My current proguard config looks like this (only the Firebase related parts):

-optimizationpasses 5
-keepattributes SourceFile,LineNumberTable,Exceptions, Signature, InnerClasses,*Annotation*

-keepnames class ** { *; }

-keep class com.firebase.** { *; }
-keepnames class com.fasterxml.jackson.** { *; }
-keepnames class javax.servlet.** { *; }
-keepnames class org.ietf.jgss.** { *; }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Peter
  • 10,910
  • 3
  • 35
  • 68
  • 3
    The exception comes from [this line](https://github.com/firebase/FirebaseUI-Android/blob/0.3.0/library/src/main/java/com/firebase/ui/FirebaseRecyclerAdapter.java#L168) in the FirebaseUI code. FirebaseUI for Android uses reflection to create the ViewHolder instances. You'll want to add `-keep class com.yourpackage.yourviewholder { *; }` to the proguard config, so that it knows not to strip your view holder. – Frank van Puffelen Jan 11 '16 at 00:25
  • 2
    Thanks for your comment Frank, it pointed me into the right direction. I was however not able to solve it with a rule that points to the ViewHolder as you indicate; I keep them as internal classes. I tried most solutions proposed here: [link](http://stackoverflow.com/questions/14059888/android-proguard-keep-inner-class), but didn't end up with a working solution. In the end I solved this by moving my viewholders to a dedicated package and adding: `-keep class com.mypackage.myapp.viewholders.** { *; }` This works fine. If I find a better approach I will post it here. – Peter Jan 11 '16 at 21:22
  • 1
    Sounds like a pretty good solution to me. Can you add it as an answer? – Frank van Puffelen Jan 11 '16 at 22:30

2 Answers2

15

Solved this by moving the ViewHolder classes that are used by the FirebaseRecyclerAdapter to a dedicated package (e.g. com.mypackage.myapp.viewholders) and adding a rule within the proguard configuration to prevent that classes within this package become obfuscated by proguard:

-keep class com.mypackage.myapp.viewholders.** { *; }
Peter
  • 10,910
  • 3
  • 35
  • 68
  • 2
    I've tried every other solution that I have found on StackOverflow but only this one worked for me! Thanks!! – Sandra Feb 04 '18 at 22:58
3

Well, I had my ViewHolder inside relative FirebaseRecyclerAdapter as an inner class and gave me this error. Making the inner class has solved the problem.

Also https://github.com/firebase/FirebaseUI-Android/issues/46#issuecomment-167373575 states same thing with an addition.

Inner class ViewHolder must be public and static so that it could be initiated via reflection.

guness
  • 6,336
  • 7
  • 59
  • 88