Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

App crashes (Databinding Error) after AndroidX Migration

I am trying to migrate a 3 yrs old project to AndroidX. The project have 3 modules.

  • app
  • sdk
  • apis

app is the main module. The project builds AAR file of sdk and apis module on runtime. And the app uses sdk and apis

The changes I did in order to migrate are :

  1. updated target version of app.gradle and gradlewrapper properties
  2. migrated to AndroidX
  3. updated all dependecies of all modules.

As the app starts, it launches an activity from app module which works fine. Just after that it launches an Activity from sdk module. But here the app crashes at DataBindingUtil.setContentView. Databinding is enabled for all 3 modules. Below is the stacktrace.

java.lang.NoClassDefFoundError: Failed resolution of: Lcom/android/databinding/library/baseAdapters/DataBinderMapperImpl;
    at com.xyz.sdk.DataBinderMapperImpl.collectDependencies(DataBinderMapperImpl.java:274)
    at androidx.databinding.MergedDataBinderMapper.addMapper(MergedDataBinderMapper.java:58)
    at androidx.databinding.MergedDataBinderMapper.addMapper(MergedDataBinderMapper.java:60)
    at androidx.databinding.DataBinderMapperImpl.<init>(DataBinderMapperImpl.java:5)
    at androidx.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:32)
    at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284)
    at com.xyz.sdk.landing.LandingPageActivity.onCreate(LandingPageActivity.java:51)
    at android.app.Activity.performCreate(Activity.java:7136)
    at android.app.Activity.performCreate(Activity.java:7127)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.android.databinding.library.baseAdapters.DataBinderMapperImpl" on path: DexPathList[[zip file "/data/app/com.example.admin.myapplication-fYwVmwZfRLEOIXuh0dvhUg==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.admin.myapplication-fYwVmwZfRLEOIXuh0dvhUg==/lib/x86, /system/lib]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at com.xyz.sdk.DataBinderMapperImpl.collectDependencies(DataBinderMapperImpl.java:274) 
    at androidx.databinding.MergedDataBinderMapper.addMapper(MergedDataBinderMapper.java:58) 
    at androidx.databinding.MergedDataBinderMapper.addMapper(MergedDataBinderMapper.java:60) 
    at androidx.databinding.DataBinderMapperImpl.<init>(DataBinderMapperImpl.java:5) 
    at androidx.databinding.DataBindingUtil.<clinit>(DataBindingUtil.java:32) 
    at androidx.databinding.DataBindingUtil.setContentView(DataBindingUtil.java:284) 
    at com.xyz.sdk.landing.LandingPageActivity.onCreate(LandingPageActivity.java:51) 
    at android.app.Activity.performCreate(Activity.java:7136) 
    at android.app.Activity.performCreate(Activity.java:7127) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:193) 
    at android.app.ActivityThread.main(ActivityThread.java:6669) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 
like image 805
Shahbaz Hashmi Avatar asked Dec 31 '22 22:12

Shahbaz Hashmi


1 Answers

I had the same error. If you are working in a multi-module project, you need to enable databinding for the app modules that depend on libraries that use it.

So, my fix was to add this to the module using directly databinding and to my app module too.

buildFeatures {
    dataBinding true
}
like image 197
Max Cruz Avatar answered Jan 10 '23 03:01

Max Cruz