Created
August 13, 2018 03:29
-
-
Save kylin17/7a6397022b4ef5c284917b5305f05729 to your computer and use it in GitHub Desktop.
Android stop 'FinalizerWatchdogDaemon' use reflection to avoid 'finalize timed out after 10 seconds' exception.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WorkerThread | |
public static void stopFinalizerWatchdogDaemon() { | |
try { | |
Class<?> watchdogDaemonClazz = Class.forName("java.lang.Daemons$FinalizerWatchdogDaemon"); | |
Class<?> daemonClazz = Class.forName("java.lang.Daemons$Daemon"); | |
Field instanceField = watchdogDaemonClazz.getDeclaredField("INSTANCE"); | |
instanceField.setAccessible(true); | |
Object watchdogDaemonInstance = instanceField.get(null); | |
Method daemonIsRunningMethod = daemonClazz.getDeclaredMethod("isRunning"); | |
daemonIsRunningMethod.setAccessible(true); | |
boolean isWatchdogDaemonRunning = (boolean) daemonIsRunningMethod.invoke(watchdogDaemonInstance); | |
if (isWatchdogDaemonRunning) { | |
Field daemonThreadField = daemonClazz.getDeclaredField("thread"); | |
daemonThreadField.setAccessible(true); | |
Object daemonThreadInstance = daemonThreadField.get(watchdogDaemonInstance); | |
Method daemonStopMethod = daemonClazz.getDeclaredMethod("stop"); | |
daemonStopMethod.setAccessible(true); | |
daemonStopMethod.invoke(watchdogDaemonInstance); | |
// After invoke stop method, Daemon.thread will be set to null, | |
// So let's reassign Thread's member variables. | |
daemonThreadField.set(watchdogDaemonInstance, daemonThreadInstance); | |
} | |
} catch (Exception e) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment