Capacitor Android Plugin Guide
Building Capacitor plugins for Android involves writing Java or Kotlin to interface with Android SDKs.
Getting Started
To get started, first generate a plugin as shown in the Getting Started section of the Plugin guide.
Next, open echo/android/
in Android Studio. You then want to navigate to the .java
file for your plugin, which changes depending on the Plugin ID and Plugin Class Name you used when creating the plugin.
For example, for a plugin with the ID com.domain.echo
and the Plugin Class Name Echo
, you would find the .java
file at android/src/main/java/com/domain/echo/EchoPlugin.java
.
Using Kotlin
Capacitor uses Java by default but you can use Kotlin instead, if you prefer.
After generating a plugin, right click the Java plugin class in Android Studio and select the "Convert Java file to Kotlin file" option from the menu. Android Studio will walk you through configuring the project for Kotlin support. Once this is completed, right click the Java class again and re-select the conversion option to convert it to a Kotlin class.
Plugin Basics
A Capacitor plugin for Android is a simple Java class that extends com.getcapacitor.Plugin
and has a @CapacitorPlugin()
annotation.
It has some methods with @PluginMethod()
annotation that will be callable from JavaScript.
Once your plugin is generated, you can start editing it by opening the file with the Plugin class name you choose on the generator.
Simple Example
In the generated example, there is a simple echo plugin with an echo
function that simply returns a value that it was given.
This example demonstrates a couple core components of Capacitor plugins: receiving data from a Plugin Call, and returning data back to the caller.
EchoPlugin.java
package android.plugin.test;
import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;
import com.getcapacitor.annotation.CapacitorPlugin;
@CapacitorPlugin(name = "Echo")
public class EchoPlugin extends Plugin {
@PluginMethod()
public void echo(PluginCall call) {
String value = call.getString("value");
JSObject ret = new JSObject();
ret.put("value", value);
call.resolve(ret);
}
}
Accessing Called Data
Each plugin method receives an instance of com.getcapacitor.PluginCall
containing all the information of the plugin method invocation from the client.
A client can send any data that can be JSON serialized, such as numbers, text, booleans, objects, and arrays. This data
is accessible on the getData
field of the call instance, or by using convenience methods such as getString
or getObject
.
For example, here is how you'd get data passed to your method:
@PluginMethod()
public void storeContact(PluginCall call) {
String name = call.getString("yourName", "default name");
JSObject address = call.getObject("address", new JSObject());
boolean isAwesome = call.getBoolean("isAwesome", false);
if (!call.getData().has("id")) {
call.reject("Must provide an id");
return;
}
// ...
call.resolve();
}
Notice the various ways data can be accessed on the PluginCall
instance, including how to check for a key using getData
's has
method.
Returning Data Back
A plugin call can either succeed or fail. Plugin calls borrow method names from JavaScript promises: call resolve()
to indicate success (optionally returning data) and use reject()
to indicate failure with an error message.
The resolve()
method of PluginCall
takes a JSObject
and supports JSON-serializable data types. Here's an example of returning data back to the client:
JSObject ret = new JSObject();
ret.put("added", true);
JSObject info = new JSObject();
info.put("id", "unique-id-1234");
ret.put("info", info);
call.resolve(ret);
To fail, or reject a call, use call.reject
, passing an error string and optionally an error code and Exception
instance
call.reject(exception.getLocalizedMessage(), null, exception);
Persisting a Plugin Call
In most cases, a plugin method will get invoked to perform a task and can finish immediately. But there are situations where you will need to keep the plugin call available so it can be accessed later. You might want to do this to periodically return data such as streaming live geolocation data, or to perform an asynchronous task.
See this guide on saving plugin calls for more details on how to persist plugin calls.
Running Code on Plugin Load
Occasionally, plugins may need to run some code when the plugin is first loaded.
To do this, provide an implementation for the load()
method:
@Override
public void load() {
}
Permissions
If your plugin has functionality on Android that requires permissions from the end user, then you will need to implement the permissions pattern.
Before following this section, make sure you've set up your permission aliases and status interfaces. If you haven't, see the Permissions section in the Web guide.