Lustro
The Lustro debug runtime facade.
Build one with builder, register DebugTabs, and start it to ARM the loopback debug server. Obtain an OkHttp interceptor with networkInterceptor to feed captured traffic into the registered NetworkDebugTab (if any), and a WebSocket.Factory with webSocketFactory to record the messages of the app's WebSockets there.
Debug-only: start refuses to arm when the host app is not marked debuggable, unless DebugConfig.allowNonDebuggableBuilds opts in. This is a runtime backstop for a consumer whose Gradle wiring puts the real runtime on a release variant; it does not replace the debugImplementation / releaseImplementation split with :lustro-noop.
Lifecycle: start registers a process-lifecycle observer and returns LustroStatus.ENABLED once armed. The socket binds when the app is in the foreground (and re-binds on each foreground); when the app goes to the background the server closes the socket, DRAINS in-flight requests, and closes their connections. stop disarms (removes the observer) and closes the socket. Both are idempotent.
Binding: the socket binds to DebugConfig.bindAddress (default 127.0.0.1; 0.0.0.0 opts into LAN exposure) on DebugConfig.serverPort. If that port is taken, DebugConfig.bindFallback controls whether the server retries on an OS-assigned ephemeral port, or tries the same port again for 10 s while the app is in the foreground, as another app's debug server can still hold it for a moment after this app comes to the foreground.
Types
Functions
Returns a NetworkCaptureSink that records the requests of an HTTP client that Lustro has no adapter for, in the registered NetworkDebugTab. Your adapter calls NetworkCaptureSink.beginRequest when the client sends a request, then NetworkCaptureSink.completeRequest with the response, or NetworkCaptureSink.failRequest:
Returns an OkHttp application interceptor that feeds captured traffic into the registered NetworkDebugTab. When no network tab is registered, returns a pass-through interceptor. It returns the pass-through also in a build that start refuses to arm, also when the app never calls start: such a build does not capture, mock, or throttle its requests.
ARMS the debug server: freezes the tab registry and registers the process-lifecycle observer that binds the socket on foreground and drains it on background. Returns LustroStatus.ENABLED once armed, or LustroStatus.DISABLED if arming fails. When the process is already at least Lifecycle.State.STARTED, the socket binds immediately. Idempotent.
Returns a WebSocket.Factory that creates its sockets with delegate, usually your OkHttpClient, and records their messages and lifecycle events in the registered NetworkDebugTab. Create the sockets you want to inspect with it, or give it to a library that takes a WebSocket.Factory: