Search code examples
androidcordovaphonegap-cli

build cordova new empty project and target to android-22


I want to set android-22 as target android sdk. I need it as workaroud permission issue with camera on Android 6 and higher. (described here https://developer.android.com/training/permissions/requesting.html, suggested here Workaround for Android 6.0 Permissions ) But my cordova 6.2.1 sets default sdk 23. I have changed it in AndroidManifest.xml

<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" />

built but the apk was the same size as if I buld for android-23. So, to be sure that it is built for android-22, I have removed sdk-23 and left only sdk-22. Build didn't start and Cordova said, I need android-23 sdk

e:\projects\android\qp2>cordova build --release
Error: Please install Android target: "android-23".

Hint: Open the SDK manager by running: "c:\android\android-sdk\tools\android.bat
"
You will require:
1. "SDK Platform" for android-23
2. "Android SDK Platform-tools (latest)
3. "Android SDK Build-tools" (latest)

, so I changed target to android-22 in project.properties and CordovaLib/project.properties as suggested here . Build failed.

E:\projects\android\qp2\platforms\android\CordovaLib\src\org\apache\cordova\CordovaInterfaceImpl.java:191: error: cannot find symbol
        getActivity().requestPermissions(permissions, requestCode);
                     ^
  symbol:   method requestPermissions(String[],int)
  location: class Activity
E:\projects\android\qp2\platforms\android\CordovaLib\src\org\apache\cordova\CordovaInterfaceImpl.java:197: error: cannot find symbol
        getActivity().requestPermissions(permissions, requestCode);
                     ^
  symbol:   method requestPermissions(String[],int)
  location: class Activity
E:\projects\android\qp2\platforms\android\CordovaLib\src\org\apache\cordova\CordovaInterfaceImpl.java:202: error: cannot find symbol
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
                                                       ^
  symbol:   variable M
  location: class VERSION_CODES
E:\projects\android\qp2\platforms\android\CordovaLib\src\org\apache\cordova\CordovaInterfaceImpl.java:204: error: cannot find symbol
            int result = activity.checkSelfPermission(permission);
                                 ^
  symbol:   method checkSelfPermission(String)
  location: variable activity of type Activity
E:\projects\android\qp2\platforms\android\CordovaLib\src\org\apache\cordova\CordovaActivity.java:493: error: method does not override or implement a method from a supertype
    @Override
    ^
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
5 errors

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':CordovaLib:compileReleaseJava'.
> Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
Error: cmd: Command failed with exit code 1

Shortly, my problem is, I don't know how to target other, than newest android-23 platform. Project is empty, just created, with no plugins and no code.

Is it possible to create and build for android-22 in cordova 6.2.1. Should I downgrade cordova?


Solution

  • Is it possible to create and build for android-22 in cordova 6.2.1.

    cordova@6.2.1 (CLI) by default installs cordova-android@5.1 which requires API 23 to be installed via the SDK Manager.

    To build for API 22, you'll need to specifically install cordova-android@4:

    $ cordova platform rm android
      && cordova platform add android@4
    

    To use current versions of Cordova plugins (which also now contains code to support Android 6.0 runtime permissions), you'll need to install cordova-plugin-compat into your project.

    I want to set android-22 as target android sdk. I need it as workaroud permission issue with camera on Android 6 and higher.

    This isn't the best way to work around a runtime permissions issue. By targeting API 22, you're building against an outdated API. While official Cordova plugins provide backwards compatibility to enable building against API 22, you may encounter build errors if you install the latest versions of some 3rd party plugins which explicitly depend on components from API 23.

    Another way to solve your problem would be to use cordova.plugins.diagnostic to manually request the appropriate Android 6.0 runtime permissions that you need by calling requestRuntimePermissions(). After successfully requesting and acquiring runtime permission, you can then invoke the camera functionality. This will allow you to build against API 23 with the latest releases of cordova-android platform, official Cordova plugins and 3rd party plugins.