Licensing

Google Play offers a licensing service that lets you enforce licensing policies for applications that you publish on Google Play. With Google Play Licensing, your application can query Google Play at run time to obtain the licensing status for the current user, then allow or disallow further use as appropriate.

Using the service, you can apply a flexible licensing policy on an application-by-application basis—each application can enforce licensing in the way most appropriate for it. If necessary, an application can apply custom constraints based on the licensing status obtained from Google Play. For example, an application can check the licensing status and then apply custom constraints that allow the user to run it unlicensed for a specific validity period. An application can also restrict use of the application to a specific device, in addition to any other constraints.

The licensing service is a secure means of controlling access to your applications. When an application checks the licensing status, the Google Play server signs the licensing status response using a key pair that is uniquely associated with the application. Your application stores the public key in its compiled .apk file and uses it to verify the licensing status response.

Any application that you publish through Google Play can use the Google Play Licensing service. No special account or registration is needed. Additionally, because the service uses no dedicated framework APIs, you can add licensing to any application that uses a minimum API level of 3 or higher.

Before you can use the API, you need to enable the licensing API under the Android Native Vending service.

Note: The Google Play Licensing service is primarily intended for paid applications that wish to verify that the current user did in fact pay for the application on Google Play. However, any application (including free apps) may use the licensing service to initiate the download of an APK expansion file. In which case, the request that your application sends to the licensing service is not to check whether the user paid for the app, but to request the URL of the expansion files. For information about downloading expansion files for your application, read the guide to APK Expansion Files.

Adding Licensing to Your App

Before you begin to use C# API, make sure that app licensing is enabled under the Android Native Vending service. See the screenshot below:

The code sample below demonstrates how to get app lincesing policy for a currently running app:

using SA.Android.Vending.Licensing;
...

AN_LicenseChecker.CheckAccess((result) => {
    switch(result.PolicyCode) {
        case AN_Policy.LICENSED:
            AN_Logger.Log("AN_LicenseChecker: LICENSED");
            break;
        case AN_Policy.NOT_LICENSED:
            AN_Logger.Log("AN_LicenseChecker: NOT_LICENSED");
            break;
        case AN_Policy.RETRY:
            AN_Logger.Log("AN_LicenseChecker: RETRY");
            break;

    }

    if(result.IsFailed) {
        Debug.Log("AN_LicenseChecker error: " + result.ErrorCode.ToString());
    }
});

You may learn more about App Licensing here.