Android App versions (Free and Paid) - tell Librar

2020-07-23 08:30发布

问题:

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?

回答1:

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.



回答2:

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.