Capacitor v3 plugins not working on android build

Viewed 6812

I'm using capacitor v3 beta and there are no problem working in web and iOS but can't run android app. Build is done fine but when running the app appears this error:

E/Capacitor/Console: File: http://localhost/vendor-es2015.js - Line 41296 - Msg: ERROR Error: Uncaught (in promise): Error: "Storage" plugin is not implemented on android
    Error: "Storage" plugin is not implemented on android

To solve this error I've removed the storage plugin and replaced with ionic/storage plugin. But when I use other plugin, for example the Keyboard, the error shows up saying that Keyboard plugin is not implemented on android.

So I suppose that there is some problem with Android builds or project configuration.

These are de node dependencies in my package.json

"@capacitor/android": "^3.0.0-beta.6",
"@capacitor/core": "^3.0.0-beta.1",
"@capacitor/storage": "^0.3.1",

And my capacitor.config.json file

{
    "appId": "net.flowww.me",
    "appName": "FLOWwwMe",
    "bundledWebRuntime": false,
    "npmClient": "npm",
    "webDir": "www",
    "cordova": {}
}

iOS version works well with this configuration.

7 Answers

Storage plugin not worked after Ionic v3 upgrades from v2. It work after manually adding a plugin to MainActivity.java for me:

package com.ionic.app;

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import com.capacitorjs.plugins.storage.StoragePlugin;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    registerPlugin(StoragePlugin.class);
  }
}

you must in mainActivity : add(StoragePlugin.class);

After creating new project and reviewing file differences saw that I have not installed

"@capacitor/cli": "^3.0.0-beta.6"

So I installed it and all compiles successfully.

I also faced the same problem when upgrading from capacitor 2 to 3

As it turned out, I forgot to execute:

npx cap sync android

This solved the problem

In Capacitor's v2 doc, in the page dedicated to Storage Plugin (https://capacitorjs.com/docs/apis/storage) the import is done like:

import { Storage } from '@capacitor/storage';

Then in the Capacitor's v2 doc for Using Plugins (https://capacitorjs.com/docs/v2/apis) you'll find that:

  1. Import the Plugins object. It represents the registry of all Capacitor plugins.
import { Plugins } from '@capacitor/core';
  1. Get a plugin from the Plugin Registry (Plugins object).
const { Browser } = Plugins;
  1. Use the plugin API:
async openBrowser() {
  // On iOS, for example, open the URL in SFSafariViewController (the in-app browser)
  await Browser.open({ url: "https://ionicframework.com" });
}

A common mistake is to import a plugin directly, then use the plugin API >immediately, resulting in the web implementation being used:

import { Browser } from '@capacitor/core';

async openBrowser() {
  // On iOS, for example, this will open the URL in Safari instead of
  // the SFSafariViewController (in-app browser)
  await Browser.open({ url: "https://ionicframework.com" });
}

By using the plugins from the plugin registry (Plugins object), the native implementation of the plugin is used (if available), with fallback to the web version.

So if you're using Quasar with Capacitor v2 you probably gone crazy like me. Just replace Browser with Storage.

Maybe in v3 that problem is solved and that's why legomolina's answer works.

For Capacitor V3 plugins (tested on Android 11 & Ionic 5)

  1. capacitor.plugins.json has the entry for Storage plugin,
  2. MainActivity.java should not have the onCreate function, where CapV3 uses native API,
  3. Try setting minifyEnabled=false in build.gradle.

If error disappears, create pro-guard rules in proguard-rules.pro as in https://github.com/ionic-team/capacitor/issues/739

Related