背景: 在Android app运行中,有时一些无关紧要的异常出现时希望App 不崩溃,能继续让用户操作,可以有效提升用户体验和增加业务价值。
新流程:
哪些场景需要Catch
Crash Config配置信息:
支持从网络上获取Crash配置表,动态防护,避免crash。
使用: 在Application onCreate中调用:
CrashPortrayHelper.INSTANCE.init(this);
实现原理—源代码:
CrashPortray.kt
package com.mcd.library.crashProtectimport com.google.gson.annotations.SerializedName
import java.io.Serializabledata class CrashPortray(@SerializedName("class_name")val className: String = "",val message: String = "",val stack: List<String> = emptyList(),@SerializedName("app_version")val appVersion: List<String> = emptyList(),@SerializedName("os_version")val osVersion: List<Int> = emptyList(),val model: List<String> = emptyList(),val type: String = "all",@SerializedName("clear_cache")val clearCache: Int = 0,@SerializedName("finish_page")val finishPage: Int = 0,val toast: String = ""
) : Serializable {fun valid(): Boolean {return className.isNotEmpty() || message.isNotEmpty() || stack.isNotEmpty()}
}
CrashPortrayHelper.kt
package com.mcd.library.crashProtectimport android.app.Application
import android.content.Context
import android.os.Build
import com.mcd.library.AppConfigLib
import com.mcd.library.common.McdLifecycleCallback
import com.mcd.library.utils.CacheUtil
import com.mcd.library.utils.DialogUtil
import java.io.File
import java.lang.reflect.InvocationTargetExceptionobject CrashPortrayHelper {private var crashPortrayConfig: List<CrashPortray>? = nullprivate lateinit var application: Applicationprivate lateinit var actionImpl: IAppprivate const val crashProtectClosed: Boolean = false // 是否关闭该功能fun init(application: Application) {if (AppConfigLib.isDebugMode() || crashProtectClosed) { // debug模式下不进行初始化return}CrashPortrayHelper.application = applicationcrashPortrayConfig = getCrashConfig()actionImpl