1page.title=Preparing Your In-app Billing Application 2parent.title=Selling In-app Products 3parent.link=index.html 4 5trainingnavtop=true 6next.title=Establishing In-app Billing Products for Sale 7next.link=list-iab-products.html 8 9@jd:body 10 11<!-- This is the training bar --> 12<div id="tb-wrapper"> 13<div id="tb"> 14 15<h2>This lesson teaches you to</h2> 16<ol> 17 <li><a href="#GetSample">Download the Sample App</a></li> 18 <li><a href="#AddToDevConsole">Add Your App to the Developer Console</a></li> 19 <li><a href="#AddLibrary">Add the In-app Billing Library</a></li> 20 <li><a href="#SetPermission">Set the Billing Permission</a></li> 21 <li><a href="#Connect">Initiate a Connection with Google Play</a></li> 22</ol> 23 24<h2>You should also read</h2> 25<ul> 26<li><a href="{@docRoot}google/play/billing/billing_overview.html">In-app Billing 27 Overview</a></li> 28</ul> 29 30</div> 31</div> 32 33<a class="notice-developers-video wide" 34href="https://www.youtube.com/watch?v=UvCl5Xx7Z5o"> 35<div> 36 <h3>Video</h3> 37 <p>Implementing Freemium</p> 38 </div> 39 </a> 40 41<p>Before you can start using the In-app Billing service, you'll need to add the library that contains the In-app Billing Version 3 API to your Android project. You also need to set the permissions for your application to communicate with Google Play. In addition, you'll need to establish a connection between your application and Google Play. You should also verify that the In-app Billing API version that you are using in your application is supported by Google Play.</p> 42 43<h2 id="GetSample">Download the Sample Application</h2> 44<p>In this training class, you will use a reference implementation for the In-app Billing Version 3 API called the {@code TrivialDrive} sample application. The sample includes convenience classes to quickly set up the In-app Billing service, marshal and unmarshal data types, and handle In-app Billing requests from the main thread of your application.</p> 45<p>To download the sample application:</p> 46<ol> 47<li>Open Android Studio and then close any open projects until you are 48presented with the welcome screen.</li> 49<li>Choose <strong>Import an Android code sample</strong> from the 50 <strong>Quick Start</strong> list on the right side the window.</li> 51<li>Type {@code Trivial Drive} into the search bar and select the 52 <strong>Trivial Drive</strong> sample.</li> 53<li>Follow the rest of the instructions in the <strong>Import Sample</strong> 54 wizard to import the sample to a directory of your choosing. The sample code 55 is in the <strong>TrivialDrive</strong> subdirectory of the repository.</li> 56</ol> 57 58<p>Alternatively, you can use {@code git} to manually clone 59 the repository from <a 60 href="https://github.com/googlesamples/android-play-billing" 61 class="external-link">https://github.com/googlesamples/android-play-billing</a></p> 62 63<h2 id="AddToDevConsole">Add Your Application to the Developer Console</h2> 64<p>The Google Play Developer Console is where you publish your In-app Billing application and manage the various digital goods that are available for purchase from your application. When you create a new application entry in the Developer Console, it automatically generates a public license key for your application. You will need this key to establish a trusted connection from your application to the Google Play servers. You only need to generate this key once per application, and don’t need to repeat these steps when you update the APK file for your application.</p> 65<p>To add your application to the Developer Console:</p> 66<ol> 67<li>Go to the <a href="http://play.google.com/apps/publish">Google Play Developer Console</a> site and log in. You will need to register for a new developer account, if you have not registered previously. To sell in-app items, you also need to have a <a href="http://www.google.com/wallet/merchants.html">Google payments</a> merchant account.</li> 68<li>Click on <strong>Try the new design</strong> to access the preview version of the Developer Console, if you are not already logged on to that version. </li> 69<li>In the <strong>All Applications</strong> tab, add a new application entry. 70<ol type="a"> 71<li>Click <strong>Add new application</strong>.</li> 72<li>Enter a name for your new In-app Billing application.</li> 73<li>Click <strong>Prepare Store Listing</strong>.</li> 74</ol> 75</li> 76<li>In the <strong>Services & APIs</strong> tab, find and make a note of the public license key that Google Play generated for your application. This is a Base64 string that you will need to include in your application code later.</li> 77</ol> 78<p>Your application should now appear in the list of applications in Developer Console.</p> 79 80<h2 id="AddLibrary">Add the In-app Billing Library</h2> 81<p>To use the In-app Billing Version 3 features, you must add the {@code IInAppBillingService.aidl} file to your Android project. This Android Interface Definition Language (AIDL) file defines the interface to the Google Play service.</p> 82<p>You can find the {@code IInAppBillingService.aidl} file in the provided sample app. Depending on whether you are creating a new application or modifying an existing application, follow the instructions below to add the In-app Billing Library to your project.</p> 83<h3>New Project</h3> 84<p>To add the In-app Billing Version 3 library to your new In-app Billing project:</p> 85<ol> 86<li>Copy the {@code TrivialDrive} sample files into your Android project.</li> 87<li>Modify the package name in the files you copied to use the package name for your project. In Android Studio, you can use this shortcut: right-click the package name, then select <strong>Refactor</strong> > <strong>Rename</strong>.</li> 88<li>Open the {@code AndroidManifest.xml} file and update the package attribute value to use the package name for your project.</li> 89<li>Fix import statements as needed so that your project compiles correctly. In Android Studio, you can use this shortcut: press <strong>Ctrl+Shift+O</strong> in each file showing errors.</li> 90<li>Modify the sample to create your own application. Remember to copy the Base64 public license key for your application from the Developer Console over to your {@code MainActivity.java}.</li> 91</ol> 92 93<h3>Existing Project</h3> 94<p>To add the In-app Billing Version 3 library to your existing In-app Billing project:</p> 95<ol> 96<li>Copy the {@code IInAppBillingService.aidl} file to your Android project. 97 <ul> 98 <li>In Android Studio: Create a directory named {@code aidl} under {@code src/main}, add a new 99 package {@code com.android.vending.billing} in this directory, and import the 100 {@code IInAppBillingService.aidl} file into this package.</li> 101 <li>In other dev environments: Create the following directory {@code /src/com/android/vending/billing} and copy the {@code IInAppBillingService.aidl} file into this directory.</li> 102 </ul> 103</li> 104<li>Build your application. You should see a generated file named {@code IInAppBillingService.java} in the {@code /gen} directory of your project.</li> 105<li>Add the helper classes from the {@code /util} directory of the {@code TrivialDrive} sample to your project. Remember to change the package name declarations in those files accordingly so that your project compiles correctly.</li> 106</ol> 107<p>Your project should now contain the In-app Billing Version 3 library.</p> 108 109<h2 id="SetPermission">Set the Billing Permission</h2> 110<p>Your app needs to have permission to communicate request and response messages to the Google Play’s billing service. To give your app the necessary permission, add this line in your {@code AndroidManifest.xml} manifest file:</p> 111<pre> 112<uses-permission android:name="com.android.vending.BILLING" /> 113</pre> 114 115<h2 id="Connect">Initiate a Connection with Google Play</h2> 116<p>You must bind your Activity to Google Play’s In-app Billing service to send In-app Billing requests to Google Play from your application. The convenience classes provided in the sample handles the binding to the In-app Billing service, so you don’t have to manage the network connection directly.</p> 117<p>To set up synchronous communication with Google Play, create an {@code IabHelper} instance in your activity's {@code onCreate} method. In the constructor, pass in the {@code Context} for the activity, along with a string containing the public license key that was generated earlier by the Google Play Developer Console. </p> 118<p class="note"><strong>Security Recommendation:</strong> It is highly recommended that you do not hard-code the exact public license key string value as provided by Google Play. Instead, you can construct the whole public license key string at runtime from substrings, or retrieve it from an encrypted store, before passing it to the constructor. This approach makes it more difficult for malicious third-parties to modify the public license key string in your APK file.</p> 119 120<pre> 121IabHelper mHelper; 122 123@Override 124public void onCreate(Bundle savedInstanceState) { 125 // ... 126 String base64EncodedPublicKey; 127 128 // compute your public key and store it in base64EncodedPublicKey 129 mHelper = new IabHelper(this, base64EncodedPublicKey); 130} 131</pre> 132 133<p>Next, perform the service binding by calling the {@code startSetup} method on the {@code IabHelper} instance that you created. Pass the method an {@code OnIabSetupFinishedListener} instance, which is called once the {@code IabHelper} completes the asynchronous setup operation. As part of the setup process, the {@code IabHelper} also checks if the In-app Billing Version 3 API is supported by Google Play. If the API version is not supported, or if an error occured while establishing the service binding, the listener is notified and passed an {@code IabResult} object with the error message.</p> 134 135<pre> 136mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() { 137 public void onIabSetupFinished(IabResult result) { 138 if (!result.isSuccess()) { 139 // Oh noes, there was a problem. 140 Log.d(TAG, "Problem setting up In-app Billing: " + result); 141 } 142 // Hooray, IAB is fully set up! 143 } 144}); 145</pre> 146 147<p>If the setup completed successfully, you can now use the {@code mHelper} reference to communicate with the Google Play service. When your application is launched, it is a good practice to query Google Play to find out what in-app items are owned by a user. This is covered further in the <a href="{@docRoot}training/in-app-billing/purchase-iab-products.html#QueryPurchases">Query Purchased Items</a> section.</p> 148 149<p class="note"><strong>Important:</strong> Remember to unbind from the In-app Billing service when you are done with your activity. If you don’t unbind, the open service connection could cause your device’s performance to degrade. To unbind and free your system resources, call the {@code IabHelper}'s {@code dispose} method when your {@code Activity} is destroyed.</p> 150 151<pre> 152@Override 153public void onDestroy() { 154 super.onDestroy(); 155 if (mHelper != null) mHelper.dispose(); 156 mHelper = null; 157} 158</pre> 159 160 161 162 163 164