Multiple targets from one Android source (the better way)

8 Comments

Some of you might have read my article Android: Deploying multiple targets from one project. It describes how to create customized versions of the same software and therefore benefit from multiple apps with the same featureset. That deployment with an Ant script has proven to work well. For example our GMX Mail App is available in four different customizations, for different brands, and uses a similar approach with maven.

However, there is a better way now to handle multiple targets. It is less complex and gives you even more options to customize the different targets. By using an Android Library Project, you still have the benefit of sharing resources and code, without the hacky Ant script. Remember, the Ant script would go through every Java source file and change an import statement, just to resolve the different package name of the R File. Switching between Targets required an Ant build with a refresh of the workspace. Not any more. Now switching between projects is as simple as clicking the run button in Eclipse. Especially for bigger projects this is a huge benefit, because refreshing the workspace can take quite some time.

So what is the new setup? You need to create one base project, with everything common inside, and declare it as an Android Library Project. This option is available under project properties in the Android tab. Then you create the first one of your targets as a different Android Project in Eclipse. On the same properties tab of the new project, you add the base project as a library. Repeat it for another project, which will be your second target. Now you will have somthing similar to this:

For the showcase, I deleted all source files of the custom projects. Since we want to re-use the majority of our sourcecode from the base project, we don’t need any custom-sources right now. There is one little fix we need to apply to the AndroidManifest.xml file. The Android Wizard in Eclipse uses relative references to our Activities. This does not work if we want to use our Activity from the base project, because it uses a different package than our custom project. Therefore, we have to specify the full package name to the specific Activity. In my sample the important part looks like this:

That’s it. You can now overwrite all your base resources and source files in the custom projects. Every new feature developed in the base project is immediately available everywhere. Only if there is a need for updating the AndroidManifest, you have to edit it in all custom projects. But this also means you have a fine grained control over the manifest file.

I updated my old example project on Google Code. Feel free to use it as a start for your own project with multiple targets. Feedback and contributions are always welcome.

8 Replies to “Multiple targets from one Android source (the better way)

  1. This looks like a nice method but i’m just wondering if there’s a way to prevent keeping duplicates of the AndroidManifest.xml for each target. Is there a way to merge them during build? Txn…

  2. Do we have to modify the manifest files of the other projects to match the library project’s?

    I’m trying your example but I keep getting a Null pointer exception when trying to run the ‘sub’ projects saying it can’t find R.string.hello resource even though hello is defined in the library project strings.xml and the ‘sub’ project’s strings.xml

  3. Unfortunately, I haven’t found a good way of preventing duplication of the AndroidManifest.xml. You might use a template and insert the package name at build time. But then it is not easily possible to launch from eclipse.

  4. I used your previous method/script – this works better with SVN 🙂 I have an issue though…. debugging seems to fail. Eclipse can’t find class definitions and dosen’t stop at breakpoints!!?! Have you had the same errors or idea what I missed? Oh by the way, is the base project needed to be library-only meaning do I need two ‘custom’ projects for application A and B? Or can the base project serve as application A?

    Thanks Aero

Comments are closed.

Related Posts

Personal Jenkins Server with Docker

Nowadays, every software development team should have a continuous integration server like Jenkins. This is as true for Android developers as for any other platform. It makes sure the current source code compiles and all the…

Interviewing software engineers

Interviewing basics When interviewing software engineers, you typically want to answer one simple question: Will they be a good fit for my team? Therefore, your interviewing process should focus on answering this question. Obviously both sides try to answer…

Implement a turn based game AI (on Android)

Game AI Developing a game AI can be as much fun as playing, especially when creating your own computer opponent. I am going to present you a simple pattern that works for nearly all turn based game…