It’s fairly common for Android apps to be released with both a free version, and a paid one with extra features. Quick Quote is one example, and now that it’s been out a little while, I thought I would share some of the tricks and traps that I’ve come across in its development.
Android has a fairly strict security system, that stops apps accessing each other’s data. That’s great when you are storing private information, but not so great when you want to share that data between your free app and your paid one. Fortunately, there is a way to tell Android that it’s safe for your apps to access each other’s data. This is by specifying the same sharedUserID in the manifest files. You also need to ensure that the APKs are signed with the same key. This causes Android to see them as related, and allows you to bridge the gap between them.
You can then call the createPackageContext() method with the package name of the relevant app, in order to obtain a Context for it, and then access its files and resources as normal. A word of warning, however: If the app in question is not installed, createPackageContext() will throw a NameNotFoundException and not return the Context you are after. There are of course, multiple ways to solve this issue. In the case of Quick Quote, I decided to have the paid version look for the free version when it launches, and copy the data over if it has not already been copied.
Now, probably the majority of the code and resources in the different app versions will be the same. Duplication is often a signal that things need a rethink. The Android development tools support the creation of Library Projects as a solution: put the majority of the code in the library project, then include it in the release projects and extend it with any differences.
In the case of inserting advertisements, it’s helpful to know about the <include/> layout element. This lets you directly include another layout file within a layout. This makes it easy to include an activity layout, and add an advertisement without duplicating the whole layout file. It’s also generally useful if you want to use a segment of layout in several places.