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

java.lang.NoSuchMethodError: No virtual method requestPermissions([Ljava/lang/String;I)V in class Landroid/app/Activity

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Implemented
    • cordova@7.0.0
    • 7.0.0, 6.3.0
    • cordova-android

    Description

      The problem

      Problem reproduced on Android 5.1.1 (API level 22), on all my devices which below API level 23

      Problematic part of code

      public class HelloWorldBridge extends CordovaPlugin {
      
          String [] permissions = {
                  Manifest.permission.CALL_PHONE,
                  Manifest.permission.INSTALL_SHORTCUT };
          private static int REQUEST_CODE_PERMISSION = 0;
      
          @Override protected void pluginInitialize() {
              onStart();
          }
      
          @Override public void onStart() {
              cordova.requestPermissions(this, REQUEST_CODE_PERMISSION, permissions);
          }
      
          @Override public void onNewIntent(Intent intent) {
              cordova.getActivity().setIntent(intent);
          }
      
          @Override
          public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
              super.onRequestPermissionResult(requestCode, permissions, grantResults);
              if (requestCode == REQUEST_CODE_PERMISSION) {
                  for (int r : grantResults) {
                      if (r == PackageManager.PERMISSION_DENIED) {
                          return;
                      }
                  }
                  cordova.getActivity().runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                         Log.d(TAG, "Granted!");
                      }
                  });
              }
          }
      }
      
      

      Stack trace:

      java.lang.NoSuchMethodError: No virtual method requestPermissions([Ljava/lang/String;I)V in class Landroid/app/Activity; or its super classes (declaration of 'android.app.Activity' appears in /system/framework/framework.jar)
            at org.apache.cordova.CordovaInterfaceImpl.requestPermissions(CordovaInterfaceImpl.java:226)
            at org.mysample.HelloWorldBridge.initDialOnce(DialOnceBridge.java:40)
            at org.mysample.HelloWorldBridge.pluginInitialize(DialOnceBridge.java:27)
            at org.apache.cordova.CordovaPlugin.privateInitialize(CordovaPlugin.java:58)
            at org.apache.cordova.PluginManager.getPlugin(PluginManager.java:171)
            at org.apache.cordova.PluginManager.startupPlugins(PluginManager.java:97)
            at org.apache.cordova.PluginManager.init(PluginManager.java:86)
            at org.apache.cordova.CordovaWebViewImpl.init(CordovaWebViewImpl.java:115)
            at org.apache.cordova.CordovaActivity.init(CordovaActivity.java:149)
            at org.apache.cordova.CordovaActivity.loadUrl(CordovaActivity.java:224)
            at com.dialonce.cordova.sample.MainActivity.onCreate(MainActivity.java:39)
            at android.app.Activity.performCreate(Activity.java:6092)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1112)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2468)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2595)
            at android.app.ActivityThread.access$800(ActivityThread.java:178)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1470)
            at android.os.Handler.dispatchMessage(Handler.java:111)
            at android.os.Looper.loop(Looper.java:194)
            at android.app.ActivityThread.main(ActivityThread.java:5631)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
      

      Investigation

      According docs Activity.requestPermissions this method require Android API 23

      Workaround

      Wrap `requestPermissions` code with `if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)

      { ... }

      `

      Attachments

        Issue Links

          Activity

            People

              bowserj Joey Robert Bowser
              abobrikovich Alex B.
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: