Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to emulate Android's productFlavors in a pure java gradle module?

I need three flavors:

  • fake
  • staging
  • prod

fake will provide classes like FakeUser, FakeUserDb - it's very important that these classes are not compiled into the prod flavor.

prod and staging are completely identical, except that I need to compile a different String url into prod vs staging.

So, I need to create an "abstract" real flavor that both prod and staging inherit.

This can be easily done with the android gradle plugin, but how can I do it in a pure java gradle module?

like image 329
ZakTaccardi Avatar asked May 17 '16 14:05

ZakTaccardi


People also ask

How do you use productFlavors?

Creating Product Flavor is pretty much easy in Android Studio, we simply need to add productFlavors block inside the Android block in the application-level build. gradle file. It's that simple to create product flavors.

How do I get the current flavor in gradle?

There is no "current flavor". The Gradle build file is building an object model of the build process. It is not an interpreted script only being used once a build is underway and a "current flavor" is known.

How can I get flavor on my Android?

You can add a config file, say 'Appconfig. java' to your productFlavor specific source folders i.e. src/<productflavor>/java/<packagename>/Appconfig. java ; you can then use Appconfig. PRODUCT_FLAVOR in your app.

What is Android studio gradle?

Android Studio uses Gradle, an advanced build toolkit, to automate and manage the build process, while allowing you to define flexible custom build configurations. Each build configuration can define its own set of code and resources, while reusing the parts common to all versions of your app.


1 Answers

I wrote a gradle-java-flavours plugin to do this:

  • github project
  • JavaFlavoursPlugin.groovy
  • JavaFlavoursPluginTest.groovy
like image 124
lance-java Avatar answered Oct 17 '22 17:10

lance-java