ionic-team / create-capacitor-plugin

Create a new Capacitor plugin ⚡️
Other
74 stars 11 forks source link

Android - Cannot read properties of undefined #104

Closed mmartinez23 closed 3 months ago

mmartinez23 commented 3 months ago

Hello,

I created a custom plugin which is working fine in iOS, but in Android I receive the error 'Cannot read properties of undefined (reading 'getDeviceVolume')'. I followed the guide and also tried a few things that I saw while Googling. Is there something I am missing?

Using @capacitor/plugin@0.12.0

My definitions.ts:

export interface VolumeGetterPlugin {
  echo(options: { value: string }): Promise<{ value: string }>;
  getDeviceVolume(): Promise<{ currentVolume: number; maxVolume: number }>;
}

My index.ts:

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

import type { VolumeGetterPlugin } from './definitions';

const VolumeGetter = registerPlugin<VolumeGetterPlugin>('VolumeGetter');

export * from './definitions';
export { VolumeGetter };

VolumeGetterPlugin.java

package com.domain.plugin.volumegetter;
import android.content.Context;
import android.media.AudioManager;
import android.util.Log;

import com.getcapacitor.JSObject;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.annotation.CapacitorPlugin;
import com.getcapacitor.PluginMethod;

@CapacitorPlugin(name = "VolumeGetter")
public class VolumeGetterPlugin extends Plugin {

    @PluginMethod
    public void getDeviceVolume(PluginCall call) {
        Context context = getContext();
        AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

        int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
        int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);

        JSObject ret = new JSObject();
        ret.put("currentVolume", currentVolume);
        ret.put("maxVolume", maxVolume);
        call.resolve(ret);
    }
}

MainActivity.java in main project:

package io.ionic.starter;

import com.getcapacitor.BridgeActivity;
import android.os.Bundle;
import android.util.Log;

import com.getcapacitor.Plugin;
import com.domain.plugin.volumegetter.VolumeGetterPlugin;
import java.util.ArrayList;

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

    // Initialize the Bridge
    this.registerPlugins(new ArrayList<Class<? extends Plugin>>() {{
      // Add your custom plugins here
      Log.i("Capacitor/Console", "Init Bridge");
      add(VolumeGetterPlugin.class);
    }});
  }
}

app.ts in main project:

import { VolumeGetter, VolumeGetterPlugin } from "volume-getter/src";

ngOnInit(){
    this.getVolume();
}
async getVolume(){
    VolumeGetter.getDeviceVolume().then((result: any) => {
      console.log("Getting Volume", result.currentVolume);
      this.currentVolume = result.currentVolume;
    });
  }
jcesarmobile commented 3 months ago

you shouldn't call registerPlugins for plugins generated with this tool, that's for custom code "plugins" (plugins that live in your project, not in an external package.

Also, in case you call it, you should call it before the super.onCreate, not after.

mmartinez23 commented 3 months ago

you shouldn't call registerPlugins for plugins generated with this tool, that's for custom code "plugins" (plugins that live in your project, not in an external package.

Also, in case you call it, you should call it before the super.onCreate, not after.

Even if I remove it I get the same error, it is working fine on iOS, the only problem is Android and I can't seem to see where the problem is. Even if I use the function that comes with the generated Plugin, meaning echo, it still gives me the error but with "reading 'echo'" instead of the one I created.

jcesarmobile commented 3 months ago

You can ask on the Ionic forums or on github discussions

But without a sample app it's going to be hard to help you.

mmartinez23 commented 3 months ago

Posting this for future reference in case someone sees this. I managed to get it working with the latest release of the plugin. I did not managed to find what was causing the issue exactly, my theory is that is something caused by Visual Studio Code Intellisense, be sure to import it on the main app as "from "custom-plugin"", in my case it was importing it using "from "custom-plugin/src" and I was trying to use it like that.

Thank you @jcesarmobile for your time and help.