As clearly noted on official docs, usage of @JavascriptInterface is needed for API level JELLY_BEAN_MR1 and above, to access a java function from the webview side.
This means that Project Build Target must point to API 17 or above which resolves the following import:
import android.webkit.JavascriptInterface;
How does android handles this code for API 16 and below? Will I get a runtime exception or does it ignore this import on runtime?
I'm quite surprised with these answers... they are not accurate. If you add the JavascriptInterface and another annotation lets say MyAnnotation to the same method(like I did), and then try to access the MyAnnotation instance annotation then you are in for a ClassDefNotFoundException surprise!
My solution which seems to work for now (it has been more than a year), is to add the annotation declaration to the application project:
package android.webkit;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface JavascriptInterface {}
This solved the problem on 2.3.5, and still worked on 4.3 and 4.4 and 4.2.
Hope this helps someone else!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With