[BuglyFix]修正FinalizerWatchdogDaemon线程后台运行时报TimeOutException

This commit is contained in:
renwj
2021-11-22 11:45:10 +08:00
parent 8fd6d6ee74
commit 85fae85d2c
4 changed files with 59 additions and 0 deletions

View File

@@ -124,6 +124,9 @@ ext {
androidxroomruntime : "androidx.room:room-runtime:2.2.3", androidxroomruntime : "androidx.room:room-runtime:2.2.3",
androidxroomcompiler : "androidx.room:room-compiler:2.2.3", androidxroomcompiler : "androidx.room:room-compiler:2.2.3",
androidxroomktx : "androidx.room:room-ktx:2.2.3", androidxroomktx : "androidx.room:room-ktx:2.2.3",
// androidx-lifecycle-process
androidxlifecycleprocess : "androidx.lifecycle:lifecycle-process:2.4.0",
// rxjava2 with room // rxjava2 with room
roomRxjava : 'androidx.room:room-rxjava2:2.2.3', roomRxjava : 'androidx.room:room-rxjava2:2.2.3',
circleimageview : "de.hdodenhof:circleimageview:3.0.1", circleimageview : "de.hdodenhof:circleimageview:3.0.1",

View File

@@ -43,6 +43,7 @@ dependencies {
implementation rootProject.ext.dependencies.rxjava implementation rootProject.ext.dependencies.rxjava
api rootProject.ext.dependencies.mogoaicloudrealtime api rootProject.ext.dependencies.mogoaicloudrealtime
implementation rootProject.ext.dependencies.amapnavi3dmap implementation rootProject.ext.dependencies.amapnavi3dmap
implementation rootProject.ext.dependencies.androidxlifecycleprocess
if (Boolean.valueOf(USE_MAVEN_PACKAGE)) { if (Boolean.valueOf(USE_MAVEN_PACKAGE)) {
implementation rootProject.ext.dependencies.mogoutils implementation rootProject.ext.dependencies.mogoutils

View File

@@ -16,6 +16,7 @@ import com.alibaba.android.arouter.launcher.ARouter;
import com.mogo.aicloud.services.httpdns.IMogoHttpDns; import com.mogo.aicloud.services.httpdns.IMogoHttpDns;
import com.mogo.aicloud.services.httpdns.MogoHttpDnsHandler; import com.mogo.aicloud.services.httpdns.MogoHttpDnsHandler;
import com.mogo.commons.analytics.AnalyticsUtils; import com.mogo.commons.analytics.AnalyticsUtils;
import com.mogo.commons.crash.FinalizeCrashFixer;
import com.mogo.commons.debug.DebugConfig; import com.mogo.commons.debug.DebugConfig;
import com.mogo.commons.device.Devices; import com.mogo.commons.device.Devices;
import com.mogo.commons.network.AllAllowedHostnameVerifier; import com.mogo.commons.network.AllAllowedHostnameVerifier;
@@ -59,6 +60,7 @@ public abstract class AbsMogoApplication extends Application {
public void onCreate() { public void onCreate() {
super.onCreate(); super.onCreate();
sApp = this; sApp = this;
FinalizeCrashFixer.fix();
initARouter(); initARouter();
Utils.init(this); Utils.init(this);
if (shouldInit()) { if (shouldInit()) {

View File

@@ -0,0 +1,53 @@
package com.mogo.commons.crash;
import androidx.lifecycle.Lifecycle;
import androidx.lifecycle.LifecycleEventObserver;
import androidx.lifecycle.ProcessLifecycleOwner;
import com.mogo.utils.logger.Logger;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class FinalizeCrashFixer {
private static void realFix() {
try {
Class<?> clazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon");
if (clazz.getSuperclass() != null) {
Field instance = clazz.getDeclaredField("INSTANCE");
instance.setAccessible(true);
Method isRunningMethod = clazz.getSuperclass().getDeclaredMethod("isRunning");
isRunningMethod.setAccessible(true);
boolean running = (boolean) isRunningMethod.invoke(instance.get(null));
if (running) {
// 只有正在执行的情况下才去调用 stop避免无意义的调用导致产生 Throwable 异常
Method stopMethod = clazz.getSuperclass().getDeclaredMethod("stop");
stopMethod.setAccessible(true);
stopMethod.invoke(instance.get(null));
}
}
} catch (Throwable e) {
e.printStackTrace();
}
}
/**
* 只有线程在运行状态才会关闭线程
*/
public static void fix() {
ProcessLifecycleOwner.get().getLifecycle().addObserver((LifecycleEventObserver) (source, event) -> {
if (event == Lifecycle.Event.ON_START) {
//前后台则重新反射关闭一遍,避免线程被再次开启
Logger.d("FinalizeCrashFixer", "--- 切换到前台 ---");
realFix();
}
if (event == Lifecycle.Event.ON_STOP) {
//前后台则重新反射关闭一遍,避免线程被再次开启
Logger.d("FinalizeCrashFixer", "--- 切换到后台 ---");
realFix();
}
});
}
}