In almost every project, we need to configure something for different environments, e.g. different backend URLs. To that problem, I have seen various attempts at a solution to this problem, which, however, primarily required manual changes to the source code or copying and renaming of configuration files.
The next version of the Maven plugin for Android is offering an additional (and much more practical) option: defining of own constants in the BuildConfig class. You have to use the current snapshot (3.6.1-SNAPSHOT) at the moment in order to use this new function.
Since these constants are not configured in the source code directly, but rather in pom.xml, it is possible to redefine the values through various build profiles.
In our example, we simply want to configure the backend URL for development and production differently. Most builds will be built against the development backend, therefore, the development URL is the default value. We create a new profile for production called “production”. In addition to that, we assure that the “production” profile is also used for a release.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://ift.tt/13J5Aq9; xmlns:xsi="http://ift.tt/Atvu06; xsi:schemaLocation="http://ift.tt/IH78KX http://ift.tt/19Bz6pF; [..] <build> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <version>3.6.1-SNAPSHOT</version> <extensions>true</extensions> <configuration> <buildConfigConstants> <constant> <name>BACKEND_URL</name> <type>java.lang.String</type> <value>http://ift.tt/1m3YAB1; </constant> </buildConfigConstants> </configuration> </plugin> </plugins> </build> <profiles> <profile> <id>production</id> <build> <plugins> <plugin> <groupId>com.jayway.maven.plugins.android.generation2</groupId> <artifactId>android-maven-plugin</artifactId> <extensions>true</extensions> <configuration> <buildConfigConstants> <constant> <name>BACKEND_URL</name> <type>java.lang.String</type> <value>http://ift.tt/1m3YAB3; </constant> </buildConfigConstants> </configuration> </plugin> </plugins> </build> </profile> </profiles> </project>
If we simply call “mvn install”, the generated BuildConfig looks as follows:
public final class BuildConfig { public final static boolean DEBUG = true; public final static String BACKEND_URL = "http://ift.tt/1m3YABe;; }
But if we build the project against the “production” profile, the BuildConfig changes
public final class BuildConfig { public final static boolean DEBUG = true; public final static String BACKEND_URL = "http://ift.tt/1wM0fSq;; }
Of course, the plugin also allows to use your own or complex classes as constants.
<buildConfigConstants> <constant> <name>CONFIGURATION_ENUM</name> <type>my.package.MyEnum</type> <value>my.package.MyEnum.CONFIG</value> </constant> </buildConfigConstants>
And that’s it! With only very little effort, we avoid manual changes in our sourcecode and we are able to manage all information in one single place.









