Android App versions (Free and Paid) - tell Library Project classes if app is Free or Paid

喜夏-厌秋 提交于 2019-12-04 16:47:38

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.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!