构建 cordova 新的空项目并定位到 android-22

build cordova new empty project and target to android-22

我想将 android-22 设置为目标 android sdk。我需要它作为 Android 6 及更高版本上相机的解决权限问题。 (此处描述 https://developer.android.com/training/permissions/requesting.html, suggested here ) 但是我的 cordova 6.2.1 设置了默认的 sdk 23。我已经在 AndroidManifest.xml

中更改了它
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="22" />

已构建,但 apk 的大小与我为 android-23 构建的大小相同。 因此,为了确保它是为 android-22 构建的,我删除了 sdk-23,只留下 sdk-22。 构建没有开始,Cordova 说,我需要 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)

,所以我按照此处的建议将 project.properties 和 CordovaLib/project.properties 中的目标更改为 android-22。构建失败。

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

很快,我的问题是,除了最新的 android-23 平台,我不知道如何定位其他平台。项目是空的,刚刚创建,没有插件也没有代码。

是否可以在 cordova 6.2.1 中为 android-22 创建和构建。我应该降级科尔多瓦吗?

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

cordova@6.2.1 (CLI) 默认安装 cordova-android@5.1,这需要通过 SDK 管理器安装 API 23。

要为 API 22 构建,您需要专门安装 cordova-android@4:

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

要使用当前版本的 Cordova 插件(现在还包含支持 Android 6.0 运行时权限的代码),您需要将 cordova-plugin-compat 安装到您的项目中。

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

这不是解决运行时权限问题的最佳方法。通过以 API 22 为目标,您正在针对过时的 API 进行构建。虽然官方 Cordova 插件提供向后兼容性以启用针对 API 22 的构建,但如果您安装某些明确依赖于 API 23.[=20 中的组件的某些第 3 方插件的最新版本,您可能会遇到构建错误=]

另一种解决问题的方法是使用 cordova.plugins.diagnostic to manually request the appropriate Android 6.0 runtime permissions that you need by calling requestRuntimePermissions()。成功请求并获取运行时权限后,您就可以调用相机功能。这将允许您使用最新版本的 cordova-android 平台、官方 Cordova 插件和第 3 方插件针对 API 23 进行构建。