See how Capacitor fits into the entire Ionic Ecosystem ->
Capacitor is part of the Ionic Ecosystem ->

Custom Native Android Code

With Capacitor, you are encouraged to write Java or Kotlin code to implement the native features your app needs.

There may not be a Capacitor plugin for everything–and that’s okay! It is possible to write WebView-accessible native code right in your app.

WebView-Accessible Native Code

The easiest way to communicate between JavaScript and native code is to build a Capacitor plugin local to your app.

First, create a file by opening Android Studio, expanding the app module and the java folder, right-clicking on your app’s Java package, selecting New -> Java Class from the context menu, and creating the file.

Android Studio app package

Copy the following Java code into

package com.example.myapp;

import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

public class MyPlugin extends Plugin {

  public void echo(PluginCall call) {
    String value = call.getString("value");

    JSObject ret = new JSObject();
    ret.put("value", value);

Register the Plugin

Capacitor plugins for Android must be registered in

 // Other imports...
+import com.example.myapp.MyPlugin;

 public class MainActivity extends BridgeActivity {
   public void onCreate(Bundle savedInstanceState) {

     // Initializes the Bridge
     this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
       // Additional plugins you've installed go here
       // Ex: add(TotallyAwesomePlugin.class);
+      add(MyPlugin.class);


Once is created and the class is registered in, the plugin is callable from JavaScript:

import { Plugins } from '@capacitor/core';
const { MyPlugin } = Plugins;

const result = await MyPlugin.echo({ value: 'Hello World!' });

Next Steps

Read the Android Plugin Guide ›

<- Updating
Deploying to Google Play ->
Contribute ->