Uploaded image for project: 'Apache Cordova'
  1. Apache Cordova
  2. CB-6890

Android plugins which use pluginManager fields break on 4.0.x branch

    XMLWordPrintableJSON

Details

    Description

      The field CordovaWebView.pluginManager was changed from a public field to a getter, getPluginManager(), for Cordova-Android v4.0.0. (to support pluggable webviews)

      This means that code in plugins like this:

      PluginManager pm = webView.pluginManager;
      

      will break. However, the replacement code,

      PluginManager pm = webView.getPluginManager();
      

      will break on existing 3.x versions of Cordova.

      The solution is to use reflection in the plugin to determine whether the method or the field is available, and to use the appropriate access method to get the plugin manager. This code works in both old and new versions of Cordova:

      Class webViewClass = webView.getClass();
      PluginManager pm = null;
      try {
          Method gpm = webViewClass.getMethod("getPluginManager");
          pm = (PluginManager) gpm.invoke(webView);
      } catch (NoSuchMethodException e) {
      } catch (IllegalAccessException e) {
      } catch (InvocationTargetException e) {
      }
      if (pm == null) {
          try {
              Field pmf = webViewClass.getField("pluginManager");
              pm = (PluginManager)pmf.get(webView);
          } catch (NoSuchFieldException e) {
          } catch (IllegalAccessException e) {
          }
      }
      

      Attachments

        Activity

          People

            iclelland Ian Clelland
            iclelland Ian Clelland
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: