JsBridge: Android Java & JavaScript Communication
JsBridge is a powerful Android library that creates a seamless, two-way communication channel between your native Java/Kotlin code and JavaScript running inside a WebView. It provides a safe, simple, and convenient API to call Java code from JavaScript and vice versa, complete with support for asynchronous callbacks.
This library is ideal for hybrid applications where you need to expose native device capabilities to your web-based UI or trigger web updates from native background tasks.
How It Works
JsBridge injects a JavaScript object (WebViewJavascriptBridge
) into your WebView's window
. This object acts as the intermediary, queuing messages and handling callbacks between the two environments. Communication is managed efficiently and reliably under the hood.
Key Features
- Bi-directional Communication: Call JavaScript functions from Java and Java methods from JavaScript.
- Asynchronous Callbacks: Pass callbacks to your function calls to receive return values asynchronously.
- Persistent Callbacks (New): Maintain long-term communication channels for features like real-time updates or event streams without re-registering callbacks.
- Default Handlers: Set up default message handlers for simplified communication patterns.
- Strongly Typed (via JSON): Pass complex data objects between environments by serializing them to JSON strings (using a library like Gson).
- Easy Integration: Simply use the provided
BridgeWebView
class, a direct subclass of Android'sWebView
.