Ionic android build error : “All flavors must now

2019-03-24 19:14发布

问题:

I've started a new project on my computer but I'm not able anymore to build for android

When I run

ionic cordova platform run android

I get this error :

All flavors must now belong to a named flavor dimension. 

回答1:

The "flavours" error will occur if you have the cordova-plugin-crosswalk-webview plugin installed in your project and you try to build with Gradle v4.

However, if you want to upgrade to cordova-android@6.4.0 (e.g. to open your Cordova Android platform project in Android Studio 3 to debug native plugin code), you'll need to upgrade to Gradle v4. Hence cordova-plugin-crosswalk-webview is currently incompatible with cordova-android@6.4.0.

As a manual workaround for the "flavours" error, you can insert a default flavour in the platforms/android/build.gradle (see here):

android { 
    ...
    flavorDimensions "default"
    ...
} 

Update

If you want to build with Gradle v4/cordova-android@6.4+ and cordova-plugin-crosswalk-webview, update to Gradle v4 then install cordova-android off the master branch, since this PR to resolve the "flavours" error with multiple APK builds has now been merged:

cordova platform rm android 
  && cordova platform add https://github.com/apache/cordova-android


回答2:

Found the solution. :)

Gradle version was the problem. I had to downgrade from 4.X to 3.5.1, which workds great!

Check you gradle version by doing

gradle -v

If if's > 4.x, downgrade it!



回答3:

I get the same error and i have created a script to solve it.

As there was said in the answer, the problem is: gradle 4 wants flavorDimensions on gradle.build.

If you check here: https://cordova.apache.org/docs/en/latest/guide/platforms/android/#extending-buildgradle

We can create a file named gradle-extra.build with will be included on gradle.build, we just need to create a script to paste this file on /platform/android/.

So i created this file build-extras.gradle in my project root

android { 
    flavorDimensions "default"
} 

and the script on ./hooks/after_platform_add/copy_build-extras.gradle.js

#!/usr/bin/env node

var fs = require('fs');
var path = require('path');

if(fs.existsSync(path.resolve(__dirname, '../../platforms/android'))) {
  fs.createReadStream(path.resolve(__dirname, '../../build-extras.gradle')).pipe(fs.createWriteStream(path.resolve(__dirname, '../../platforms/android/build-extras.gradle')));
}

You can check about hooks here: https://cordova.apache.org/docs/en/latest/guide/appdev/hooks/index.html

every script under /hooks/after_platform_add will be executed after ionic platform add ***

With this, we don't need to downgrade gradle nor change cordova-android version.