Eclipse building Android app: How can I create two

2020-07-24 05:59发布

问题:

I'm writing an Android application and would like to create two versions based on the same code -- a free and a premium version. I have one codebase for both versions with various run-time checks to enable or disable certain features, e.g.

public class MyAppContext extends Application
{
    public static final boolean isPremium = true;
}

// later, in another file....
if (!MyAppContext.isPremium) {
   CoolFeatures.setVisibility(View.GONE);
}

I'm using the latest dev tools (Eclipse Indigo with Google's ADT 16). Right now I follow a fairly cumbersome process of setting isPremium to true in my source file, exporting the app into an APK, then setting it to false and exporting it a second time into a different APK. This is both annoying and error-prone.

Is there a way to automate this process? That is,

1) Create two different build configurations so that when I export my app, two APK files are generated.

2) Have the build configuration affect the code at compile-time -- for example, setting a static boolean to true or false.

Thanks!

回答1:

Take your project and change it into a library project. Then create two applications (free, premium) and import the library. Then you can inherit the MyAppContext object and change the isPremium member as needed, maybe by using an additional setPremium() functions.

Watch out, in the manifest XML you have to use the package name of the library project for each activity, service, etc.



回答2:

Why don't you automate the build with ant. You can use the same target with different parameter (say path to properties file you will read in the java code). You will need to make sure the two apks will be named differently of course, however this is the approach most continuous build tools do it for different environments, which is essentially the same.