Mobile Development 11 min read

Hooking Android Looper Observer for Message Timing Monitoring and Bypassing Hidden APIs

This article explains how to use Android's hidden Looper$Observer API via reflection and dynamic proxies to monitor long‑running messages on the main thread, classify their execution time, and bypass hidden‑API restrictions, with complete code examples and practical tips for Android 10‑14.

Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Rare Earth Juejin Tech Community
Hooking Android Looper Observer for Message Timing Monitoring and Bypassing Hidden APIs

The author shares a practical exploration of Android's internal Looper and its newly introduced Observer interface (available since Android 10) for tracking message dispatch performance on the main thread. By reading the source code, they discovered that the Observer provides callbacks for message start, completion, and exception handling, offering richer information than the older Printer approach.

To hook into this hidden API, the article demonstrates using reflection to obtain the private Looper$Observer class, creating a dynamic proxy that implements the Observer methods, and injecting the proxy into the static sObserver field of Looper . The proxy records timestamps, calculates wait and work durations, and logs the results with different severity levels based on configurable thresholds (10 ms, 50 ms, 100 ms, 300 ms, and above).

public final class Looper { ... }

The Observer interface is defined as follows:

public interface Observer {
    Object messageDispatchStarting();
    void messageDispatched(Object token, Message msg);
    void dispatchingThrewException(Object token, Message msg, Exception exception);
}

Implementation details include generating a unique token for each message, measuring wait time (dispatch start minus message's original timestamp) and work time (dispatch end minus start), and printing a formatted log entry. The logging strategy categorises messages as negligible, worth a glance, needs attention, requires handling, or is critical, based on the measured work time.

Additional sections cover how to bypass hidden‑API restrictions using the FreeReflection library, the impact of Android 14's stricter .dex loading policies (requiring read‑only files), and the correct way to apply the hook helper class in production code, including safety checks to avoid affecting release builds.

public class HandlerLoopHookHelper {
    // hookLooperObserver, unHookLooperObserver, hookMainLooperMessageIdleHandlers, etc.
}

The article concludes with a summary highlighting the advantages of Observer over Printer , the necessity of hidden‑API work‑arounds during development, version‑specific considerations (Android 10+ uses Observer, older versions still rely on Printer), and the requirement to set dynamically loaded .dex files as read‑only on Android 14.

Androidreflectionperformance monitoringLooperObserverHidden API
Rare Earth Juejin Tech Community
Written by

Rare Earth Juejin Tech Community

Juejin, a tech community that helps developers grow.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.