Need to ramp up the monetization of your Android apps? In-app purchases may be the answer. In my last post, I discussed some of the trends and high-level aspects of the topic, and in this article, we will walk through an actual implementation.
This tutorial uses Eclipse with the Android SDK. It assumes you have some basic experience with Eclipse and that you have an Android app in Eclipse.
I am going to create an In-app purchase for Certificate Maker! The app is a very simple one which allows a user to enter some basic information to create a certificate acknowledging someone’s accomplishment. I want to allow a person to pay $.99 to have access to other certificate templates. I also hope to build it in a way that I can eventually have other in-app products such as themed certificates, or give users the ability to further customize certificates with their own images or packs of images (stickers), or rearrange the text location, size, color etc.
What I am going to do will surely be different in some ways from what you would do for your own projects, but I hope there is enough overlap to give you basic knowledge and a shot of confidence to start moving forward.
Creating an In-App Purchase Product
1. Prepare your Project
The first thing I did was to move all the needed files into my project in Eclipse. For Certificate Maker!, this required adding several image files, Java files and XML files. I also had to make a few modifications to my Manifest.
I then modified several Java and XML files to facilitate the flow of screens (activities) as they would occur once the app was actually set up for in-app purchases. One day I hope to have one button with logic to initiate an in-app purchase if the user has never made the purchase OR simply launch the additional templates if the user had made the purchase. But to keep things clearer in this tutorial, I have split these two functions into separate buttons and defaulted one to be disabled until a purchase is made.
2. Add the Billing permission to your APK
Let’s start by updating our application’s manifest, which will need to have the following line added to have the billing permissions:
<uses-permission android:name=”com.android.vending.BILLING” />
3. Get the Google Play API
Next you will need to download a number of files and then bring them into your own project. Google makes the files available to us as part of a sample. To get them:
- Open “Android SDK Manager” in the “Window” drop-down menu
- In the window that opens, scroll to the Extras section and select Google Play Billing Library
- Click Install (Note: Eclipse may have checked other packages than the billing one to install. To stay on task, you might uncheck all but the billing library and then press install.)
- Read and accept the license, and then wait.
This installs a sample project called TrivialDrive, which you should import into your workspace. The file was placed in the following location for me: C:\Users\dan\Android\adt-bundle-windows-x86_64-20130717\sdk\extras\google\play_billing\samples.
After I imported the files into Eclipse, the sample app would not run in the emulator or on my device. That does not really matter, because we just imported it so that we could harvest a few files. Seems like a silly way to get a few files, but that is how Google has made them available at this time so bear with me.
You need to copy the com.android.vending.billing package (including the IInAppBillingService.aidl file) from the sample app’s src folder into the src folder of your own app. No modification to the package or the aidl file is needed.
4. Preparing Your Activity for In-App Purchases
Open the Java for the Activity where users will access your In-app product. Paste the following two lines into your code where you initialize variables:
iabHelper mHelper;static final String ITEM_SKU = “android.test.purchased”;
Then in your onCreate method, you will need to add code so that Google Play can know from what app the purchases are coming. You will need to get a key from the Google Play Developer Console. To get the key:
- Open your app’s page in the Developer Console
- Click on “Services & APIs”
- Scroll down until you see “YOUR LICENSE KEY FOR THIS APPLICATION” – in a gray box, you will see a multi-line code that includes letters, numbers and special characters. This is sometimes called your license key or public key. Note this is NOT your Developer Public Key.
- Create a line of code in your Activity immediately below the last two lines of code you created, then paste your app’s license key between the quotation marks:
- Immediately below the previous line of code, paste the following:
- In your onDestroy method paste the following two lines to make sure the connection to Google Play is properly terminated:
At this point save (but do not close) the file, ensuring you do not have any errors. You may need to perform some imports to properly point to the files you created in the util package. Let Eclipse help you with this by using Control+Shift+o.
5. Code for Initiating a Purchase
While the above steps will be common to all implementations of In-App Purchases (I think J) the next set of steps will vary based upon how you plan to have your users initiate a purchase. Certificate Maker! users will click a simple button to enter the purchase flow. Initially we will code it to use dummy data. Once everything is working correctly, we will modify it to use real data. I changed the code for my button to be as follows:
6. Listen Up – We Are Almost Done
The next bit of code should be place after and outside of your @onCreate. It includes three listeners:
- Query Inventory – queries the billing system for purchased items
- Purchase Finished – confirms the specific purchase was made that we intended
- Consumption Finished – marks the product as consumed. Google says, “if your application detects there are any consumable items that are owned by the user, you should send a consumption request to Google Play immediately and provision the item to the user.” When a successful purchase is made, it is stored locally, but Google recommends that your app queries Google Play to confirm the purchases Google Play is tracking to ensure consistency between the service and what you display to the user.
Remember, the concept of consumption does not necessarily mean that a user has used up the item. I think of it more as your app acknowledging that the purchased product has been received by the app. It is recommended to consume the product before providing the user access to the product. With the current implementation of Google’s API all managed products are consumable. So even if the “old” concept of consumption does not apply to the product you are creating, (e.g., it is an item that provide permanent benefit such as a premium upgrade,) you will still need consumption related code.
If your product is a subscription, simply set the product type to “subs”. Then in the onActivityResult method do the following:
To query for active subscriptions use the following:
Bundle activeSubs = mService.getPurchases(3, “com.example.myapp”,”subs”, continueToken);
If you’ve followed the article properly you’re one step ahead to implementing In-app purchases into your app. Stay tuned for the last part to finish the project.