Android WebView: Apply the Proxy Override Before Navigation
Use AndroidX WebKit's feature check and completion callback, and account for the override shared by WebViews in one process.
Navigation that depends on a WebView proxy override should start after the override's completion listener runs. Setting it and immediately loading a page leaves the operation racing an asynchronous update.
- Check
WebViewFeature.isFeatureSupported(WebViewFeature.PROXY_OVERRIDE). Support depends on the WebView implementation and AndroidX WebKit library as well as Android's API level. Define what happens when it is absent; silently going direct may defeat the feature. - Build the intended
ProxyConfigusing its documented proxy rules, endpoint syntax and bypass behavior. For permitted public-site navigation, choose a candidate from the free proxy list whose protocol matches a rule supported by that WebView implementation. A PAC URL, SOCKS URI and bare host are different inputs. - Call
ProxyController.setProxyOverride()with that configuration, an executor and a completion listener. Begin dependent navigation from the completion path. - When the temporary override ends, clear it with
clearProxyOverride()and handle its asynchronous completion too.
Give that lifecycle one owner. The setting applies to WebViews throughout the application process; repeatedly changing it cannot give two simultaneous views independent exits. Other processes and HTTP libraries have their own routes.
This supported interface avoids reliance on private proxy-change listeners that vary between Android releases. It selects routing; proxy credentials and certificate trust still use their supported mechanisms. An authentication or TLS failure after selection needs to be diagnosed at that stage.