How do you inject build-specific configuration into an APK?
I have an Android app which I distribute to several different market providers; the app is functionally the same for each, but requires certain distinct configuration parameters. Ideally I would like to produce all the individual APKs in one build step. Is there an "official" way to achieve this?
I am considering an approach in which I have a template file in the project structure, and at build time use ant to interpolate values into an output file such as res/xml/config.xml.
Thanks in ad开发者_运维知识库vance!
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.
I use a library project that has all the code and two (or more) projects referencing that library.
The library project has a resource file called config.xml in its res folder and this file is overridden in the other projects. that way I can have different application settings and each build has its own manifest.xml so you can really configure things differently for each build type.
This also allows you to have specific code/permissions/... that is only required by a specific build and makes the build process very easy.
精彩评论