混淆导致的数据绑定库错误

问题摘要

AbstractMethodError: abstract method "androidx.databinding.ViewDataBinding androidx.databinding.DataBinderMapper.getDataBinder(androidx.databinding.DataBindingComponent, android.view.View, int)"

堆栈如下:

java.lang.AbstractMethodError: abstract method "androidx.databinding.ViewDataBinding androidx.databinding.DataBinderMapper.getDataBinder(androidx.databinding.DataBindingComponent, android.view.View, int)"
        at androidx.databinding.MergedDataBinderMapper.getDataBinder(MergedDataBinderMapper.java:74)
        at androidx.databinding.DataBindingUtil.bind(DataBindingUtil.java:199)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:130)
        at androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)
        at cn.sintoon.common.adapter.SinRecyclerViewBaseAdapter.onCreateViewHolder(Unknown Source:11)
        at cn.sintoon.common.adapter.SinRecyclerViewBaseAdapter.onCreateViewHolder(Unknown Source:0)
        at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6794)
        at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5975)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
        at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
        at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
        at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3336)
        at android.view.View.measure(View.java:23454)
        at androidx.constraintlayout.widget.ConstraintLayout.internalMeasureChildren(ConstraintLayout.java:1227)
        at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1572)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:143)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at androidx.appcompat.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:403)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:847)
        at android.view.View.measure(View.java:23454)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2954)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1753)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2041)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1636)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7946)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
        at android.view.Choreographer.doCallbacks(Choreographer.java:893)
        at android.view.Choreographer.doFrame(Choreographer.java:812)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
        at android.os.Handler.handleCallback(Handler.java:907)

出现场景

封装了一个类库,向外提供一个封装好的 RecyclerAdapter,使用 DataBinding 。生成类库时开启了混淆,在使用时,抛出异常。

问题原因

androidx.databinding.DataBindingComponent 混淆掉了。

如何修复

修改混淆规则。不混淆这个类。

-keep class androidx.databinding.DataBindingComponent {*;}

排查过程

根据错误堆栈看,是找不到这个方法,初步怀疑就是被混淆掉了方法,然后直接使用不混淆的测试,的确没有异常了。确认是混淆的问题。

然后在 /build/outputs/mapping/ 目录下查看 mapping.txt 文件,发现果然是将这类混淆掉了。

# compiler: R8
# compiler_version: 1.4.77
# min_api: 1
androidx.databinding.DataBindingComponent -> a.a.a:

mapping.txt 这个文件提供混淆前后类、方法、类成员等的对照表。

总结

在基础类库中使用了数据绑定库要配置混淆规则。

参考 Proguard 混淆规则

12-26 04:09