AndroidStudio RenderProblem UnsatisfiedLinkError导致布局无法预览

最近同事升级了新版SDK后,发现布局文件无法预览了,抛出异常:

java.lang.UnsatisfiedLinkError: video.xxx.Logging.nativeLog(ILjava/lang/String;Ljava/lang/String;)V
    at video.xxx.Logging.nativeLog(Native Method)
    at video.xxx.Logging.log(Logging.java:134)
    at video.xxx.Logging.d(Logging.java:158)
    at video.xxx.EglBase14.isEGL14Supported(EglBase14.java:43)
    at video.xxx.EglBase.create(EglBase.java:96)
    at video.xxx.EglBase.create(EglBase.java:107)
    at video.xxx.rtc.impl.EglSharedContextHelper.getEglSharedContext(EglSharedContextHelper.java:11)
    at com.xxx.rtc.api.RtcView.init(RtcView.java:65)
    at com.xxx.rtc.api.RtcView.onAttachedToWindow(RtcView.java:76)
    at android.view.View.dispatchAttachedToWindow(View.java:19575)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3430)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3437)
    at android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:42)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:335)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:373)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:713)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:844)
    at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)

但是APP运行正常,仅仅是无法预览布局。

我又重现了下,确实如同事所说,只要布局里包含了这个RtcView,就无法预览:

Screen Shot 2021-07-08 at 7.47.54 PM.png

看了下报错堆栈,是RtcView执行onAttachedToWindow时有个初始化操作涉及到了native方法,需要加载so库。正常运行APP的话,加载so库的操作都是一开始就执行完了,所以不会有类似问题。AndroidStudio预览布局文件,居然也会执行到ViewonAttachedToWindow方法,真是又开眼了。有空一定要了解下AndroidStudio预览布局文件的原理。

解决方法就是在RtcView报错的地方加try catch捕获异常。不知道是否还有更优雅的方案。

你可能感兴趣的:(AndroidStudio RenderProblem UnsatisfiedLinkError导致布局无法预览)