andrecardoso
andrecardoso

Reputation: 235

Android gradle build flavors with cordova 5.0.0

I tried adding some flavors on build-extras.gradle but after doing that cordova run android has stopped working.

Is it possible to have different build flavors on Android using the new gradle based build system?

Upvotes: 3

Views: 3443

Answers (1)

DelGurth
DelGurth

Reputation: 879

I was facing the same issue and I think I've found a workable solution (even though I rather see a solution within cordova itself).

The problem is that the cordova build script (in the run phase) assumes that if you have multiple APK's (which is the case if you add ProductFlavors) one of them must be architecture specific. Which results in an empty list:

Built the following apk(s):

cordova-app/platforms/android/cordova/node_modules/q/q.js:126 throw e;

^ Error: Could not find apk architecture: x86 build-type: debug

What I've done now is add the following to my build-extras.gradle:

android.variantFilter { variant ->
  def flavor = variant.flavors.get(0).name
  if (project.hasProperty("activeFlavor")) {
    if (flavor != project.getProperty("activeFlavor")) {
      variant.setIgnore(true)
    }
  }
  else {
    if (flavor != "mydefaultproductflavor") {
      variant.setIgnore(true)
    }
  }
}

And I when I don't want my default flavor I need to specify the flavor using:

cordova run android -- --gradleArg="-PactiveFlavor=myotherflavor"

For those that don't know, the -- separator is a special symbol that, by convention, tells the program to stop parsing args afterwards. Cordova states you should use double -- to indicate that these are platform-specific arguments.[1] Nice to know if you need to pass arguments to cordova from ionic. [2]

Unfortunately, the cordova build process doesn't like me switching flavors without a clean (since then I've multiple .apk files again in my output directory). So when you switch first do a ./platforms/android/cordova/clean to remove the old .apk files.

[1] See the note for using flags when signing an app

[2] See for more details the comment of dwieeb on ionic-cli issue 2254.

Upvotes: 1

Related Questions