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?

7
  • 1
    are you sure that the javascript interface itself is not working, and not the javascript command ( JS parse error) ? Commented Dec 8, 2012 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 Commented Dec 10, 2012 at 3:52
  • Whats the javascript function which you are calling using "loadUrl()" to fetch the height ? Commented Dec 10, 2012 at 6:08
  • Yes and i tested that in from 2.2 to 4.1 its is working but not working on android 4.2 Commented Dec 10, 2012 at 8:28
  • whats the javascript command you are calling ? Commented Dec 10, 2012 at 9:02

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot for this information :D We were stuck since we upgraded our application's API version.
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!
Supposedly this fixes some security flaw in earlier versions of Android, as describe here - but the example code crashes, in my testing: stackoverflow.com/questions/6415882
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

2 Comments

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 :)
Oh my god thank you. you saved me from 11 hours of manipulating my code
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).

Comments

5

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

-keep @interface android.webkit.JavascriptInterface
-keepclassmembers class * {
    @android.webkit.JavascriptInterface <methods>;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.