question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewBinding and proguard rules for obfuscation

See original GitHub issue

If you are using the abstract class ViewBindingEpoxyModelWtihHolder and you have obfuscation with R8 enabled, then the code does not work because it is trying to get the reflected bind method from the ViewBinding class. Since bind is not part of the ViewBinding interface and gets added only in implementations the reflection is the only way to go.

To solve this the following rule should be added to proguard:

-keepclassmembers class <package name of your app>.databinding.**  {
    public <methods>;
}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:9
  • Comments:5

github_iconTop GitHub Comments

2reactions
davidbilikcommented, Jan 13, 2022

Both problems can be solved with this proguard rules

-keep class * extends $package.ViewBinding  { *;}
-keepclassmembers class * extends $package.ViewBinding  { *;}

-keep class * extends $package.path.to.ViewBindingEpoxyModelWithHolder  { *;}
-keepclassmembers class * extends $package.path.to.ViewBindingEpoxyModelWithHolder { *;}

and don’t forget to change the class paths to point to your package for the ViewBinding classes and to ViewBindingEpoxyModelWithHolder class. Apparently R8 somehow messes up the classes and when the bind method of the ViewBinding class is trying to be resolved, this line

val genericSuperclass = klass.genericSuperclass

will incorrectly resolve the superclass. I’ve created a vanilla project and tried it with only my classes and the problem is there as well so it has nothing to do with Epoxy per se.

So the solution, even though I don’t know the original cause, is to keep those EpoxyModels extending from ViewBindingEpoxyModelWithHolder and don’t allow R8 to mess with them

1reaction
ubarua123commented, Dec 13, 2021
java.lang.NullPointerException: Missing required view with ID: com.muuzzer_wq.android.apps.readers_writers.community:id/header_title
at com.wonderquill.databinding.HoirzontalRowHeaderBinding.bind(HoirzontalRowHeaderBinding.java:5)

The controller where it crashed, wasn’t even inflating this model. I have a build running in production with the same code.

It’s only after I upgraded to:

JDK: 11 Gradle: 7.0.4 Arctic Fox

that all my problems started. Worst part is, it’s working fine in debug. Obviously it has something to do with proguard. These are my proguard rules if that helps:

-keepclassmembers class * extends androidx.viewbinding.ViewBinding {
    public static *** bind(android.view.View);
}
-keepclassmembers class com.wonderquill.databinding.**  {
    public <methods>;
}

-keep class * implements androidx.viewbinding.ViewBinding {
    public static *** bind(android.view.View);
    public static *** inflate(android.view.LayoutInflater);
}

This is the model class:

@EpoxyModelClass(layout = R.layout.hoirzontal_row_header)
abstract class HorizontalHeaderModel : ViewBindingEpoxyModelWithHolder<HoirzontalRowHeaderBinding>() {

     override fun HoirzontalRowHeaderBinding.bind() {

     }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

ProGuard Manual: Examples - Guardsquare
Useful configurations and examples for ProGuard. ... These options shrink, optimize, and obfuscate the single Android activity com.example.MyActivity :
Read more >
Troubleshooting ProGuard issues on Android - Medium
There are also versions of the three keep rules that only prevent obfuscation (renaming), but don't prevent shrinking. You can take look at...
Read more >
ProGuard with AppBundle and data-binding - android
I have a multi-module project (I use Android App Bundles) with data-binding, ViewModel. But ...
Read more >
Shrink, obfuscate, and optimize your app - Android Developers
In fact, R8 works with all of your existing ProGuard rules files, so updating the Android ... Enables code shrinking, obfuscation, and optimization...
Read more >
Data Binding class missing during runtime with Proguard on in ...
Proguard set on for library module. ... -dontwarn android.databinding. ... are providing library output as SDK for partners so code obfuscation is needed....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found