Updating Capacitor to 3.0 in your plugin
There are several required and recommended changes for plugins that are being updated to Capacitor 3.
Planning for a Core API
It is currently difficult for the core team to make changes to the internals of Capacitor without potentially affecting plugins. Because most classes and methods in Capacitor 2 are public for both iOS and Android, we have observed undesired usage of Capacitor APIs that we considered internal.
During Capacitor 3 development, we will be evaluating this problem and creating an official public API for plugins, which will be documented here.
Android
Use the new @CapacitorPlugin
annotation
The @NativePlugin
annotation is deprecated. We now recommend using the new @CapacitorPlugin
annotation, which will allow for the new permissions API.
The name
attribute is the same. The requestCodes
and permissionRequestCode
attributes are removed. The permissions
attribute will need to be replaced with list of @Permission
annotations, each containing a list of manifest strings and their corresponding alias
, which you can omit for now until the new permissions API is implemented in your plugin.
-@NativePlugin(
+@CapacitorPlugin(
name = "FooBar",
- requestCodes = {
- FooBarPlugin.REQUEST_SOME_METHOD,
- FooBarPlugin.REQUEST_SOME_OTHER_METHOD
- },
- permissionRequestCode = FooBarPlugin.REQUEST_ALL_PERMISSIONS,
- permissions = { Manifest.permission.FOO, Manifest.permission.BAR }
+ permissions = {
+ @Permission(strings = { Manifest.permission.FOO }, alias = "foo"),
+ @Permission(strings = { Manifest.permission.BAR }, alias = "bar")
+ })
)
public class FooBarPlugin extends Plugin {
static final int REQUEST_SOME_METHOD = 10051;
static final int REQUEST_SOME_OTHER_METHOD = 10052;
Android request codes
Capacitor 3.0 implements the AndroidX Activity Result API and removes manually defined request codes. Instead of providing a request code and overriding handleOnActivityResult
or handleRequestPermissionsResult
, plugins should provide callback methods using the @ActivityCallback
or @PermissionCallback
annotations. These callbacks can then be referenced when launching a new Activity or Permission request.
-static final int IMAGE_REQUEST = 10052;
@PluginMethod
public void chooseImage(PluginCall call) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
- startActivityForResult(call, intent, IMAGE_REQUEST);
+ startActivityForResult(call, intent, "chooseImageResult");
}
+@ActivityCallback
+private void chooseImageResult(PluginCall call, ActivityResult result) {
+ if (result.getResultCode() == Activity.RESULT_CANCELED) {
+ call.reject("Activity canceled");
+ } else {
+ Intent data = result.getData();
+ // do something with the result data
+ call.resolve("Success!");
+ }
+}
Use WebColor.parseColor() over Color.parseColor()
Android parses hex color strings with an alpha channel as ARGB while in iOS and Web they are parsed as RGBA. If you are sharing colors with alpha channels across platforms be sure to use the new WebColor
utility. WebColor.parseColor()
works similar to the native Android Color.parseColor()
function, but parses the string as RGBA instead.
String colorStringWithAlpha = "#FF000088"; // Semi-transparent red
int color = WebColor.parseColor(colorStringWithAlpha);
If you do not have an alpha channel on your colors both functions will return the same result.
Change default compileSdkVersion and targetSdkVersion to 30
In android/build.gradle
change compileSdkVersion
and targetSdkVersion
default values to 30
.
android {
- compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 29
+ compileSdkVersion project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 30
defaultConfig {
minSdkVersion project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 21
- targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 29
+ targetSdkVersion project.hasProperty('targetSdkVersion') ? rootProject.ext.targetSdkVersion : 30
...
}
...
}