I saw the stock Android-Developer licensing library instructions, but the overview seems to omit several key steps in the process and fails to fully explain how to get something working.
Can someone provide an explicit set of operations that worked to get the licensing library set up on an Android app so it checks to ensure that a user has paid for an app in Google Play before allowing use?
I have been working on implementing licensing in my app for some time now and finally have it working. I wanted to share some of the things I found helpful for getting started and some problems and solutions that I found with everyone. The android dev tutorial I have linked below is ok, but it wasn't that useful for me, so I decided to make a tutorial. Enjoy, and I hope it helps you!
Link to developer page here.
1. Getting started
Things you will need.
1.1 Your Base64 unique application key
How to get it:
a. Go to your developer console. Link.
b. If you haven't already created an application draft for your app, do it now.
c. Once you have created the draft, it is a good idea to upload your
.apk
as Alpha or Beta. Leave it unpublished.d. Click
Services & APIs
e. Scroll down and find
YOUR LICENSE KEY FOR THIS APPLICATION
f. Copy the key into your app like this:
Make sure that there are no spaces.
1.2 A salt
a. What is a salt?
A salt is random data that is additional input when hashing a password. They are used to defend against dictionary attacks and rainbow table attacks.
b. How do I get one?
This is a good link to generate a random salt. There should be exactly 20 random integers, so put
20
in for the amount of random strings to generate, each string should be2
characters long (used for this example, it doesn't have to be). Check numeric digits, and check Identical strings are allowed. They can be negative numbers too. Try to remove any redundancy, e.g.00 -> 0
, for the sake of consistency.c. Where do I put the salt?
When declaring variables just put this code in, except with your random salt.
2. Importing the LVL (Licensing) library into Eclipse and the code you need
2.1 Importing the library
a. Open
Android SDK Manager
b. Go to
Extras
c. Install
Google Play Licensing Library
d. Find your
SDK
install path which is listed at the top of the SDK manager.e. Once you are there, navigate to:
<sdk>/extras/google/play_licensing
f. In eclipse, click
file
thenimport
, thenExisting Android Code Into Workspace
and when it asks you for the file path, navigate to theplay_licensing
folder and click onlibrary
.g. Once the project named
library
has been imported, right click it, then hitproperties
. ClickAndroid
on the left and navigate to the bottom and checkIs Library
, then hit apply. This lets eclipse know that you can use this project code as a library.h. Right click on your app that you are adding licensing to, and click properties, then hit
Android
. Go to the bottom and clicklibrary
and add it to the build path. This should import the library to theAndroid Dependencies
folder.i. Your project is set up to go to the next step.
2.2 Variables to declare along with your
SALT
andKEY
2.3 The code
Paste this code near the bottom of your app. This implementation will notify the user if the license is not valid and prompt them to buy the app or exit it.
2.4 Getting your device id
There has been some debate about this in the past about whether or not to use the sim serial or
TelephonyManager.getDeviceId();
but it is generally recommended you use the following code to get theANDROID_ID
of your device for maximum compatibility.2.5 Creation of the license checker
a. Before you call
doCheck();
you must put this code in your app to make sure everything gets created properly.When I was doing my implemetation of LVL, I read that if you are having problems with Licensing, you can change the first
this
in themChecker = new LicenseChecker(this...
togetApplicationContext()
, mine seemed to work without it, but just in case.2.6 Adding permissions
a. There are two permissions that you need to add to your applications
manifest
file.2.7 Make sure you have the proper imports!
You have probably already done this, but I figured it would be a good place for you to check.
2.8 How to call the license to be checked
a. Simply call
doCheck();
whenever you want to check the license. For example if the app is on its first run, do the check.3. How do I test the licensing to make sure it works before publishing it?
3.1 Configuring the testing device
a. I have my personal phone that I also use for testing. It is recommended that there be only one Google account registered on the phone, historically it makes things a little easier. You can check the accounts by going to
Settings -> Accounts
.3.2 Configuring the developer console
a. Open your developer console and go to
Settings
on the left hand side.b. Find
License Testing
c. Make sure that your email address is listed under
Gmail accounts with testing access
d. Now, you can change the test response to whatever you like for testing purposes. The app should respond accordingly. Remember that if you are saving the data via SharedPrefs you will need to clear your app data every time you test it. Make sure that you click save after you change the test response or nothing will happen! I forgot about this multiple times and I ended up with a migraine, then I saw that stinking save button. Lol.
4. Things to try
4.1 Conditional license checking
a. You can try this code if you are saving the
didCheck
data inSharedPreferences
.4.2 Encrypting your
SharedPreferences
usingSecurePreferences
a. Go to this link.
b. Copy and paste the code from
SecurePreferences.java
into a class with the exact same name into your project.c. Read the
ReadMe.md
for info on implementing this.5. Troubleshooting
Licensing can be one heck of a headache to troubleshoot, simply because there are many more things that can go wrong. For example, there could be network problems or server problems that make you want to rip your hair out. Use of proper logging will help with this, you can also get the server response codes if there is a problem and you can trace it to the server or your app. I have had to do this on multiple occasions.
5.1 I can't get my app to return anything from the server
Possible Fixes:
a. Make sure that your app has the correct
KEY
.b. Make sure you are logging each step of the progress
c. Check your log for anything from the licensing service. It can be useful for figuring out where something went wrong.
d. Make sure
allow()
anddontAllow()
andapplicationError()
have@Override
tags.5.2 My app always says
LICENSED
orNOT_LICENSED
no matter what I set it to in the test responsea. The best cure I have for this is just to wait. It seems that if you do lots of testing in a short period of time, it will always send you server code
291
which is the retry code. I waited overnight and everything worked fine the next morning.b. You can clear the data (not just cache) of the Google Play app and the Google Play Services app. Then open play back up and accept all the licenses and try again.
c. Clear your app data.
5.3 List of server response codes for debugging
You should get these decimal values for
int reason
if you log them. Use this table to reference what the server is actually sending to your app.5.4 Room for more! They will come!
I hope this helps you guys! I tried to share my headaches and fixes with you guys as best I can and I hope this helps!
If I made any errors, be sure to tell me about them so I can get them fixed ASAP!