EddieH
EddieH

Reputation: 143

On Android, how to use code to distinguish free app vs paid app using the same library

I found this article on using a library for paid vs free. It specifically mentioned the following: "possible to have a boolean resource indicating paid or free status which can be tested by code in the library project and features switched on or off accordingly. Normally the library project would have the the default paid status value of the resource, and the free version would override it to the free value." How exactly can I make a boolean that can be overrided in the free version? Basically how can I do something in my common library code that says if app is free version then display ads?

Upvotes: 3

Views: 474

Answers (2)

user942821
user942821

Reputation:

Not only Android. It is normal case for other platforms. You can store your information on user's device, or on your service (if you have a site). Then check it when the user uses the application.

On Android, you can use SharedPreferences.

Upvotes: 0

Timothy Lee Russell
Timothy Lee Russell

Reputation: 3748

One way to do this is by using the string resource file -- since the values are "cascading".

The library code gets copied into both the free and paid projects and runs in the context of that project which means it looks at the "local" string resource first.

As long as you also specify that string resource name in your library project, it will compile but use the more "local" value when it is executing.

In your library project, specify a string named "paid" in res/values/strings.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
    <string name="app_name">My app</string>
    <string name="paid">false</string>
</resources>

In your free project, specify:

<string name="paid">false</string>

In your paid project, specify:

<string name="paid">true</string>

You can now access this in your library project:

boolean paid = Boolean.parseBoolean(getString(R.string.paid));

When you run the free project it will report False and when you run the paid project it will report True.

Then you just have to check that boolean and branch away...

if(!paid) {
  //show an ad (or whatever)
}

if(paid) {
  //load more game levels (or whatever)
}

Upvotes: 5

Related Questions