Android Studio: Gradle Product Flavors: Define custom properties

During evaluation phase, Gradle executes all of the code in your android block; it doesn’t just execute the code relevant to the flavors you want to compile. In fact, during evaluation phase, it doesn’t even really know what your flavors are; it has to evaluate that to find out.

So all three of your lines customer = "a", customer = "b", and customer = "c" will get executed.

This is one of the subtle things about Gradle that make it a little difficult to learn.

So I’ve explained why your code isn’t working the way you expect, but this answer is incomplete because I haven’t said a lot about what to do to make it work right, but it’s hard to say what to do because I’m not sure what you’re trying to accomplish. In general I can say that you should think of trying to accomplish what you want using user-defined tasks, and setting up intra-task dependencies to make sure things get executed in the right order. A gotcha with Android Gradle builds is that even those tasks don’t get defined until evaluation phase (it can’t know what tasks it needs to build all your flavors until it’s evaluated the build file and knows what those flavors are), so do some SO sleuthing to see how to hook things onto Android Gradle build tasks — you have to set up your tasks at the end of evaluation phase after the Android plugin has done its thing.

Leave a Comment