写了一个 demo 应用,横竖屏切换时应用必然 crash 在 libunity.so 中。
分析原因,看不出任何端倪。
image.png

一通google,受 https://blog.csdn.net/ximsfei/article/details/40518727 这篇文章启发,有可能是 activity 重建,引起的依赖 activity 存在的 unityplayer 内部的错误。
在 AndroidManifest.xml 中加上下面的配置以避免 activity 重建。

1
android:configChanges="mcc|mnc|orientation|screenSize"

再次试验,果然不再 crash 了。

思考,为什么 activity 重建会引起 crash 呢? 应该跟随生命周期才对啊。

一通排查,发现 unity 的 destory 没有绑定到 activity 的 destory 生命周期上。
给加上:

1
2
3
4
5
6
7
@Override
protected void onDestroy() {
super.onDestroy();
if(this.vhSDK!=null){
this.vhSDK.destroy();
}
}

重试,没有再 crash,即使去掉 android:configChanges 配置也不会再 crash。

原因很简单,但是从排查问题的过程来看,属于怎么开脑洞也想不出的问题 —— 要么疯狂 google ,要么研究 unity 源码。故此记录一下,以供后人 google。

☞ 参与评论