1

I am using addJavascriptInterface in my app to popup a share window from Javascript.

Problem is : I can see pop up when I use my mobile in development environment. But the same in not working after publishing to Google and downloading. I am using Android 4.2 Jellybean mobile to test.

I have tried several ways but nothing worked.

---------onCreate----------

wvMainMenu = (WebView) findViewById(R.id.wvMainMenu);

    wvMainMenu.getSettings().setJavaScriptEnabled(true);
    wvMainMenu.getSettings().setLightTouchEnabled(true);
    wvMainMenu.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    wvMainMenu.addJavascriptInterface(new WebappInterface(this), "Android");

-------Inside Javascript--------------

function share_it(id) {

Android.share_this(msg[id]);

}

-----WebappInterface.java--------

@JavascriptInterface public void share_this(String str) {

    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);

    String sharedText = "Text to share " + str;

    sendIntent.putExtra(Intent.EXTRA_TEXT, sharedText);
    sendIntent.setType("text/plain");
    mContext.startActivity(sendIntent);



}
GMaster9
  • 197
  • 1
  • 6

1 Answers1

6

Ok. Nobody answered, but I got the issue resolved myself. When you are using Proguard, your method names get changed. So, while calling them from javascript, due to method name changed, calling method does not work.

So what was happening here, when I was running during development, it was working fine due to no Proguard in development. But when signed apk was generated Proguard was obfuscating the method name and stopping javascript interface to function.

You have two options : 1. Disable Proguard 2. Add exceptions to Proguard

GMaster9
  • 197
  • 1
  • 6
  • 1
    That was a nice clue for me... Following are the lines you have to add to your proguard-rules.pro: -keepclassmembers class * { @android.webkit.JavascriptInterface ; } -keepattributes JavascriptInterface This is another source... http://stackoverflow.com/questions/17629507/how-to-configure-proguard-for-javascript-interface – Tejasvi Hegde Sep 09 '15 at 10:15
  • Just add @Keep annotation. – c-an Mar 03 '21 at 02:06