Search code examples
javascriptandroidannotationsandroid-webviewandroid-4.2-jelly-bean

@JavaScriptInterface to work with both ICS and JB projects


I am running into an issue with @JavaScriptInterface annotation requirement to support api 15 (ICS) and 17 (JB). I have an Android library project which is shared by two application projects. The library project contains WebView-related classes with javascript interfaces. For the application projects, one of them targets api 15 while the other targets api 17. This is our requirement/contraint since we use a customized Android SDK.

The problem is how to make the library project works with both application projects. If the library project targets api 15, it cannot have @JavaScriptInterface annotation which means javascript callback will not work with api 17 application project.

On the other hand, if the library project targets api 17, it will not work with api 15 application project because application project cannot link with library project with higher api level.

Any suggestion on how to have the javascript interface classes in the library project work with both api 15 and api 17 application projects?


Solution

  • Any suggestion on how to have the javascript interface classes in the library project work with both api 15 and api 17 application projects?

    Set your android:targetSdkVersion to be less than 17, as @JavaScriptInterface is not applied until you set android:targetSdkVersion to 17 or higher. Then, simply skip @JavaScriptInterface.