Developing plugins, can't impor to MainActivity.java

I’m having trouble with developing a plugin. I followed this page on the docs, but I must’ve missed something since it’s not working at all.

I’m not very familiar with Java or android development, please let me know if I’ve got something wrong.

I create the plugin just as explained in the guide with the same parameters:

npx @capacitor/cli plugin:generate
✏️  Creating new Capacitor plugin
? Plugin NPM name (kebab-case): my-plugin
? Plugin id (domain-style syntax. ex: com.example.plugin) com.ionicframework.myplugin
? Plugin class name (ex: AwesomePlugin) MyPlugin
? description:
? git repository:
? author:
? license: MIT
? package.json will be created, do you want to continue? (Y/n)

Then I run npm run build.

In my Capacitor app I run npm install "/home/leonardo/Desktop/Node Projects/my-plugin" to locally install my plugin and run npx cap sync:

npx cap sync
✔ Copying web assets from build to android/app/src/main/assets/public in 21.37ms
✔ Copying native bridge in 1.14ms
✔ Copying capacitor.config.json in 1.41ms
✔ copy in 43.39ms
✔ Updating Android plugins in 2.40ms
  Found 1 Capacitor plugin for android:
    my-plugin (0.0.1)
✔ update android in 25.32ms
✔ copy in 248.88μp
✔ update web in 5.94μp
Sync finished in 0.08s

In src/main/java/com/ionicframework/myplugin/MyPlugin.java the Plugin looks like this:

package com.ionicframework.myplugin;

import com.getcapacitor.JSObject;
import com.getcapacitor.NativePlugin;
import com.getcapacitor.Plugin;
import com.getcapacitor.PluginCall;
import com.getcapacitor.PluginMethod;

@NativePlugin()
public class MyPlugin extends Plugin {

    @PluginMethod()
    public void echo(PluginCall call) {
        String value = call.getString("value");

        JSObject ret = new JSObject();
        ret.put("value", value);
        call.success(ret);
    }
}

I then go to my app to make Capacitor aware of the plugin and edit src/main/java/com/ironeko/time/MainActivity.java:

package com.ironeko.time;

import android.os.Bundle;

import com.getcapacitor.BridgeActivity;
import com.getcapacitor.Plugin;

import java.util.ArrayList;

import com.ironeko.time.EchoPlugin;

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

    // Initializes the Bridge
    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      // Ex: add(TotallyAwesomePlugin.class);
      add(EchoPlugin.class);
    }});
  }
}

I also tried importing:

import com.ionicframework.myplugin.EchoPlugin;

Once I run my app in Android Studio however I get this error:

/home/leonardo/Desktop/Node Projects/time-management/android/app/src/main/java/com/ironeko/time/MainActivity.java:10: error: cannot find symbol
import com.ironeko.time.EchoPlugin;
                       ^
  symbol:   class EchoPlugin
  location: package com.ironeko.time

What’s happening? Shouldn’t this work?

Can you give this a try: https://capacitorjs.com/docs/android/troubleshooting#clean-rebuild and sync project with Gradle files https://capacitorjs.com/docs/android/troubleshooting#error-please-select-android-sdk

Sorry for the late response, completely missed the email notification.

I’m not 100% what happened but I think the error was actually caused by Gradle Syncing not functioning correctly. I ended up completely clearing my Android Studio and project and setting it up from nothing and it worked.

When your code is compiled, the compiler needs to work out what each and every identifier in your code means. As the compiler is going through the code it will find something and know what to do with it or not. Cannot find symbol error relates to the identifiers and means that Java cannot figure out what the “symbol” means.

  • You need to check import declarations on class file header.
  • Remember that Java is a case sensitive language. ArrayList is different from Arraylist