22

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?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Ravi
  • 2,277
  • 3
  • 22
  • 37
  • 1
    are you sure that the javascript interface itself is not working, and not the javascript command ( JS parse error) ? – Kiran Kumar Dec 08 '12 at 17:49
  • Actually i am calling java-script function and that is being called in previous version than 17 but not working on android 4.2 api level 17 – Ravi Dec 10 '12 at 03:52
  • Whats the javascript function which you are calling using "loadUrl()" to fetch the height ? – Kiran Kumar Dec 10 '12 at 06:08
  • Yes and i tested that in from 2.2 to 4.1 its is working but not working on android 4.2 – Ravi Dec 10 '12 at 08:28
  • whats the javascript command you are calling ? – Kiran Kumar Dec 10 '12 at 09:02
  • w.loadUrl("java script:window.HTMLOUT.selectedword(function1())"); – Ravi Dec 10 '12 at 09:24
  • If you know the problem please tell me as i told you there is no syntax mistake there it is working perfectly on every android os version from 2.2 to 4.1 – Ravi Dec 10 '12 at 09:26

4 Answers4

46

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

jlovison
  • 1,126
  • 1
  • 10
  • 12
  • 1
    Thanks a lot for this information :D We were stuck since we upgraded our application's API version. – Ethenyl Jul 15 '13 at 12:53
  • 4
    Holy crap! I can't believe how long it too me to find this post. It should throw an error or SOMETHING to let me know what's going on! – Richard Jul 26 '13 at 21:39
  • Supposedly this fixes some security flaw in earlier versions of Android, as describe here - but the example code crashes, in my testing: http://stackoverflow.com/questions/6415882 – NoBugs Aug 31 '13 at 05:40
19

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

Community
  • 1
  • 1
Frank Nguyen
  • 6,493
  • 3
  • 38
  • 37
  • At last, this is the solution. Even adding the first part to proguard.cfg it didnt seem to work (Android 4.3 for Galaxy Note 3), but the key seemed to be the "-keepattributes *Annotation*" part. As soon as I added that to the cfg it started to work right away. Thanks a lot :) – Sobakus Dec 14 '13 at 13:34
  • Oh my god thank you. you saved me from 11 hours of manipulating my code – hadi Jun 13 '16 at 06:46
8

According to the doc: Android doc Since Android 4.2 you have to annotate your method with @JavascriptInterface (and your method has to be public).

Nicolas
  • 81
  • 1
5

Proguard config that works for me with target API 17+:

-keep @interface android.webkit.JavascriptInterface
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}
Andreyul
  • 383
  • 3
  • 8