Home » Android » java – None of the methods in the added interface have been annotated with @android.webkit.JavascriptInterface; they will not be visible in API 17

java – None of the methods in the added interface have been annotated with @android.webkit.JavascriptInterface; they will not be visible in API 17

Posted by: admin June 15, 2020 Leave a comment

Questions:

I am using a webview and want to load a JScode to show in that webview. However I am getting a problem above API16.It never loads the JS page. It gives me an error:

Here is my code:

        @JavascriptInterface

    public void loadWebviewUrl(){
            Map <String, String> extraEncryptionHeaders = new HashMap<String, String>();
            extraEncryptionHeaders.put("X-APP-DEVICETYPE", "ANDROID"); 
            appVersionname=CommonMethods.getAppVersion(WebPayUActivity.this);
            extraEncryptionHeaders.put("X-APP-APPVERSIONNAME", appVersionname); 
            extraEncryptionHeaders.put("X-DEVICE", Constant.ANDROIDID); 
            extraEncryptionHeaders.put(Constant.HEADER_SECURITY_KEY, Constant.SECURITY_HASH_VALUE);
            mWebView.loadUrl(mPayUrl, extraEncryptionHeaders);

**//getting error in this line that has been mentioned above**
            mWebView.addJavascriptInterface(new Wscript(WebPayUActivity.this),
                    "Android");
            screenDpi=CommonMethods.getResoltuion(this);
            mWebView.setWebViewClient(new MyWebViewClient(this));

            mImageView.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    customAlertDialog();    
                }
            });
            mWebView.setWebChromeClient(new WebChromeClient());
        }

After reading the solutions at different sites I got to know to add annotation. But after writing the annotation, the error is still the same.

Can anyone please check and help me back

Thanks

How to&Answers:

You must add

@SuppressLint("JavascriptInterface")

to the your public methods which you used your webView object.

Answer:

You must add @JavascriptInterface to the public methods of your Wscript class.

If your code is obscured with proguard, you should also add -keepattributes *Annotation* to the configuration file of proguard.