2

I have an Android app project that contains all of my code. I've made this "app" a Library project as defined here at android.developer.com..

Consequently, I have made two new Android projects which utilize this library project. The new package names for each of the new projects are:

com.myapps.freeapp
com.myapps.paidapp

As you can probably see, I am trying to publish free and paid versions of the app.

What is the best way to tell the library project whether the installed app is the free version or paid version?

dell116
  • 5,835
  • 9
  • 52
  • 70

2 Answers2

5

This is from my blog post that describes how to build a free / paid version using a library project.

Generally, you'll create three projects; a free project, a paid project and a library project.

You'll then create a Build.java file in your library project such as this:

public class Build {
    public final static int FREE = 1;
    public final static int PAID = 2;
    public static int getBuild(Context context){
        return context.getResources().getInteger(R.integer.build);
    }
}

Now, you'll create an build.xml resource in each of your projects:

[library]/resources/values/build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <integer name="build">0</integer>
</resources>

[free]/resources/values/build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <integer name="build">1</integer>
</resources>

[paid]/resources/values/build.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <integer name="build">2</integer>
</resources>

You will then be able to check for the version at run time:

if (Build.getBuild(context) == Build.FREE){
   // Do the free stuff
} else {
   // Do the paid stuff
}

The blog post details the exact steps necessary to create the projects from scratch at the Linux command line.

JohnnyLambada
  • 12,700
  • 11
  • 57
  • 61
0

The "paid" and "free" informations are dependant on each application -- and the library doesn't know such informations.

If you have some specific code that depends on those applications (i.e. code to check for the licence, for example), it should be in the specific applications, and not in the part that's common to both.

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • I was thinking that since the new projects have different package names, I could then pass these package names to the library project and create different logic paths based upon the package name of the new projects(com.myapps.freeapp and com.myapps.paidapp)....this is not possible? – dell116 Jul 25 '11 at 22:05
  • 1
    I suppose it would be possible, but it *feels wrong* : your library would not be *generic* anymore. A possibly better solution would be an abstract class in the library, extended by one class in each project ; and, then, from each project, instanciate the corresponding class to an object in the library. – Pascal MARTIN Jul 25 '11 at 22:08