问题描述
这是我想记录的上述错误的解决方案.我发现了其他类似的帖子,但没有一个描述此错误如何与 Cordova 或 Ionic 相关联.
This is a solution to the above error that I want to document. I found other similar posts, but none described how this error can be associated with Cordova or Ionic.
如果您不小心,Android Studio 使用的 Gradle 版本与 Cordova/cordova-android 在其自动生成的应用程序代码中指定的 Gradle 版本之间可能存在不匹配.如您所知,运行
If you are not careful, there can be a mismatch between the version of Gradle that Android Studio uses and the version of Gradle that Cordova / cordova-android specifies in its auto-generated application code. As you know, running
$ cordova platform add android
(或 $ ionic platform add android
,如果您正在构建一个 Ionic 应用程序)在 -project/platforms/android.js 中创建本机应用程序代码.
(or $ ionic platform add android
, if you are building an Ionic app) creates the native application code at the-project/platforms/android.
在该文件夹中,文件:/the-project/platforms/android/cordova/lib/builders/GradleBuilder.js 导出如下所示的变量:
Inside that folder, the file: /the-project/platforms/android/cordova/lib/builders/GradleBuilder.js exports a variable as shown below:
var distributionUrl = process.env['CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL'] || 'http\\://services.gradle.org/distributions/gradle-x.y-all.zip';
其中 x 和 y 取决于用于构建本机应用程序代码的 Cordova/cordova-android 版本.
Where x and y depened on which version of Cordova / cordova-android are being used to build the native application code.
跑步时
$ cordova build android
distributionUrl
变量中指定的 Gradle 版本是用于构建的版本.
The version of Gradle specified in the distributionUrl
var is the version used for the build.
现在是棘手的部分.当您将项目导入 Android Studio 时,您很可能会收到一条强烈建议您将 Gradle 升级到更新版本的消息,如下所示:
Now here comes the tricky part. When you import the project into Android Studio, you will most likely get a message strongly recommending that you upgrade Gradle to a newer version, as shown below:
如果这样做,Android Studio 将下载新版本的 Gradle 并将其存储在本地,并将项目配置为使用新下载的本地 Gradle 发行版,即选中的使用默认等级包装器"下方的单选选项,我最终选择了它取消选择,因为这会导致错误.
If you do this, Android Studio will download a new version of Gradle and store it locally and configure the project to use the newly download local Gradle distribution, which is the radio option below the selected "Use default grade wrapper", which I ended up deselecting because this will cause errors.
这会导致问题,因为 Android Studio 和 Cordova 现在将尝试使用不同版本的 Gradle 构建应用程序,您将在 Android Studio 中以及使用
This will cause problems because Android Studio and Cordova will now be attempting to build the application with different versions of Gradle and you will get build errors within Android Studio and also with
$ cordova build android
在命令行中.Cordova 应用程序的解决方案是始终将 Android Studio 项目设置为使用默认 gradle 包装器"并忽略升级的诱人消息.如果您确实想使用较新版本的 Gradle,您可以随时更改上述文件中的 distributionUrl 变量(但是 Cordova 强烈建议不要修改平台文件夹中的代码,因为它很容易被覆盖).在撰写本文时,我不知道是否有办法在
in the command line. The solution with Cordova apps is to always keep the Android Studio project set to "Use default gradle wrapper" and ignore the tempting messages to upgrade. If you do want to use a newer version of Gradle, you can always change the distributionUrl var in the file mentioned above (however Cordova strongly discourages modifying code within the platforms folder since it is easily overwritten). At the time of writing this, I cannot tell is there is a way to set the Gradle version at the
$ cordova platform add android
步骤,这是您想要这样做的时候,因此您永远不会直接修改项目/平台内的代码
step, which is when you would want to do it so you are never directly modifiying code inside of the-project/platforms
推荐答案
对我来说,以下命令解决了这个问题:
For me, the following commands solved the problem:
cordova platform remove android
cordova platform add android
ionic build android
这篇关于Cordova/Ionic 构建 android Gradle 错误:支持的最低 Gradle 版本为 2.14.1.当前版本是 2.13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!