Reputation: 5390
I have a very weird problem, my application is an e-reader. to call functions for the web view I use 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 add 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");
}
}
and in my main activity I initiate 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 working perfectly, lately in my latest update, as I was testing the application on the device it was still working, but when I submit it on store the functionToCall was not getting called any more. I tested the apk file on the device the functionToCall is not getting called. It only works when I run the app directly from the pc to the device, but using the apk file on the same device as mentioned before the functionToCall doesn't get called.
I have tested on Samsung S4, Samsung Note 10.1, and nexus note. with android 4.4.2, 4.0.4 and 5.0.1.
Upvotes: 0
Views: 1200
Reputation: 2077
Have you enabled proguard or some code obfuscation ?if yes you should add keep attributes
-keep public class com.MyJavaScriptInterface
-keep public class * implements com.MyJavaScriptInterface
-keepclassmembers class * implements com.MyJavaScriptInterface {
<fields>;
<methods>;
}
i do not know your classes full qualified names. so please put your fully qualified names.
Upvotes: 3