Using Maven in Android to build for different environments (dev, staging, prod, etc)

If you are working on a relatively complex project, chances are that you may need to build the application against different environments, such as a development, a staging, and a production environment. Assuming you are using Maven as a build tool, here’s one solution how Maven could help with this.

For the sake of this example let assume that you are working on an Android app that consumes web services. You have a dedicated development environment where you spent most of the time, but once every two weeks you need to make a version of the app that is built for production.

Let assume that the endpoint for the testing environment is, and for the production environment is:
But as an aspiring developer and after reading a bunch of books about best practices, you can no longer accept to just go and manually edit the source files by changing all over the place one environment with another, and you ask yourself if Maven could help with this.

It can, and here’s how.
The main idea is to store all the properties, in this particular example all the urls, in separate resource files. Then, using maven resource plugin copy the corresponding property file into values/environment.xml.

1. In your project root directory create following directory structure:

maven environment

2. Then fill the content of the resource files with appropriate values.

<?xml version="1.0" encoding="utf-8"?>
    <string name="url"></string>


<?xml version="1.0" encoding="utf-8"?>
    <string name="url"></string>

3. Add the maven resource plugin to pom.xml by specifying the phase when the copy action should be executed, the resources, and the output directory:

                <phase>validate</phase> <!-- when to execute copy operation -->

4.1 From this point on, you can automate the preparation of environment by creating 2 run configurations:
mvn validate -Denvironment=dev
mvn validate -Denvironment=prod

that will do nothing but copy the appropriate resources from evironment/${environment}/environment.xml, into values/environment.xml

Now, every time you’ll need to prepare a build against a specific environment, the only thing you’ll need to do will be to run one of those commands.

4.2 Another option, to make the things even more compact, is instead of passing the environment like this: -Denvironment=dev, would be to create 2 profiles in pom.xml:



and then create 2 run configurations with appropriate profile:
mvn validate -P development
mvn validate -P prod

For easy access at environment resources you could create a class like this that will return all the environment properties your applications uses:

public class ApplicationEnvironment{
	public String getServerUrl(){
		return resources.getString(R.string.url);

4 thoughts on “Using Maven in Android to build for different environments (dev, staging, prod, etc)

  1. I followed the steps and in terms of build, it builds successfully but one important thing is missing:
    In my code I want to call use this string and the way that I call this url is like bellow:
    String myurl = getResources().getString(R.string.myurl);
    But the compiler gives me compile error because there is not such myurl string in my string file yet. When project is built it will add it.
    So my problem is: How can I call the url string or any url that we define in environent.xml

    1. I guess the values/strings.xml should be populatet with your default environment data, for example “dev”, to prevent compilation errors.
      When you will build against another environment, eg.: prod, the values in strings.xml will be overriten.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s