How do you inject build-specific configuration into an APK? How do you inject build-specific configuration into an APK? android android

How do you inject build-specific configuration into an APK?


There are a couple ways to approach this:

1) Setup an Android Library Application(designed for Eclipse)

This is the method suggested in the Android documentation. With this method, you would setup your application to be a "Library Project".

Then, for each specific build you would like to produce, you would setup a new Android project that references the previously created Library Project. This project would need to have its own AndroidManifest which declares the components used from the library application. Since each application has its own manifest, almost any type of customization can be made by swapping out components or changing information. However, while allowing for complex differences, this does create some redundancy in maintenance if there is few differences.

2) Built an ANT script or other build script to manipulate the configuration

An ANT script could at build time manipulate the sources or XML files to allow for custom configuration. The easiest way to do this would be use the unix sed or a python/ruby script to do the customization. This is ideal where the customization is limited to small changes and/or just textual replacements. This is my route of choice in most cases.

I recommend copying the source tree to a temporary directory prior to making the manipulations such that it can be cleared on an "ant clean" and will not affect the working copy of the source.

One warning with this is that I would design the modifications such that the source still builds without running the script (i.e. so you can do standard builds while developing in eclipse or any other environment without requiring the special script to run).


You could just set up a Maven Android Plugin based build for your apk that uses resource filtering and profiles for your different configurations. Check the morseflash example of the official samples project to see how it is all done in detail.

Once you have the profiles set up you can use the maven invoker plugin to run it all in one sweep. And if you want to have it automated just put the build on Hudson.

http://code.google.com/p/maven-android-plugin/

http://code.google.com/p/maven-android-plugin/wiki/Samples

http://www.simpligility.com/2010/11/release-version-management-for-your-android-application/


I use an ant script, which reads a few build-time parameters and generates the appropriate APK for me (appname-1.1_ggl, appname-1.1_amz.apk which contain links to the Google Market or Amazon Market, respectively). Manually running it twice isn't a big deal.

It may not be realistic, depending upon how many permutations you have (you don't mention), but you could use subant. Using a different buildpath and subsequent build.properties for each permutation you need to call.