Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ProGuard with AppBundle and data-binding: IllegalStateException

I have a multi-module project (I use Android App Bundles) with data-binding, ViewModel. But when I enable Proguard the app crashes with the following error :

java.lang.RuntimeException: Unable to start activity ComponentInfo{...MyActivity}: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null

Caused by: java.lang.IllegalStateException: DataBindingUtil.setConte… R.layout.my_activity) must not be null
        at MyActivity.onCreate(MyActivity.kt:38)

When I remove modules and create one app module, everything works. When I disable Proguard, it also works fine. Here is part of my proguard-rules.pro:

-dontwarn android.databinding.**
-keep class android.databinding.** { *; }
-keep class com.example.module1.databinding.** { *; }
-keep class com.example.module2.databinding.** { *; }

In build.gradle:

minifyEnabled true
useProguard true

So the error points to this line of code:

val binding: com.example.module1.databinding.MyActivityBinding =
                DataBindingUtil.setContentView(this, R.layout.my_activity)

Maybe any ideas how to fix?

like image 507
alla Avatar asked Jan 28 '19 14:01

alla


1 Answers

Only need to add this to your proguard-rules.pro, where module1 and module2 are Dynamic Feature Modules with databinding enabled.

-keep class com.example.module1.DataBinderMapperImpl { *; } 
-keep class com.example.module2.DataBinderMapperImpl { *; } 
like image 118
shp Avatar answered Sep 17 '22 20:09

shp