背景
此前共享过相关内容,见: Android稳定性:可长途装备化的Looper兜底结构
App Crash关于用户来讲是一种最糟糕的体验,它会导致流程中断、app口碑变差、app卸载、用户流失、订单流失等。相关数据显示,当Android App的溃散率超过0.4%的时候,活泼用户有明显下降态势。
项目思路来源于一次发问:有没有方法打造一个永不溃散的app?
继续深挖这个问题后,我们其实有几个问题需求考虑:
- 怎么打造永不溃散的 app
- 当这样做了之后,app 还能正常运转吗?
- 怎样才能在吃掉反常的搭档,让主线程继续运转?
- 反常被吃掉之后会有什么影响?
- 到底什么反常需求被吃掉或者说能够吃掉?
- 吃掉反常能给带来什么好处?是否能对线上问题进行容灾?
这些问题在Android稳定性:可长途装备化的Looper兜底结构都被一一回答了。
怎么完成、怎么更好的完成
完成代码参考 demo 项目:scuzoutao/AndroidCrashProtect
主要的中心代码就两部分:
1. 按装备判别是否需求维护
fun needBandage(throwable: Throwable): Boolean {
if (crashPortrayConfig.isNullOrEmpty()) {
return false
}
val config: List<CrashPortray>? = crashPortrayConfig
if (config.isNullOrEmpty()) {
return false
}
for (i in config.indices) {
val crashPortray = config[i]
if (!crashPortray.valid()) {
continue
}
//1. app 版本号
if (crashPortray.appVersion.isNotEmpty()
&& !crashPortray.appVersion.contains(actionImpl.getVersionName(application))
) {
continue
}
//2. os_version
if (crashPortray.osVersion.isNotEmpty()
&& !crashPortray.osVersion.contains(Build.VERSION.SDK_INT)
) {
continue
}
//3. model
if (crashPortray.model.isNotEmpty()
&& crashPortray.model.firstOrNull { Build.MODEL.equals(it, true) } == null
) {
continue
}
val throwableName = throwable.javaClass.simpleName
val message = throwable.message ?: ""
//4. class_name
if (crashPortray.className.isNotEmpty()
&& crashPortray.className != throwableName
) {
continue
}
//5. message
if (crashPortray.message.isNotEmpty() && !message.contains(crashPortray.message)
) {
continue
}
//6. stack
if (crashPortray.stack.isNotEmpty()) {
var match = false
throwable.stackTrace.forEach { element ->
val str = element.toString()
if (crashPortray.stack.find { str.contains(it) } != null) {
match = true
return@forEach
}
}
if (!match) {
continue
}
}
//7. 相应操作
if (crashPortray.clearCache == 1) {
actionImpl.cleanCache(application)
}
if (crashPortray.finishPage == 1) {
actionImpl.finishCurrentPage()
}
if (crashPortray.toast.isNotEmpty()) {
actionImpl.showToast(application, crashPortray.toast)
}
return true
}
return false
}
2. 完成维护,looper 兜底:
override fun uncaughtException(t: Thread, e: Throwable) {
if (CrashPortrayHelper.needBandage(e)) {
bandage()
return
}
//崩吧
oldHandler?.uncaughtException(t, e)
}
/**
* 让当前线程康复运转
*/
private fun bandage() {
while (true) {
try {
if (Looper.myLooper() == null) {
Looper.prepare()
}
Looper.loop()
} catch (e: Exception) {
uncaughtException(Thread.currentThread(), e)
break
}
}
}
怎么线上容灾
其实思路很简单,问几个问题,答完就知道了。
-
溃散兜底机制能够维护 app,已知我们用装备文件来描述溃散画像,那装备文件能否长途下发?
-
装备文件长途下发后,app 拉下来后能否立即生效?
-
假设线上出了个溃散,溃散本身触及代码流程不重要,但是会让 app 直接挂掉,能否线上修改装备文件,将这个溃散包含进去进行维护,后续鄙人版本修正之?
溃散画像实例
[ { "class_name": "", "message": "No space left on device", "stack": [],
"app_version": [],
"clear_cache": 1,
"finish_page": 0,
"toast": "",
"os_version": [],
"model": []
},
{
"class_name": "BadTokenException",
"message": "",
"stack": [],
"app_version": [],
"clear_cache": 0,
"finish_page": 0,
"toast": "",
"os_version": [],
"model": []
},
{
"class_name": "IllegalStateException",
"message": "not running",
"stack": [
"Daemons"
],
"app_version": [],
"clear_cache": 0,
"finish_page": 0,
"toast": "",
"os_version": [],
"model": []
},
{
"class_name": "",
"message": "Activity client record must not be null to execute",
"stack": [],
"app_version": [],
"clear_cache": 0,
"finish_page": 0,
"toast": "",
"os_version": [],
"model": []
},
{
"class_name": "",
"message": "The previous transaction has not been applied or aborted",
"stack": [],
"app_version": [],
"clear_cache": 0,
"finish_page": 0,
"toast": "",
"os_version": [],
"model": []
}
]