Search code examples
javascriptjavaandroidwebviewandroid-webview

Issue with calling JavaScript interface function in Android WebView


I am encountering a peculiar issue with calling JavaScript interface functions in my Android WebView. I have defined a JavaScript interface class as follows:

public class JavaScriptInterface {
    Context mContext;

    JavaScriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void playSound(int id) {
        // Implementation for playSound method
        doPlay(id);
    }

    @JavascriptInterface
    public void Amir(int id) {
        // Implementation for Amir method
        doPlay(id);
    }
}

I can execute these functions through the WebView using the following code:

Android.playSound(12);

The issue arises when I change the code to call the Amir function like so:

Android.Amir(12);

This results in an error:

Uncaught TypeError: Android.Amir is not a function

I have ensured that the Amir function is correctly defined within the JavaScriptInterface class. Could someone please help me identify the potential cause of this problem and suggest a solution? Thank you.


Solution

  • The problem was with Android studio and after restarting it and making project problem solved.