Add capacitor speech recognition to android mainactivity

I’m trying to use capacitor speech recognition, they said on android it need this code in order to be working on android

import com.getcapacitor.community.speechrecognition.SpeechRecognition;

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(SpeechRecognition.class);
          }
        }
      );
  }
}

i’ve added it like this

package io.ionic.starter;

import android.os.Bundle;

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

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(SpeechRecognition.class);
                    }
                }
        );
    }
}

but it gives me an error like this

error: package com.getcapacitor.community.speechrecognition does not exist
import com.getcapacitor.community.speechrecognition.SpeechRecognition;

anyone knows how to fix this?

Try adding it via npm, the package isnt installed yet
npm install @capacitor-community/speech-recognition
Run ionic cap sync when everything is done installing

actually i have tried that, and it is available on package.json but it does’t show up on android studio, even i did build or run it still not available on android studioimage

image

and it is added to capacitor.settings.gradle

// DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN
include ':capacitor-android'
project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor')

include ':capacitor-community-speech-recognition'
project(':capacitor-community-speech-recognition').projectDir = new File('../node_modules/@capacitor-community/speech-recognition/android')

include ':capacitor-app'
project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android')

include ':capacitor-haptics'
project(':capacitor-haptics').projectDir = new File('../node_modules/@capacitor/haptics/android')

include ':capacitor-keyboard'
project(':capacitor-keyboard').projectDir = new File('../node_modules/@capacitor/keyboard/android')

include ':capacitor-status-bar'
project(':capacitor-status-bar').projectDir = new File('../node_modules/@capacitor/status-bar/android')

Apparently, remove android folder then add it again using capacitor will fix it

1 Like

Since you are in Capacitor 3, it automatically register plugins, you don’t have to add any plugin to the init method, in fact you have to remove the init method for the automatic plugin registration to work.

Anyway, when a plugin is not found, you probably forgot to run npx cap update after installing the plugin, or if you did, gradle might not find it for some reason, in that case you can use the “Sync Project with Gradle Files” button (elephant icon).

Removing the folder as you did fixed the issue because it runs npx cap update, but removing android folder will undo any manual changes you did in your project.

yes i agree, but i was thinking that npx cap sync runs npx cap copy and npx cap update that is why i didn’t do that. I’ve tried npx cap sync and ionic cap sync still didn’t fix it, but just like node_modules problem sometimes remove the folder fixing the issue, no? cmiiw
also at the Readme.md they still say that it needs to be register on init, they haven’t updated the Readme.md then?

I marked @jcesarmobile answer as a first to go solution, because by removing android folder you may/will lose your edited gradle, manifest etc.

Yeah, if you ran sync it also runs update internally. In that case it’s the gradle thing I mentioned, sometimes you need to hit the “Sync Project with Gradle Files” button (elephant icon) to make the project see the plugins.

Glad to know there’s another way, but unfortunately because of that hassle, i’m using cordova plugin instead and it is working just fine, i wanna try capacitor-speechrecognition too someday. Thanks