ご存じのとおり、JELLY_BEAN 以来、JavasScriptInterface@JavascriptInterface
を介してアクセス可能なすべてのメソッドで注釈を付ける必要があります。
私はいくつかの JavaScript を注入しながらそれを行っており、API>JELLY_BEAN_MR1 を使用して端末で正常に動作しています。それにもかかわらず、古いデバイスには NoClassDefFoundError があります。
これは私のコードです:
wvPromo.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
if(mDialog!=null && mDialog.isShowing()) mDialog.dismiss();
view.loadUrl("javascript:(function() { "
+ "var myEl = document.getElementById('mw-mf-main-menu-button'); "
+ "myEl.addEventListener('click', function(){window.JSInterface.share();}, false);"
+ "})();");
view.addJavascriptInterface(this, "JSInterface");
}
});.
...
@JavascriptInterface
public void share() {
Log.i(getClass().getSimpleName(), "Share button");
Intent i = Utils.shareContentIntent(this, "word", "Title");
startActivity(i);
}
これは例外です:
01-30 12:42:41.750: E/AndroidRuntime(13662): FATAL EXCEPTION: main
01-30 12:42:41.750: E/AndroidRuntime(13662): java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.cleverox.flamencomovil/com.zonaapp.flamencomovil.PromotionActivity}: com.google.inject.internal.util.$ComputationException: java.lang.NoClassDefFoundError: android.webkit.JavascriptInterface
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.os.Handler.dispatchMessage(Handler.java:99)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.os.Looper.loop(Looper.java:130)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread.main(ActivityThread.java:3691)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.Method.invoke(Method.java:507)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
01-30 12:42:41.750: E/AndroidRuntime(13662): at dalvik.system.NativeStart.main(Native Method)
01-30 12:42:41.750: E/AndroidRuntime(13662): Caused by: com.google.inject.internal.util.$ComputationException: java.lang.NoClassDefFoundError: android.webkit.JavascriptInterface
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.FailableCache.get(FailableCache.java:50)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.MembersInjectorStore.get(MembersInjectorStore.java:65)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.InjectorImpl.getMembersInjector(InjectorImpl.java:950)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.InjectorImpl.getMembersInjector(InjectorImpl.java:957)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.InjectorImpl.injectMembers(InjectorImpl.java:943)
01-30 12:42:41.750: E/AndroidRuntime(13662): at roboguice.inject.ContextScopedRoboInjector.injectMembersWithoutViews(ContextScopedRoboInjector.java:243)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.zonaapp.utils.roboguice.RoboSherlockActivity.onCreate(RoboSherlockActivity.java:60)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.zonaapp.flamencomovil.PromotionActivity.onCreate(PromotionActivity.java:40)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
01-30 12:42:41.750: E/AndroidRuntime(13662): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
01-30 12:42:41.750: E/AndroidRuntime(13662): ... 11 more
01-30 12:42:41.750: E/AndroidRuntime(13662): Caused by: java.lang.NoClassDefFoundError: android.webkit.JavascriptInterface
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.Method.getDeclaredAnnotations(Native Method)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.Method.getDeclaredAnnotations(Method.java:262)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.AccessibleObject.getAnnotations(AccessibleObject.java:188)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.reflect.AccessibleObject.getAnnotation(AccessibleObject.java:196)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.spi.InjectionPoint.getAtInject(InjectionPoint.java:466)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.spi.InjectionPoint.getInjectionPoints(InjectionPoint.java:664)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.spi.InjectionPoint.forInstanceMethodsAndFields(InjectionPoint.java:356)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.MembersInjectorStore.createWithListeners(MembersInjectorStore.java:90)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.MembersInjectorStore.access$000(MembersInjectorStore.java:34)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:42)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:39)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.FailableCache$1.apply(FailableCache.java:39)
01-30 12:42:41.750: E/AndroidRuntime(13662): at com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
01-30 12:42:41.750: E/AndroidRuntime(13662): ... 23 more
01-30 12:42:41.750: E/AndroidRuntime(13662): Caused by: java.lang.ClassNotFoundException: android.webkit.JavascriptInterface in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.cleverox.flamencomovil-1.apk]
01-30 12:42:41.750: E/AndroidRuntime(13662): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
01-30 12:42:41.750: E/AndroidRuntime(13662): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
01-30 12:42:41.750: E/AndroidRuntime(13662): ... 36 more