Jacob
Jacob

Reputation: 460

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?

Upvotes: 3

Views: 2010

Answers (2)

Florian Cremer
Florian Cremer

Reputation: 139

In my case the registerPlugin was not able to resolve my custom plugin class.

After hours of searching I found out, that I needed to add the dependency of @capacitor/android manually to the dependencies list of the package.json of the client ionic project.

E.g.:

"dependencies": {
 ...
"@capacitor/android": "~4.7.0",
"@capacitor/app": "4.1.1",
"@capacitor/core": "^4.7.0",
...
}

Furthermore it helped to delete the .gradle/caches folder.

Upvotes: 0

jcesarmobile
jcesarmobile

Reputation: 53341

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

Upvotes: 3

Related Questions