JavaScript Bridge function in WebView not invoked

I encountered a strange issue with my application, which functions as an e-reader. To invoke functions for the web view, I utilize a JavaScript class:

    public class MyJavaScriptInterface {
        Context mContext;

        /* Instantiate the interface and set the context */
        MyJavaScriptInterface(Context c) {
             mContext = c;
        }

        @JavascriptInterface
        public void functionToCall(String input) {
             System.out.println("this function was called: "+ input);
        }
  }

I have a customized web view where I incorporate my JavaScript class.

  public class newBTWebView extends WebView implements {
       public newBTWebView(Context context) {
         super(context);
         init(context);

       }
       public void init(Context context) {
         System.out.println("newBTWebview init");
         this.context = context;
         this.getSettings().setJavaScriptEnabled(true);
         setInitialScale(100);
         addJavascriptInterface(new MyJavaScriptInterface(this.context), "HTMLOUT");
     }
 }

In the main activity of my app, I initialize the web view and call the function using JavaScript:

   public class BookReader extends Activity implements WebViewDelegate{
        private static Context mContext;
        private static newBTWebView testWV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        System.out.println("onCreate");
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        testWV = (newBTWebView) findViewById(R.id.mywebview1);
        testWV.setDelegate(this);
        testWV.setWebChromeClient(new WebChromeClient());
        testWV.getSettings().setJavaScriptEnabled(true);
        testWV.getSettings().setPluginState(PluginState.ON);


        testWV.setWebViewClient(new WebViewClient() {

             @Override 
             public boolean shouldOverrideUrlLoading(WebView view, String url)
             {
                 System.out.println("shouldOverrideUrlLoading: " + url);
             }
             @Override
             public void onPageFinished(WebView view, String url) {
                 System.out.println("onPageFinished");
             }
         }
    }
    public void callMyFunction
    {
         System.out.println("callMyFunction");
        testWV.loadUrl("javascript:window.HTMLOUT.functionToCall('myinput');”);
    }
 }

The functionToCall was functioning flawlessly until my recent update. While testing the application on my device, it worked fine; however, after submitting it to the store, the functionToCall stopped working. Even when I tested the APK file on the same device, the function did not get invoked. The function only works while directly running the app from the PC to the device.

I performed tests on various devices such as Samsung S4, Samsung Note 10.1, and Nexus Note, each with different Android versions including 4.4.2, 4.0.4, and 5.0.1.

Answer №1

Did you activate Proguard or any code obfuscation? If so, you need to include keep attributes.

-keep public class com.MyJavaScriptInterface 
-keep public class * implements  com.MyJavaScriptInterface 
-keepclassmembers class * implements com.MyJavaScriptInterface {
<fields>;
<methods>;
}

I am not aware of the fully qualified names of your classes, so please make sure to add them accordingly.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ensure that my program is halted until the xmlhttprequest has completed

It seems like I've overcomplicated this problem for myself. In my page, I have 2 XMLHTTPRequests - the first request retrieves data from an API and fills my elements with this data. However, one of the fields in my elements requires a second request t ...

Using AngularJS to automatically include predefined data in an $http request

I am looking for a way to include a default data attribute in an http request. I came up with the following code: var app = angular.module('dumbhttp', []); app.service('dumbhttp', function ($http, $location) { this.post = function ...

Stop the slider when a video pops up

Years ago, I had a slider created for me that supports both images and video with a timer. However, the issue I'm facing is that when the timer runs every 10 seconds, the video gets cut off if it's not finished playing. Below is the json file st ...

What could be causing the script to display the object's content inaccurately?

Below is the code for the client side: import {useEffect,useState} from 'react'; import io from 'socket.io-client'; import Peer from './Peer'; export default function TestMeeting(){ let peerName; const [peerList,setPee ...

Setting up environments utilizing restify

Having experience with gulp, I am well-versed in setting up a distinct configuration for each environment within a single configuration file. This allows for running gulp dev to initiate the server using the development environment configuration, gulp stag ...

What is the best way to achieve a precision of 6 decimal places in JavaScript when working with decimals?

While working on coding to round numbers to six decimal places after performing some arithmetic operations, I encountered a problem. I was iterating through the elements of an array and conducting calculations based on the array contents. To achieve roundi ...

Is there a way to determine the app bar height within a React Material UI application?

I'm trying to create a full-screen image for the opening of my website using React and Material UI. Here's a snippet of my JSX code with the default Material UI theme: <AppBar> //code in between </AppBar> <Container sx={{margin: ...

Using HTML5 video with cue points and stop points

I've noticed that there are various options available for implementing cuepoints in HTML5 videos, such as using PopcornJS or CuepointsJS to trigger play events at specific times within the video track. However, I am wondering if there is a solution t ...

Creating a custom URL following a redirect in Contact Form 7 to ensure a unique

This is the code I am currently using: <script type="text/javascript> document.addEventListener( 'wpcf7mailsent', function( event ) { location = 'https://www.example.com/thank-you/'; }, false ); </script> With this ...

How to Hide Validation Messages Automatically Using Knockout on Page Load

I have been facing a persistent issue where error messages are displaying onload and I want them to appear only on save. Although I have been successful in many cases and can adjust my code accordingly, this particular bug seems to be causing continuous pr ...

I'm having trouble getting $.getJSON to function properly

Has anyone encountered issues with the getJSON function in jQuery? function loadJSON(){ console.log("loading JSON") var jsonFile = themeURL+"/map.json" $.getJSON(jsonFile, function(data){ console.log("loaded JSON") $("#infobox").fadeOut(1000 ...

Using the spread operator to modify an array containing objects

I am facing a challenge with updating specific properties of an object within an array. I have an array of objects and I need to update only certain properties of a single object in that array. Here is the code snippet I tried: setRequiredFields(prevRequir ...

The keyboard doesn't appear when using autocompletetextview

I am currently working on an Android application that utilizes fragments and includes an AutoCompleteTextView. I want the keyboard to automatically appear when the fragment is displayed, but so far my attempts have been unsuccessful. Below is an excerpt o ...

Nested Promise.all within another Promise.all appears to terminate prematurely, triggering a warning indicating failure to return from a promise

I am utilizing this function to be invoked within another Promise.all. However, I consistently encounter a warning message: Caution: a promise was generated in a handler but was not returned from it. Additionally, the function deleteFutureAppointments() ap ...

`vue.js: li element tag numbers not displaying correctly`

Issue with Number list not displaying correctly using Vue.js I have tried sending it like this. Is there a fix for this problem? This is the code snippet I used to output in Vue.js: p(v-html="selectedProduct.description") Snapsho ...

Ways to showcase a JSON menu with a single level

I have a json file containing links to all the images in a specific folder, as shown below: ["http://img1.png","http://img2.png","http://img3.png","http://img4.png"] I would like to create a <ul> list using this data, but I'm not sure how to d ...

jQuery DataTables covering a CSS-anchored menu bar

My webpage has a pinned navigation menu bar at the top and some tables with interactive features using jQuery's DataTables. However, after implementing jQuery, I encountered an issue where the tables cover the menu when scrolling down, making it uncli ...

Check if the input value was chosen by pressing Enter instead of clicking on it

There is an input tag with the following attributes: <input type="text" name="cOperator" class="form-control scale-input operator" placeholder="Enter your ID" autocomplete="off" onkeyup="ajax_showOptions(this,'getEmp',event)" required> ...

The authentication callback function fails to execute within Auth0 Lock

I'm having an issue with logging into my application using Auth0. I have integrated Auth0 Lock version 10.3.0 through a CDN link in my project and am utilizing it as shown below: let options = { disableSignupAction: true, rememberLastLogin: f ...

What is the best way to include a string in an Ajax GET request as a query parameter without it being encoded?

I've encountered an issue while trying to pass a list of subject IDs as query params in an Ajax get-request. The API expects the subjectId param to be either a single number or a string of numbers separated by commas. I have made sure that what I am s ...