Cordova / Ionic build android Gradle error: Minimu

2019-03-12 02:50发布

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.

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

(or $ ionic platform add android, if you are building an Ionic app) creates the native application code at the-project/platforms/android.

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';

Where x and y depened on which version of Cordova / cordova-android are being used to build the native application code.

When you run

$ cordova build android

The version of Gradle specified in the distributionUrl var is the version used for the build.

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:

enter image description here 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.

enter image description here

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

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

8条回答
太酷不给撩
2楼-- · 2019-03-12 03:06

Another way to fix issue, that also works on Windows:

cordova build android --release --CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
查看更多
等我变得足够好
3楼-- · 2019-03-12 03:10

For me, the following commands solved the problem:

cordova platform remove android

cordova platform add android

ionic build android
查看更多
唯我独甜
4楼-- · 2019-03-12 03:14

I want to extend @bungler answer.

I got confused with version of gradle & compatible version of android plugin for gradle. Following link has the list of compatibale versions mapping:

Gradle vs Android plugin for gradle - Compatible version list

For current version of gradle 3.3+, compatible version of android plugin is 2.3.3

So final settings will look like following:

For Mac

STEP 1: Add following in env variables:

option a) update env variable:

export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL="https\://services.gradle.org/distributions/gradle-3.3-all.zip"

or

option b ) you can download the gradle file and place it in <project>/platforms/android/gradle/ and update env variable:

export CORDOVA_ANDROID_GRADLE_DISTRIBUTION_URL="../gradle-3.3-all.zip"

STEP 2: Update following in <project>/platforms/android/CordovaLib/build.gradle

buildscript {
  ...
  dependencies {
    classpath 'com.android.tools.build:gradle:2.3.3'
  }
}

This worked for me.

查看更多
Emotional °昔
5楼-- · 2019-03-12 03:15

In follow up to Chuck Holbrooks answer, with following solution I get an error when trying to add android platform again saying it is already added.

ionic cordova platform remove android
ionic cordova platform add android

My working Solution:

ionic cordova platform remove android
ionic cordova platform check android
ionic cordova platform add android 
查看更多
神经病院院长
6楼-- · 2019-03-12 03:22

Switching back to "Use default gradle wrapper" didn't work for me on my ionic 1 project, but running

ionic platform remove android
ionic platform add android

Worked for me

查看更多
我欲成王,谁敢阻挡
7楼-- · 2019-03-12 03:22

For follow error:

Minimum supported Gradle version is 4.1. Current version is 3.3. If using t he gradle wrapper, try editing the distributionUrl in E:\ionic\MyIonicProject\gr adle\wrapper\gradle-wrapper.properties to gradle-4.1-all.zip for gradle 4.1

This command worked for me:

ionic cordova platform update android
查看更多
登录 后发表回答