Handling multiple Java source and resources using flavors on Gradle

VanDusen Botanical Garden/Vancouver, Canada

In these days I was faced with a problem: Configure an URL to be used in many versions of an app. To solve this I thought in some possibility:

  1. May I copy manually all files every time when I need to generate an APK with different logics ?
  2. May I override the copy task resources in the build.gradle ?
  3. May I use flavors to handle multiple builds ?

Take your time and think about these options. What do you think about ? Choose one option and think how to implement it before go foward.

Multiple Build using Flavor

All of the choice above will work properly, but one of them need more work than other. In this post I will teach you to do this in an easy way, using flavors.

Tip: Have you ever used the gradle with new instant run ? No ? You should give it a try and update you app right now with this new feature of Android Studio

Configuring the Flavor

I will use the same concept showed before, configuring the path of request using the resources, strings.xml, concept using the flavor. And another one using the java class concept. Both will be configured using the flavor production and development. Let’s first configure the Flavor on Android Studio.

Tip: Before you go forward, you need to download the started project on github.

Configuring the build.gradle

This step will be very easy and very important. Change the perpective of Android Studio and choice the android’ s perpective. Open the build.gradle (Module:app) and insert the code below on tag android {….}

This will actived the multiple build on Android Studio. You can choose the build you want on “build variants” as showed below. This is very important to choose which apk we need to generate.

That’s it. You’ ve configured the structure of your project to accept multiple builds. Now we need to solve two problems.

  1. How can I use multiple builds using the Resources of Android ?
  2. How can I use multiple builds using a java’ s class

Configuring multiple build using resources’ s file strategy

  1. How can I use multiple build using the Resources of Android ?

On Android Studio, change the perspective to project as showed below

Expand it until is showed the option androidTest, main and test as showed below.

Select the src folder and create a folder named production and another one with the name development. These folder will be used by gradle to find the resources stored in the strings.xml.

Next step is create a folder named res/values to mimic the android folder structure.

On project create a new resources’s file named configuration.xml in the on flavor, folder, main and put the key pair as showed below. The resources in the “main” folder will be used as default if they do not exist in the flavor folders.

Create the file configuration.xml in folder development and put the key pair as showed below.

Do the same before and put the key pair below on production , folder, flavor.

Testing the first strategy

Open the class CommunicationController and change the line code final String typeOfFlavor = “”; to accept the value that came from resources and change the signature of method to accept a no empty Context as showed below. Don’t forget to put this context on MainActivity.

Run the example with a valid internet and check the results changing the build variant on Android Studio. When you choice the build variant production the result may be a JSON with content {“message”: “This request came from flavor named production”} and on flavor development the message may be a JSON with content {“message”: “This request came from flavor named developed”}.

Tip: The message will be showed when you click on Button Start Request with resources flavor on app. You can track it using the TAG request_result on logcat

Tip: Need performance on your virtual devices ? Have you ever tried the genymotion ? No, You should give it a try. Check it out on this link.

Play with it changing the build variant and check the result. :D

Configuring multiple build using java class file strategy

  1. How can I use multiple build using a java’ s class ?

On android studio change the perpective to project and create a new folder named java with a packageName lopes.br.final_project.communication.util with a file named Constants as showed below.

In the class Constants on flavor production, put the following code:

In the class Constants on the flavor development, put the following code.

Testing the second strategy

Open the class MainActivity and put the following code on listener javaSetClickListener.

Run and check what is showed when you click on Button Show the value of constant. Don’ t forgot to change the build variant to development or production before run your tests. When you use this strategy it will automatically copy a specific java class according of the choose flavor. This is very usefully when it’s necessary to keep some different logics in many versions.

That’ s it folks. You can adapt these strategy on your project to get this powerful feature. Play with it and if you have any doubt, please let me know. Keep in touch. I’ m writting anothers great posts for you.

Thanks Felipe Vasconcelos for your support.

System engineer with focus on mobile development & blockchain research.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store