Search code examples
androidcapacitor

Capacitor android plugin not working for some reason


MainActivity.java

public class MainActivity extends BridgeActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        registerPlugin(EchoPlugin.class);
    }

}

EchoPlugin.java

@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);
    }
}

echo.plugin.ts

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

export interface EchoPlugin {
    echo(options: { value: string }): Promise<{ value: string }>;
}

const EchoPlugin = registerPlugin<EchoPlugin>("Echo");

export default EchoPlugin;

After calling the echo method from my typescript, I get no response. It seems as though no code after the plugin gets called gets ran for some odd reason. My project is using capacitor v4. Does anybody have any ideas/suggestions?


Solution

  • In capacitor 4 you have to call registerPlugin(EchoPlugin.class); before super.onCreate(savedInstanceState); instead of after.

    https://capacitorjs.com/docs/updating/4-0#change-registerplugin-order