@JavascriptInterface cannot be resolved
Asked Answered
M

3

20

I'm working with Android to make a webpage interact with my app. As such, I've gone through the documentation and tutorials and ended up coming up with this site. In it, the developers list that you should include @JavascriptInterface before any function you wish to be accessible by the WebView and that without it, Jelly Bean won't recognize the function.

My problem is that when I put that in, I get an error saying:

@JavascriptInterface cannot be resolved to a type

Without it, my code compiles and works fine, but I want Jelly Bean compatibility. I'm currently working on Android 1.6 as a base, so does it just not have @JavascriptInterface? Is that a Jelly Bean specific thing, meaning I'll have to make a program specifically for Jelly Bean? Any help would be greatly appreciated. Here is my complete interface class:

import android.content.Context;

public class WebAppInterface{

    Context mContext;

    /** Instantiate the interface and set the context */
    WebAppInterface(Context c) {
        mContext = c;
    }

    /** Show a toast from the web page */
    //needed for jelly bean
    @JavascriptInterface
    public void turnOff() {
        MyAlarmPopup.turnOff();
    }

}
Milliner answered 16/1, 2013 at 7:10 Comment(0)
J
37

@JavascriptInterface is introduced in JellyBean.

You need to set the Project Build Target to API Level 17

From Eclipse IDE, go to Project->Properties. Select Android and set the target

The APK will work on older versions of android.

Jeanene answered 2/2, 2013 at 0:59 Comment(2)
@nimmy how can i do this for lower API level ??Piazza
for lower API levels, do not add @JavascriptInterface. It will still work fine.Scaffold
F
23

You need to add this:

import android.webkit.JavascriptInterface;

Check the sample here:

Fulgurous answered 21/2, 2013 at 6:13 Comment(2)
this only gets available when we set the target to API level 17 like the answer above.Cenogenesis
This works very fine as stated and should be the accepted correct answer. the comment stating that you need to set target API level 17 is incorrect, as there is no need.Scaffold
H
0

Steps for users of Android Studio:

  1. Check that in build.gradle the minSdkVersion is 17 or above
  2. Add import android.webkit.JavascriptInterface;
Hindman answered 18/7, 2018 at 7:43 Comment(3)
A short answer is welcome, however it won't provide much value to the latter users who are trying to understand what's going on behind the problem. Please spare some time to explain what's the real issue to cause the problem and how to solve. Thank you ~Dogear
A bit more detailed explanation for those wondering why you have to make the above steps:Hindman
1. is needed because you need a min sdk level of 17, but in AS, unlike in eclipse, the minsdkVersion is set in build.gradle and not in manifest file. 2. import has to be done manually because in the current Android Studio version 3.1.3 the auto import does not do importation for the annotation with the shortcut alt + enter, and I dont see a way to set it in the File -> Settings -> Editor -> General -> Autom Import menu.Hindman

© 2022 - 2024 — McMap. All rights reserved.