Click here to Skip to main content
15,851,242 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a Java plugin to include speech recognition in my Unity Android game. It is activated by touching a button onscreen, and when the button is released, the audio is processed. This feature isn't working, and checking the game logs I found I am getting a ClassNotFoundException related to the plugin's class.

My plugin is a JAR file located at Assets/Plugins/Android inside my Unity project. Here is the code that it contains:

package com.example.unityandroidplswork;

import android.content.Intent;
import android.speech.RecognizerIntent;
import android.speech.SpeechRecognizer;
import android.util.Log;

import java.util.ArrayList;

public class STTPlugin {
    private static final int REQUEST_CODE = 1234;
    private static final String TAG = "SpeechRecognitionPlugin";
    private Activity activity;
    private SpeechRecognizer speechRecognizer;
    private SpeechRecognitionResultListener resultListener;

    public STTPlugin(Activity activity) {

        this.activity = activity;

    public void startSpeechRecognition(SpeechRecognitionResultListener listener) {
        if (SpeechRecognizer.isRecognitionAvailable(activity.getApplicationContext())) {
            resultListener = listener;
            if (speechRecognizer == null) {
                speechRecognizer = SpeechRecognizer.createSpeechRecognizer(activity.getApplicationContext());
            Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
            intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
            intent.putExtra(RecognizerIntent.EXTRA_MAX_RESULTS, 10);
            Log.d(TAG, "Escuchando...");
        } else {
            Log.d(TAG, "Reconocimiento no disponible.");


    public void stopSpeechRecognition() {
        if (speechRecognizer != null) {

    private String[] processRecognitionResults(ArrayList<String> results) {
        return results.toArray(new String[results.size()]);

    public void handleActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            ArrayList<String> results = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
            if (resultListener != null) {
                String[] resultsArray = processRecognitionResults(results);

    public interface SpeechRecognitionResultListener {
        void onResults(String[] results);


And the C# script from which I call the plugin:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using UnityEngine.UI;
using TMPro;

public class SpeechRecog : MonoBehaviour

    private AndroidJavaObject speechRecPlugin;
    public TextMeshProUGUI textofresult;

    // Start is called before the first frame update
    void Start()
        AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
        speechRecPlugin = new AndroidJavaObject("com.example.unityandroidplswork.STTPlugin", activity);

    public void StartSpeechRecognition() {
        AndroidJavaObject speechResultListener = new AndroidJavaObject("com.example.unityandroidplswork.STTPlugin$SpeechRecognitionResultListener", this);
        speechRecPlugin.Call("startSpeechRecognition", speechResultListener);

    public void StopSpeechRecognition() {

    private void ProcessRecognitionResults(string[] results) {
        string recognizedtext = "";
        foreach(string result in results) {
            Debug.Log("Result: " + result);
            recognizedtext += result + " ";
        textofresult.text = recognizedtext;
        //process here for sure

    private class SpeechRecognitionResultListener : AndroidJavaProxy {

        private SpeechRecog plugin;

        public SpeechRecognitionResultListener(SpeechRecog plugin) : base("com.example.unityandroidplswork.STTPlugin$SpeechRecognitionResultListener") {
            this.plugin = plugin;

        public void onResults(string[] results) {

    // Update is called once per frame
    void Update()

What I have tried:

I searched about this problem and I tried proposed solutions such as reimporting the plugin, changing its location, unchecking the release field in the publishing settings... But none of this has worked for me.
Updated 20-Jun-23 23:28pm
Gerry Schmitz 19-Jun-23 13:33pm    
Use C# if Java doesn't work for you.

1 solution

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900