Lustro

class 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

Link copied to clipboard
class Builder

Builder for Lustro; register tabs and apply a DebugConfig, then build.

Link copied to clipboard
object Companion

Factory for Lustro.

Functions

Link copied to clipboard

Returns where the debug server listens and the token that a client sends, or null when the server does not listen: before start, after stop, while the app is in the background, and when the bind failed.

Link copied to clipboard

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:

Link copied to clipboard
fun networkInterceptor(): Interceptor

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.

Link copied to clipboard

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.

Link copied to clipboard
fun stop()

DISARMS the server: removes the lifecycle observer, and closes the socket and the connections of a server that still drains after a background. Idempotent.

Link copied to clipboard
fun webSocketFactory(delegate: WebSocket.Factory): WebSocket.Factory

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: