Databinding not working correctly after code obfuscation

Viewed 624

I'm trying to use my aar library as Maven dependency. The library uses AndroidX, Databinding and code obfuscation.

the library works when `minifyEnabled=false' but when I turn on code obfuscation it crashes.

The problem is, inside Activity.onCreate(savedInstanceState) when I call:

binding = DataBindingUtils.setContentView(Activity,ResInt);
binding.getRoot(); // binding == null

I don't get an ClassNotFoundException but a NullPointerException

I also tried to use

binding = MyActivityBinding.inflate(LayoutInflater,parent,attachtoparent);
setContentView(binding.getRoot());

but binding is still null.

First I achieve the migration by replacing all android.support libraries with the corresponding AndroidX package.

that's my gradle.properties

android.useAndroidX=true
android.enableJetifier=true
android.enableR8=false
android.databinding.enableV2=false

and my proguardrules.pro

-ignorewarnings

-keepattributes Signature

-keepattributes *Annotation*

-dontwarn sun.misc.**

-keep class * implements com.google.gson.TypeAdapterFactory
-keep class * implements com.google.gson.JsonSerializer
-keep class * implements com.google.gson.JsonDeserializer
-keep class com.google.gson.** { *; }
-keep class com.loopj.android.** { *; }
-keep interface com.loopj.android.** { *; }

-keep class com.google.android.gms.maps.** { *; }
-keep interface com.google.android.gms.maps.** { *; }
-keepclassmembers class com.google.android.gms.maps.model.LatLng

Without AndroidX the aar library worked without problems and without other ProGuard rules.

Are there some extra steps to do to make AndroidXm DataBinding and code obfuscation works together?

Thanks in advice.

1 Answers

Try to add these Proguard rules:

-keep class androidx.databinding.** { *; }
-keep class * extends androidx.databinding.DataBinderMapper { *; }
Related