Core bridge infrastructure that enables communication between web and native layers, manages plugin lifecycle, and provides the foundational runtime for Capacitor Android applications.
The main bridge between JavaScript and native Android code, responsible for plugin management, message handling, and lifecycle coordination.
/**
* Main bridge class providing communication between JavaScript and native Android
*/
public class Bridge {
/**
* Builder pattern for creating Bridge instances with configuration
*/
public static class Builder {
/**
* Create a bridge builder for the given activity
* @param activity - The AppCompatActivity hosting the bridge
*/
public Builder(AppCompatActivity activity);
/**
* Set saved instance state for bridge restoration
* @param savedInstanceState - Android saved instance state bundle
* @returns Builder instance for method chaining
*/
public Builder setInstanceState(Bundle savedInstanceState);
/**
* Add plugins to be loaded by the bridge
* @param plugins - List of plugin classes to register
* @returns Builder instance for method chaining
*/
public Builder addPlugins(List<Class<? extends Plugin>> plugins);
/**
* Set Capacitor configuration for the bridge
* @param config - CapConfig instance with app configuration
* @returns Builder instance for method chaining
*/
public Builder setConfig(CapConfig config);
/**
* Create the bridge instance with all configured options
* @returns Configured Bridge instance ready for use
*/
public Bridge create();
}
/**
* Get the activity hosting this bridge
* @returns AppCompatActivity instance
*/
public AppCompatActivity getActivity();
/**
* Get Android context from the bridge
* @returns Android Context instance
*/
public Context getContext();
/**
* Get the WebView instance managed by this bridge
* @returns CapacitorWebView instance
*/
public CapacitorWebView getWebView();
/**
* Check if the bridge should keep running
* @returns boolean indicating if bridge should remain active
*/
public boolean shouldKeepRunning();
/**
* Execute JavaScript code in the WebView
* @param js - JavaScript code string to execute
* @param callback - Optional callback for execution result
*/
public void eval(String js, ValueCallback<String> callback);
/**
* Load a URL in the WebView
* @param url - URL string to load
*/
public void loadUrl(String url);
}Usage Examples:
// Create a bridge with plugins
Bridge bridge = new Bridge.Builder(this)
.setInstanceState(savedInstanceState)
.addPlugins(Arrays.asList(MyPlugin.class, AnotherPlugin.class))
.setConfig(config)
.create();
// Execute JavaScript
bridge.eval("console.log('Hello from native');", null);
// Load a URL
bridge.loadUrl("https://example.com");Main Android activity that hosts Capacitor applications, managing the bridge lifecycle and providing plugin initialization.
/**
* Base activity class for Capacitor applications
* Extends AppCompatActivity and manages Bridge lifecycle
*/
public class BridgeActivity extends AppCompatActivity {
/** The bridge instance managing web-native communication */
protected Bridge bridge;
/** Flag indicating if the activity should keep running */
protected boolean keepRunning;
/** Capacitor configuration for this activity */
protected CapConfig config;
/** Current activity depth for lifecycle tracking */
protected int activityDepth;
/** List of initial plugins to load */
protected List<Class<? extends Plugin>> initialPlugins;
/** Bridge builder for configuring the bridge */
protected final Bridge.Builder bridgeBuilder;
/**
* Load and initialize the bridge with plugins
* Called after onCreate to set up the Capacitor runtime
*/
protected void load();
/**
* Initialize the activity with plugins
* @param savedInstanceState - Android saved state
* @param plugins - List of plugin classes to register
*/
public void init(Bundle savedInstanceState, List<Class<? extends Plugin>> plugins);
/**
* Override to handle Android back button
* @returns boolean indicating if back press was handled
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event);
/**
* Handle activity results from plugin operations
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data);
/**
* Handle permission request results
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults);
}Usage Examples:
public class MainActivity extends BridgeActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Add your plugins to the initial plugins list
this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
add(MyCustomPlugin.class);
add(AnotherPlugin.class);
}});
}
@Override
protected void onResume() {
super.onResume();
// Custom resume logic
if (bridge != null) {
bridge.getActivity().onResume();
}
}
}Manages plugin loading, instantiation, and lifecycle for the bridge system.
/**
* Manages plugin loading and lifecycle
*/
public class PluginManager {
/**
* Create plugin manager with asset manager for plugin discovery
* @param assetManager - Android AssetManager for reading plugin configurations
*/
public PluginManager(AssetManager assetManager);
/**
* Load plugin classes from configuration
* @returns List of discovered plugin classes
* @throws PluginLoadException if plugins cannot be loaded
*/
public List<Class<? extends Plugin>> loadPluginClasses() throws PluginLoadException;
/**
* Get plugin instance by name
* @param pluginName - Name of the plugin to retrieve
* @returns Plugin instance or null if not found
*/
public Plugin getPlugin(String pluginName);
/**
* Register a plugin instance
* @param pluginName - Name to register the plugin under
* @param plugin - Plugin instance to register
*/
public void registerPlugin(String pluginName, Plugin plugin);
}Classes that integrate Android WebView with the Capacitor bridge system.
/**
* Custom WebView implementation for Capacitor
*/
public class CapacitorWebView extends WebView {
/**
* Create Capacitor WebView with context
* @param context - Android context
*/
public CapacitorWebView(Context context);
/**
* Set up WebView settings for Capacitor
*/
public void setup();
}
/**
* WebView client handling navigation and resource requests
*/
public class BridgeWebViewClient extends WebViewClient {
/**
* Handle page navigation and URL loading
*/
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request);
/**
* Handle resource requests with custom routing
*/
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request);
}
/**
* WebView chrome client for handling JavaScript dialogs and permissions
*/
public class BridgeWebChromeClient extends WebChromeClient {
/**
* Handle JavaScript console messages
*/
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage);
/**
* Handle permission requests from web content
*/
@Override
public void onPermissionRequest(PermissionRequest request);
}Interface for handling communication between the bridge and plugins.
/**
* Interface for handling messages between bridge and plugins
*/
public interface MessageHandler {
/**
* Send response message back to JavaScript layer
* @param call - The original plugin call
* @param successResult - Success result data, null if error
* @param errorResult - Error result data, null if success
*/
void sendResponseMessage(PluginCall call, PluginResult successResult, PluginResult errorResult);
/**
* Send event message to JavaScript layer
* @param eventName - Name of the event to trigger
* @param data - Event data as JSObject
*/
void sendEventMessage(String eventName, JSObject data);
}