Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript interface not working with android 4.2

Tags:

I am facing a serious bug of android OS Jelly Bean 4.2, here in my program I am using JavaScript to get the height of webpage and it is perfectly working from android 2.2 to 4.1 but when I tried to use the same code in android 4.2 so the JavaScript interface not working.

How can I fix this issue?

like image 757
Ravi Avatar asked Dec 08 '12 06:12

Ravi


2 Answers

From the Android 4.2 documentation:

Caution: If you've set your targetSdkVersion to 17 or higher, you must add the @JavascriptInterface annotation to any method that you want available your web page code (the method must also be public). If you do not provide the annotation, then the method will not accessible by your web page when running on Android 4.2 or higher.

Source: Android WebView Doc

like image 175
jlovison Avatar answered Nov 23 '22 20:11

jlovison


And, if you are using Proguard, remember to add this

-keepclassmembers class * {     @android.webkit.JavascriptInterface <methods>; }  -keepattributes JavascriptInterface -keep public class com.mypackage.MyClass$MyJavaScriptInterface -keep public class * implements com.mypackage.MyClass$MyJavaScriptInterface -keepclassmembers class com.mypackage.MyClass$MyJavaScriptInterface {      <methods>;  } 

If it's still not OK, add this

-keepattributes *Annotation* 

Note: your MyJavaScriptInterface must be Public class

Ref#: Android Proguard Javascript Interface Fail

like image 41
Frank Nguyen Avatar answered Nov 23 '22 22:11

Frank Nguyen