Pedro Varela Pedro Varela - 3 months ago 19
Android Question

Different files and gradle android studio

I have a project where I need to generate two versions.
One pointing to development servers and the other to production servers. The previous developers created a configuration file .properties there they have the endpoints set up.
My questions is. How can I modify or generate the two versions with Android Studio. I know about the flavors and build types, but is there a way to keep lets say two .properties files and the compiler use them in each case.

Best regards

Pedro.

Answer

You want to use Gradle flavors. In a nutshell, it allows you to include different resources in different build-time variations (flavors).

In your build.gradle,

android {
  productFlavors {
    flavor1 {
    }
    flavor2 {
    }
  }
}

This will create two different APKS, one for each flavor. Now, in your src tree,

src\
  main\
  flavor1\
  flavor2\

Now for example if your .properties file was stored as an asset, you'd have src/flavor1/assets/foo.properties and src/flavor2/assets/foo.properties.

You can also include different source files, and Android manifests. The directory structure under the flavor folder is the same as you'd find in main and can contain all of the same resources.