Registering capacitor plugins in java with FragmentActivity

I’m working on a capacitor/electron project. I have written a plugin of my own and also I am using a few community plugins. This is my MainActivity class. I have one big problem with the line that starts out ‘this.init…’ . The error is shown below. How do I get rid of this error?

My second problem is weather or not to try to register the PluginURLPost plugin with an ‘add’ statement. That’s not as big of a problem because I think I can just try it out both ways and whichever works I go with.

    package org.theguy.GptEtc;

    import android.os.Bundle;
    import org.theguy.GptEtc.PluginURLPost;

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

    import java.util.ArrayList;
    import com.getcapacitor.community.speechrecognition.SpeechRecognition;


    public class MainActivity extends BridgeActivity {

        @Override
        public void onCreate(Bundle savedInstanceState) {
            registerPlugin(PluginURLPost.class); // <-- my plugin 
             
            super.onCreate(savedInstanceState); 
            this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{ // <-- how to stop error
                // Additional plugins you've installed go here
                // Ex: add(TotallyAwesomePlugin.class);
                add(SpeechRecognition.class); // <-- community plugin 
                //add(PluginURLPost.class) // <-- this?
            }});
             
        }

    }

I get this error:

    'init()' has private access in 'androidx.fragment.app.FragmentActivity'

I hope this makes sense. Any help would be appreciated.

So I find from googling that the init statement is probably from something you might find in code from before capacitor 3. This is the page:

My code now looks like this:

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

    }

Still, when I run the app on android studio I get this logcat output:

    D/Capacitor: Unable to find a Capacitor plugin to handle requestCode, trying Cordova plugins 2002

I’m not sure, but I think this is part of my problem. Can anyone suggest what I could try?

1 Like

The init method was deprecated on Capacitor 3 and removed in Capacitor 4.
For plugins that you install from npm you no longer need the add sentence because Capacitor 3+ auto register plugins that you have installed from npm (or github, or local path, as long as they are installed using npm install command).

For custom plugins that you have added to your project and are not installed with npm install but part of your native project, you have to use registerPlugin.

thanks. I thought it would be easier to find the documentation for this. It’s out there, I guess, but it’s hard to find.

1 Like