1page.title=APK Expansion Files 2page.metaDescription=If your app needs more than the 100MB APK max, use free APK expansion files from Google Play. 3page.tags="apk size, apk max, large assets" 4@jd:body 5 6 7<div id="qv-wrapper"> 8<div id="qv"> 9<h2>Quickview</h2> 10<ul> 11 <li>Recommended for most apps that exceed the 100MB APK limit</li> 12 <li>You can provide up to 4GB of additional data for each APK</li> 13 <li>Google Play hosts and serves the expansion files at no charge</li> 14 <li>The files can be any file type you want and are saved to the device's shared storage</li> 15</ul> 16 17<h2>In this document</h2> 18<ol> 19 <li><a href="#Overview">Overview</a> 20 <ol> 21 <li><a href="#Filename">File name format</a></li> 22 <li><a href="#StorageLocation">Storage location</a></li> 23 <li><a href="#DownloadProcess">Download process</a></li> 24 <li><a href="#Checklist">Development checklist</a></li> 25 </ol> 26 </li> 27 <li><a href="#Rules">Rules and Limitations</a></li> 28 <li><a href="#Downloading">Downloading the Expansion Files</a> 29 <ol> 30 <li><a href="#AboutLibraries">About the Downloader Library</a></li> 31 <li><a href="#Preparing">Preparing to use the Downloader Library</a></li> 32 <li><a href="#Permissions">Declaring user permissions</a></li> 33 <li><a href="#DownloaderService">Implementing the downloader service</a></li> 34 <li><a href="#AlarmReceiver">Implementing the alarm receiver</a></li> 35 <li><a href="#Download">Starting the download</a></li> 36 <li><a href="#Progress">Receiving download progress</a></li> 37 </ol> 38 </li> 39 <li><a href="#ExpansionPolicy">Using APKExpansionPolicy</a></li> 40 <li><a href="#ReadingTheFile">Reading the Expansion File</a> 41 <ol> 42 <li><a href="#GettingFilenames">Getting the file names</a></li> 43 <li><a href="#ZipLib">Using the APK Expansion Zip Library</a></li> 44 </ol> 45 </li> 46 <li><a href="#Testing">Testing Your Expansion Files</a> 47 <ol> 48 <li><a href="#TestingReading">Testing file reads</a></li> 49 <li><a href="#TestingReading">Testing file downloads</a></li> 50 </ol> 51 </li> 52 <li><a href="#Updating">Updating Your Application</a></li> 53</ol> 54 55<h2>See also</h2> 56<ol> 57 <li><a href="{@docRoot}google/play/licensing/index.html">Application Licensing</a></li> 58 <li><a href="{@docRoot}google/play/publishing/multiple-apks.html">Multiple 59APK Support</a></li> 60</ol> 61</div> 62</div> 63 64 65 66<p>Google Play currently requires that your APK file be no more than 100MB. For most 67applications, this is plenty of space for all the application's code and assets. 68However, some apps need more space for high-fidelity graphics, media files, or other large assets. 69Previously, if your app exceeded 100MB, you had to host and download the additional resources 70yourself when the user opens the app. Hosting and serving the extra files can be costly, and the 71user experience is often less than ideal. To make this process easier for you and more pleasant 72for users, Google Play allows you to attach two large expansion files that supplement your 73APK.</p> 74 75<p>Google Play hosts the expansion files for your application and serves them to the device at 76no cost to you. The expansion files are saved to the device's shared storage location (the 77SD card or USB-mountable partition; also known as the "external" storage) where your app can access 78them. On most devices, Google Play downloads the expansion file(s) at the same time it 79downloads the APK, so your application has everything it needs when the user opens it for the 80first time. In some cases, however, your application must download the files from Google Play 81when your application starts.</p> 82 83 84 85<h2 id="Overview">Overview</h2> 86 87<p>Each time you upload an APK using the Google Play Developer Console, you have the option to 88add one or two expansion files to the APK. Each file can be up to 2GB and it can be any format you 89choose, but we recommend you use a compressed file to conserve bandwidth during the download. 90Conceptually, each expansion file plays a different role:</p> 91 92<ul> 93 <li>The <strong>main</strong> expansion file is the 94primary expansion file for additional resources required by your application.</li> 95 <li>The <strong>patch</strong> expansion file is optional and intended for small updates to the 96main expansion file.</li> 97</ul> 98 99<p>While you can use the two expansion files any way you wish, we recommend that the main 100expansion file deliver the primary assets and should rarely if ever updated; the patch expansion 101file should be smaller and serve as a “patch carrier,” getting updated with each major 102release or as necessary.</p> 103 104<p>However, even if your application update requires only a new patch expansion file, you still must 105upload a new APK with an updated <a 106href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code 107versionCode}</a> in the manifest. (The 108Developer Console does not allow you to upload an expansion file to an existing APK.)</p> 109 110<p class="note"><strong>Note:</strong> The patch expansion file is semantically the same as the 111main expansion file—you can use each file any way you want. The system does 112not use the patch expansion file to perform patching for your app. You must perform patching 113yourself or be able to distinguish between the two files.</p> 114 115 116 117<h3 id="Filename">File name format</h3> 118 119<p>Each expansion file you upload can be any format you choose (ZIP, PDF, MP4, etc.). You can also 120use the <a href="{@docRoot}tools/help/jobb.html">JOBB</a> tool to encapsulate and encrypt a set 121of resource files and subsequent patches for that set. Regardless of the file type, Google Play 122considers them opaque binary blobs and renames the files using the following scheme:</p> 123 124<pre class="classic no-pretty-print"> 125[main|patch].<expansion-version>.<package-name>.obb 126</pre> 127 128<p>There are three components to this scheme:</p> 129 130<dl> 131 <dt>{@code main} or {@code patch}</dt> 132 <dd>Specifies whether the file is the main or patch expansion file. There can be 133only one main file and one patch file for each APK.</dd> 134 <dt>{@code <expansion-version>}</dt> 135 <dd>This is an integer that matches the version code of the APK with which the expansion is 136<em>first</em> associated (it matches the application's <a 137href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code android:versionCode}</a> 138value). 139 <p>"First" is emphasized because although the Developer Console allows you to 140re-use an uploaded expansion file with a new APK, the expansion file's name does not change—it 141retains the version applied to it when you first uploaded the file.</p></dd> 142 <dt>{@code <package-name>}</dt> 143 <dd>Your application's Java-style package name.</dd> 144</dl> 145 146<p>For example, suppose your APK version is 314159 and your package name is com.example.app. If you 147upload a main expansion file, the file is renamed to:</p> 148<pre class="classic no-pretty-print">main.314159.com.example.app.obb</pre> 149 150 151<h3 id="StorageLocation">Storage location</h3> 152 153<p>When Google Play downloads your expansion files to a device, it saves them to the system's 154shared storage location. To ensure proper behavior, you must not delete, move, or rename the 155expansion files. In the event that your application must perform the download from Google Play 156itself, you must save the files to the exact same location.</p> 157 158<p>The specific location for your expansion files is:</p> 159 160<pre class="classic no-pretty-print"> 161<shared-storage>/Android/obb/<package-name>/ 162</pre> 163 164<ul> 165 <li>{@code <shared-storage>} is the path to the shared storage space, available from 166{@link android.os.Environment#getExternalStorageDirectory()}.</li> 167 <li>{@code <package-name>} is your application's Java-style package name, available 168from {@link android.content.Context#getPackageName()}.</li> 169</ul> 170 171<p>For each application, there are never more than two expansion files in this directory. 172One is the main expansion file and the other is the patch expansion file (if necessary). Previous 173versions are overwritten when you update your application with new expansion files.</p> 174 175<p>If you must unpack the contents of your expansion files, <strong>do not</strong> delete the 176{@code .obb} expansion files afterwards and <strong>do not</strong> save the unpacked data 177in the same directory. You should save your unpacked files in the directory 178specified by {@link android.content.Context#getExternalFilesDir getExternalFilesDir()}. However, 179if possible, it's best if you use an expansion file format that allows you to read directly from 180the file instead of requiring you to unpack the data. For example, we've provided a library 181project called the <a href="#ZipLib">APK Expansion Zip Library</a> that reads your data directly 182from the ZIP file.</p> 183 184<p class="note"><strong>Note:</strong> Unlike APK files, any files saved on the shared storage can 185be read by the user and other applications.</p> 186 187<p class="note"><strong>Tip:</strong> If you're packaging media files into a ZIP, you can use media 188playback calls on the files with offset and length controls (such as {@link 189android.media.MediaPlayer#setDataSource(FileDescriptor,long,long) MediaPlayer.setDataSource()} and 190{@link android.media.SoundPool#load(FileDescriptor,long,long,int) SoundPool.load()}) without the 191need to unpack your ZIP. In order for this to work, you must not perform additional compression on 192the media files when creating the ZIP packages. For example, when using the <code>zip</code> tool, 193you should use the <code>-n</code> option to specify the file suffixes that should not be 194compressed: <br/> 195<code>zip -n .mp4;.ogg main_expansion media_files</code></p> 196 197 198<h3 id="DownloadProcess">Download process</h3> 199 200<p>Most of the time, Google Play downloads and saves your expansion files at the same time it 201downloads the APK to the device. However, in some cases Google Play 202cannot download the expansion files or the user might have deleted previously downloaded expansion 203files. To handle these situations, your app must be able to download the files 204itself when the main activity starts, using a URL provided by Google Play.</p> 205 206<p>The download process from a high level looks like this:</p> 207 208<ol> 209 <li>User selects to install your app from Google Play.</li> 210 <li>If Google Play is able to download the expansion files (which is the case for most 211devices), it downloads them along with the APK. 212 <p>If Google Play is unable to download the expansion files, it downloads the 213APK only.</p> 214 </li> 215 <li>When the user launches your application, your app must check whether the expansion files are 216already saved on the device. 217 <ol> 218 <li>If yes, your app is ready to go.</li> 219 <li>If no, your app must download the expansion files over HTTP from Google Play. Your app 220must send a request to the Google Play client using the Google Play's <a 221href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service, which 222responds with the name, file size, and URL for each expansion file. With this information, you then 223download the files and save them to the proper <a href="#StorageLocation">storage location</a>.</li> 224 </ol> 225 </li> 226</ol> 227 228<p class="caution"><strong>Caution:</strong> It is critical that you include the necessary code to 229download the expansion files from Google Play in the event that the files are not already on the 230device when your application starts. As discussed in the following section about <a 231href="#Downloading">Downloading the Expansion Files</a>, we've made a library available to you that 232greatly simplifies this process and performs the download from a service with a minimal amount of 233code from you.</p> 234 235 236 237 238<h3 id="Checklist">Development checklist</h3> 239 240<p>Here's a summary of the tasks you should perform to use expansion files with your 241application:</p> 242 243<ol> 244 <li>First determine whether your application absolutely requires more than 100MB per installation. 245Space is precious and you should keep your total application size as small as possible. If your app 246uses more than 100MB in order to provide multiple versions of your graphic assets for multiple screen 247densities, consider instead publishing <a 248href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> in which each APK 249contains only the assets required for the screens that it targets.</li> 250 <li>Determine which application resources to separate from your APK and package them in a 251file to use as the main expansion file. 252 <p>Normally, you should only use the second patch expansion file when performing updates to 253the main expansion file. However, if your resources exceed the 2GB limit for the main 254expansion file, you can use the patch file for the rest of your assets.</p> 255 </li> 256 <li>Develop your application such that it uses the resources from your expansion files in the 257device's <a href="#StorageLocation">shared storage location</a>. 258 <p>Remember that you must not delete, move, or rename the expansion files.</p> 259 <p>If your application doesn't demand a specific format, we suggest you create ZIP files for 260your expansion files, then read them using the <a href="#ZipLib">APK Expansion Zip 261Library</a>.</p> 262 </li> 263 <li>Add logic to your application's main activity that checks whether the expansion files 264are on the device upon start-up. If the files are not on the device, use Google Play's <a 265href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service to request URLs 266for the expansion files, then download and save them. 267 <p>To greatly reduce the amount of code you must write and ensure a good user experience 268during the download, we recommend you use the <a href="#AboutLibraries">Downloader 269Library</a> to implement your download behavior.</p> 270 <p>If you build your own download service instead of using the library, be aware that you 271must not change the name of the expansion files and must save them to the proper 272<a href="#StorageLocation">storage location</a>.</p></li> 273</ol> 274 275<p>Once you've finished your application development, follow the guide to <a href="#Testing">Testing 276Your Expansion Files</a>.</p> 277 278 279 280 281 282 283<h2 id="Rules">Rules and Limitations</h2> 284 285<p>Adding APK expansion files is a feature available when you upload your application using the 286Developer Console. When uploading your application for the first time or updating an 287application that uses expansion files, you must be aware of the following rules and limitations:</p> 288 289<ol type="I"> 290 <li>Each expansion file can be no more than 2GB.</li> 291 <li>In order to download your expansion files from Google Play, <strong>the user must have 292acquired your application from Google Play</strong>. Google Play will not 293provide the URLs for your expansion files if the application was installed by other means.</li> 294 <li>When performing the download from within your application, the URL that Google Play 295provides for each file is unique for every download and each one expires shortly after it is given 296to your application.</li> 297 <li>If you update your application with a new APK or upload <a 298href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> for the same 299application, you can select expansion files that you've uploaded for a previous APK. <strong>The 300expansion file's name does not change</strong>—it retains the version received by the APK to 301which the file was originally associated.</li> 302 <li>If you use expansion files in combination with <a 303href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> in order to 304provide different expansion files for different devices, you still must upload separate APKs 305for each device in order to provide a unique <a 306href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a> 307value and declare different <a href="{@docRoot}google/play/filters.html">filters</a> for 308each APK.</li> 309 <li>You cannot issue an update to your application by changing the expansion files 310alone—<strong>you must upload a new APK</strong> to update your app. If your changes only 311concern the assets in your expansion files, you can update your APK simply by changing the <a 312href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a> (and 313perhaps also the <a href="{@docRoot}guide/topics/manifest/manifest-element.html#vname">{@code 314versionName}</a>).</p></li> 315 <li><strong>Do not save other data into your <code>obb/</code> 316directory</strong>. If you must unpack some data, save it into the location specified by {@link 317android.content.Context#getExternalFilesDir getExternalFilesDir()}.</li> 318 <li><strong>Do not delete or rename the {@code .obb} expansion file</strong> (unless you're 319performing an update). Doing so will cause Google Play (or your app itself) to repeatedly 320download the expansion file.</li> 321 <li>When updating an expansion file manually, you must delete the previous expansion file.</li> 322</ol> 323 324 325 326 327 328 329 330 331 332<h2 id="Downloading">Downloading the Expansion Files</h2> 333 334<p>In most cases, Google Play downloads and saves your expansion files to the device at the same 335time it installs or updates the APK. This way, the expansion files are available when your 336application launches for the first time. However, in some cases your app must download the 337expansion files itself by requesting them from a URL provided to you in a response 338from Google Play's <a 339href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service.</p> 340 341<p>The basic logic you need to download your expansion files is the following:</p> 342 343<ol> 344 <li>When your application starts, look for the expansion files on the <a 345href="#StorageLocation">shared storage location</a> (in the 346<code>Android/obb/<package-name>/</code> directory). 347 <ol type="a"> 348 <li>If the expansion files are there, you're all set and your application can continue.</li> 349 <li>If the expansion files are <em>not</em> there: 350 <ol> 351 <li>Perform a request using Google Play's <a 352href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> to get your 353app's expansion file names, sizes, and URLs.</li> 354 <li>Use the URLs provided by Google Play to download the expansion files and save 355the expansion files. You <strong>must</strong> save the files to the <a 356href="#StorageLocation">shared storage location</a> 357(<code>Android/obb/<package-name>/</code>) and use the exact file name provided 358by Google Play's response. 359 <p class="note"><strong>Note:</strong> The URL that Google Play provides for your 360expansion files is unique for every download and each one expires shortly after it is given to 361your application.</p> 362 </li> 363 </ol> 364 </li> 365 </ol> 366 </li> 367</ol> 368 369 370<p>If your application is free (not a paid app), then you probably haven't used the <a 371href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service. It's primarily 372designed for you to enforce 373licensing policies for your application and ensure that the user has the right to 374use your app (he or she rightfully paid for it on Google Play). In order to facilitate the 375expansion file functionality, the licensing service has been enhanced to provide a response 376to your application that includes the URL of your application's expansion files that are hosted 377on Google Play. So, even if your application is free for users, you need to include the 378License Verification Library (LVL) to use APK expansion files. Of course, if your application 379is free, you don't need to enforce license verification—you simply need the 380library to perform the request that returns the URL of your expansion files.</p> 381 382<p class="note"><strong>Note:</strong> Whether your application is free or not, Google Play 383returns the expansion file URLs only if the user acquired your application from Google Play.</p> 384 385<p>In addition to the LVL, you need a set of code that downloads the expansion files 386over an HTTP connection and saves them to the proper location on the device's shared storage. 387As you build this procedure into your application, there are several issues you should take into 388consideration:</p> 389 390<ul> 391 <li>The device might not have enough space for the expansion files, so you should check 392before beginning the download and warn the user if there's not enough space.</li> 393 <li>File downloads should occur in a background service in order to avoid blocking the user 394interaction and allow the user to leave your app while the download completes.</li> 395 <li>A variety of errors might occur during the request and download that you must 396gracefully handle.</li> 397 <li>Network connectivity can change during the download, so you should handle such changes and 398if interrupted, resume the download when possible.</li> 399 <li>While the download occurs in the background, you should provide a notification that 400indicates the download progress, notifies the user when it's done, and takes the user back to 401your application when selected.</li> 402</ul> 403 404 405<p>To simplify this work for you, we've built the <a href="#AboutLibraries">Downloader Library</a>, 406which requests the expansion file URLs through the licensing service, downloads the expansion files, 407performs all of the tasks listed above, and even allows your activity to pause and resume the 408download. By adding the Downloader Library and a few code hooks to your application, almost all the 409work to download the expansion files is already coded for you. As such, in order to provide the best 410user experience with minimal effort on your behalf, we recommend you use the Downloader Library to 411download your expansion files. The information in the following sections explain how to integrate 412the library into your application.</p> 413 414<p>If you'd rather develop your own solution to download the expansion files using the Google 415Play URLs, you must follow the <a href="{@docRoot}google/play/licensing/index.html">Application 416Licensing</a> documentation to perform a license request, then retrieve the expansion file names, 417sizes, and URLs from the response extras. You should use the <a href="#ExpansionPolicy">{@code 418APKExpansionPolicy}</a> class (included in the License Verification Library) as your licensing 419policy, which captures the expansion file names, sizes, and URLs from the licensing service..</p> 420 421 422 423<h3 id="AboutLibraries">About the Downloader Library</h3> 424 425<p>To use APK expansion files with your application and provide the best user experience with 426minimal effort on your behalf, we recommend you use the Downloader Library that's included in the 427Google Play APK Expansion Library package. This library downloads your expansion files in a 428background service, shows a user notification with the download status, handles network 429connectivity loss, resumes the download when possible, and more.</p> 430 431<p>To implement expansion file downloads using the Downloader Library, all you need to do is:</p> 432 433<ul> 434 <li>Extend a special {@link android.app.Service} subclass and {@link 435android.content.BroadcastReceiver} subclass that each require just a few 436lines of code from you.</li> 437 <li>Add some logic to your main activity that checks whether the expansion files have 438already been downloaded and, if not, invokes the download process and displays a 439progress UI.</li> 440 <li>Implement a callback interface with a few methods in your main activity that 441receives updates about the download progress.</li> 442</ul> 443 444<p>The following sections explain how to set up your app using the Downloader Library.</p> 445 446 447<h3 id="Preparing">Preparing to use the Downloader Library</h3> 448 449<p>To use the Downloader Library, you need to 450download two packages from the SDK Manager and add the appropriate libraries to your 451application.</p> 452 453<p>First, open the <a href="{@docRoot}studio/intro/update.html">Android SDK Manager</a> 454(<strong>Tools > Android > SDK Manager</strong>), and under 455<em>Appearance & Behavior > System Settings > Android SDK</em>, select 456the <em>SDK Tools</em> tab to select and download:</p> 457<ul> 458 <li><em>Google Play Licensing Library package</em></li> 459 <li><em>Google Play APK Expansion Library package</em></li> 460</ul> 461 462<p>Create a new library module for the License Verification Library and Downloader 463Library. For each library:</p> 464<ol> 465 <li>Select <strong>File > New > New Module</strong>.</li> 466 <li>In the <em>Create New Module</em> window, select <strong>Android Library</strong>, 467and then select <strong>Next</strong>.</li> 468 <li>Specify an <em>Application/Library name</em> such as "Google Play License Library" 469and "Google Play Downloader Library", choose <em>Minimum SDK level</em>, then select 470<strong>Finish</strong>.</li> 471 <li>Select <strong>File > Project Structure</strong>.</li> 472 <li>Select the <em>Properties</em> tab and in <em>Library 473Repository</em>, enter the library from the {@code <sdk>/extras/google/} directory 474({@code play_licensing/} for the License Verification Library or {@code 475play_apk_expansion/downloader_library/} for the Downloader Library).</li> 476 <li>Select <strong>OK</strong> to create the new module.</li> 477</ol> 478 479<p class="note"><strong>Note:</strong> The Downloader Library depends on the License 480Verification Library. Be sure to add the License 481Verification Library to the Downloader Library's project properties.</p> 482 </li> 483</ol> 484 485<p>Or, from a command line, update your project to include the libraries:</p> 486<ol> 487 <li>Change directories to the <code><sdk>/tools/</code> directory.</li> 488 <li>Execute <code>android update project</code> with the {@code --library} option to add both the 489LVL and the Downloader Library to your project. For example: 490<pre class="no-pretty-print"> 491android update project --path ~/Android/MyApp \ 492--library ~/android_sdk/extras/google/market_licensing \ 493--library ~/android_sdk/extras/google/market_apk_expansion/downloader_library 494</pre> 495 </li> 496</ol> 497 498<p>With both the License Verification Library and Downloader Library added to your 499application, you'll be able to quickly integrate the ability to download expansion files from 500Google Play. The format that you choose for the expansion files and how you read them 501from the shared storage is a separate implementation that you should consider based on your 502application needs.</p> 503 504<p class="note"><strong>Tip:</strong> The Apk Expansion package includes a sample 505application 506that shows how to use the Downloader Library in an app. The sample uses a third library 507available in the Apk Expansion package called the APK Expansion Zip Library. If 508you plan on 509using ZIP files for your expansion files, we suggest you also add the APK Expansion Zip Library to 510your application. For more information, see the section below 511about <a href="#ZipLib">Using the APK Expansion Zip Library</a>.</p> 512 513 514 515<h3 id="Permissions">Declaring user permissions</h3> 516 517<p>In order to download the expansion files, the Downloader Library 518requires several permissions that you must declare in your application's manifest file. They 519are:</p> 520 521<pre> 522<manifest ...> 523 <!-- Required to access Google Play Licensing --> 524 <uses-permission android:name="com.android.vending.CHECK_LICENSE" /> 525 526 <!-- Required to download files from Google Play --> 527 <uses-permission android:name="android.permission.INTERNET" /> 528 529 <!-- Required to keep CPU alive while downloading files 530 (NOT to keep screen awake) --> 531 <uses-permission android:name="android.permission.WAKE_LOCK" /> 532 533 <!-- Required to poll the state of the network connection 534 and respond to changes --> 535 <uses-permission 536 android:name="android.permission.ACCESS_NETWORK_STATE" /> 537 538 <!-- Required to check whether Wi-Fi is enabled --> 539 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 540 541 <!-- Required to read and write the expansion files on shared storage --> 542 <uses-permission 543 android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 544 ... 545</manifest> 546</pre> 547 548<p class="note"><strong>Note:</strong> By default, the Downloader Library requires API 549level 4, but the APK Expansion Zip Library requires API level 5.</p> 550 551 552<h3 id="DownloaderService">Implementing the downloader service</h3> 553 554<p>In order to perform downloads in the background, the Downloader Library provides its 555own {@link android.app.Service} subclass called {@code DownloaderService} that you should extend. In 556addition to downloading the expansion files for you, the {@code DownloaderService} also:</p> 557 558<ul> 559 <li>Registers a {@link android.content.BroadcastReceiver} that listens for changes to the 560device's network connectivity (the {@link android.net.ConnectivityManager#CONNECTIVITY_ACTION} 561broadcast) in order to pause the download when necessary (such as due to connectivity loss) and 562resume the download when possible (connectivity is acquired).</li> 563 <li>Schedules an {@link android.app.AlarmManager#RTC_WAKEUP} alarm to retry the download for 564cases in which the service gets killed.</li> 565 <li>Builds a custom {@link android.app.Notification} that displays the download progress and 566any errors or state changes.</li> 567 <li>Allows your application to manually pause and resume the download.</li> 568 <li>Verifies that the shared storage is mounted and available, that the files don't already exist, 569and that there is enough space, all before downloading the expansion files. Then notifies the user 570if any of these are not true.</li> 571</ul> 572 573<p>All you need to do is create a class in your application that extends the {@code 574DownloaderService} class and override three methods to provide specific application details:</p> 575 576<dl> 577 <dt>{@code getPublicKey()}</dt> 578 <dd>This must return a string that is the Base64-encoded RSA public key for your publisher 579account, available from the profile page on the Developer Console (see <a 580href="{@docRoot}google/play/licensing/setting-up.html">Setting Up for Licensing</a>).</dd> 581 <dt>{@code getSALT()}</dt> 582 <dd>This must return an array of random bytes that the licensing {@code Policy} uses to 583create an <a 584href="{@docRoot}google/play/licensing/adding-licensing.html#impl-Obfuscator">{@code 585Obfuscator}</a>. The salt ensures that your obfuscated {@link android.content.SharedPreferences} 586file in which your licensing data is saved will be unique and non-discoverable.</dd> 587 <dt>{@code getAlarmReceiverClassName()}</dt> 588 <dd>This must return the class name of the {@link android.content.BroadcastReceiver} in 589your application that should receive the alarm indicating that the download should be 590restarted (which might happen if the downloader service unexpectedly stops).</dd> 591</dl> 592 593<p>For example, here's a complete implementation of {@code DownloaderService}:</p> 594 595<pre> 596public class SampleDownloaderService extends DownloaderService { 597 // You must use the public key belonging to your publisher account 598 public static final String BASE64_PUBLIC_KEY = "YourLVLKey"; 599 // You should also modify this salt 600 public static final byte[] SALT = new byte[] { 1, 42, -12, -1, 54, 98, 601 -100, -12, 43, 2, -8, -4, 9, 5, -106, -107, -33, 45, -1, 84 602 }; 603 604 @Override 605 public String getPublicKey() { 606 return BASE64_PUBLIC_KEY; 607 } 608 609 @Override 610 public byte[] getSALT() { 611 return SALT; 612 } 613 614 @Override 615 public String getAlarmReceiverClassName() { 616 return SampleAlarmReceiver.class.getName(); 617 } 618} 619</pre> 620 621<p class="caution"><strong>Notice:</strong> You must update the {@code BASE64_PUBLIC_KEY} value 622to be the public key belonging to your publisher account. You can find the key in the Developer 623Console under your profile information. This is necessary even when testing 624your downloads.</p> 625 626<p>Remember to declare the service in your manifest file:</p> 627<pre> 628<application ...> 629 <service android:name=".SampleDownloaderService" /> 630 ... 631</application> 632</pre> 633 634 635 636<h3 id="AlarmReceiver">Implementing the alarm receiver</h3> 637 638<p>In order to monitor the progress of the file downloads and restart the download if necessary, the 639{@code DownloaderService} schedules an {@link android.app.AlarmManager#RTC_WAKEUP} alarm that 640delivers an {@link android.content.Intent} to a {@link android.content.BroadcastReceiver} in your 641application. You must define the {@link android.content.BroadcastReceiver} to call an API 642from the Downloader Library that checks the status of the download and restarts 643it if necessary.</p> 644 645<p>You simply need to override the {@link android.content.BroadcastReceiver#onReceive 646onReceive()} method to call {@code 647DownloaderClientMarshaller.startDownloadServiceIfRequired()}.</p> 648 649<p>For example:</p> 650 651<pre> 652public class SampleAlarmReceiver extends BroadcastReceiver { 653 @Override 654 public void onReceive(Context context, Intent intent) { 655 try { 656 DownloaderClientMarshaller.startDownloadServiceIfRequired(context, 657 intent, SampleDownloaderService.class); 658 } catch (NameNotFoundException e) { 659 e.printStackTrace(); 660 } 661 } 662} 663</pre> 664 665<p>Notice that this is the class for which you must return the name 666in your service's {@code getAlarmReceiverClassName()} method (see the previous section).</p> 667 668<p>Remember to declare the receiver in your manifest file:</p> 669<pre> 670<application ...> 671 <receiver android:name=".SampleAlarmReceiver" /> 672 ... 673</application> 674</pre> 675 676 677 678<h3 id="Download">Starting the download</h3> 679 680<p>The main activity in your application (the one started by your launcher icon) is 681responsible for verifying whether the expansion files are already on the device and initiating 682the download if they are not.</p> 683 684<p>Starting the download using the Downloader Library requires the following 685procedures:</p> 686 687<ol> 688 <li>Check whether the files have been downloaded. 689 <p>The Downloader Library includes some APIs in the {@code Helper} class to 690help with this process:</p> 691 <ul> 692 <li>{@code getExpansionAPKFileName(Context, c, boolean mainFile, int 693versionCode)}</li> 694 <li>{@code doesFileExist(Context c, String fileName, long fileSize)}</li> 695 </ul> 696 <p>For example, the sample app provided in the Apk Expansion package calls the 697following method in the activity's {@link android.app.Activity#onCreate onCreate()} method to check 698whether the expansion files already exist on the device:</p> 699 700<pre> 701boolean expansionFilesDelivered() { 702 for (XAPKFile xf : xAPKS) { 703 String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsBase, 704 xf.mFileVersion); 705 if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false)) 706 return false; 707 } 708 return true; 709} 710</pre> 711 712 <p>In this case, each {@code XAPKFile} object holds the version number and file size of a known 713expansion file and a boolean as to whether it's the main expansion file. (See the sample 714application's {@code SampleDownloaderActivity} class for details.)</p> 715 <p>If this method returns false, then the application must begin the download.</p> 716 </li> 717 <li>Start the download by calling the static method {@code 718DownloaderClientMarshaller.startDownloadServiceIfRequired(Context c, PendingIntent 719notificationClient, Class<?> serviceClass)}. 720 <p>The method takes the following parameters:</p> 721 <ul> 722 <li><code>context</code>: Your application's {@link android.content.Context}.</li> 723 <li><code>notificationClient</code>: A {@link android.app.PendingIntent} to start your main 724activity. This is used in the {@link android.app.Notification} that the {@code DownloaderService} 725creates to show the download progress. When the user selects the notification, the system 726invokes the {@link android.app.PendingIntent} you supply here and should open the activity 727that shows the download progress (usually the same activity that started the download).</li> 728 <li><code>serviceClass</code>: The {@link java.lang.Class} object for your implementation of 729{@code DownloaderService}, required to start the service and begin the download if necessary.</li> 730 </ul> 731 <p>The method returns an integer that indicates 732whether or not the download is required. Possible values are:</p> 733 <ul> 734 <li>{@code NO_DOWNLOAD_REQUIRED}: Returned if the files already 735exist or a download is already in progress.</li> 736 <li>{@code LVL_CHECK_REQUIRED}: Returned if a license verification is 737required in order to acquire the expansion file URLs.</li> 738 <li>{@code DOWNLOAD_REQUIRED}: Returned if the expansion file URLs are already known, 739but have not been downloaded.</li> 740 </ul> 741 <p>The behavior for {@code LVL_CHECK_REQUIRED} and {@code DOWNLOAD_REQUIRED} are essentially the 742same and you normally don't need to be concerned about them. In your main activity that calls {@code 743startDownloadServiceIfRequired()}, you can simply check whether or not the response is {@code 744NO_DOWNLOAD_REQUIRED}. If the response is anything <em>other than</em> {@code NO_DOWNLOAD_REQUIRED}, 745the Downloader Library begins the download and you should update your activity UI to 746display the download progress (see the next step). If the response <em>is</em> {@code 747NO_DOWNLOAD_REQUIRED}, then the files are available and your application can start.</p> 748 <p>For example:</p> 749 750<pre> 751@Override 752public void onCreate(Bundle savedInstanceState) { 753 // Check if expansion files are available before going any further 754 if (!expansionFilesDelivered()) { 755 // Build an Intent to start this activity from the Notification 756 Intent notifierIntent = new Intent(this, MainActivity.getClass()); 757 notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | 758 Intent.FLAG_ACTIVITY_CLEAR_TOP); 759 ... 760 PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, 761 notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT); 762 763 // Start the download service (if required) 764 int startResult = 765 DownloaderClientMarshaller.startDownloadServiceIfRequired(this, 766 pendingIntent, SampleDownloaderService.class); 767 // If download has started, initialize this activity to show 768 // download progress 769 if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) { 770 // This is where you do set up to display the download 771 // progress (next step) 772 ... 773 return; 774 } // If the download wasn't necessary, fall through to start the app 775 } 776 startApp(); // Expansion files are available, start the app 777} 778</pre> 779 780 </li> 781 <li>When the {@code startDownloadServiceIfRequired()} method returns anything <em>other 782than</em> {@code NO_DOWNLOAD_REQUIRED}, create an instance of {@code IStub} by 783calling {@code DownloaderClientMarshaller.CreateStub(IDownloaderClient client, Class<?> 784downloaderService)}. The {@code IStub} provides a binding between your activity to the downloader 785service such that your activity receives callbacks about the download progress. 786 <p>In order to instantiate your {@code IStub} by calling {@code CreateStub()}, you must pass it 787an implementation of the {@code IDownloaderClient} interface and your {@code DownloaderService} 788implementation. The next section about <a href="#Progress">Receiving download progress</a> discusses 789the {@code IDownloaderClient} interface, which you should usually implement in your {@link 790android.app.Activity} class so you can update the activity UI when the download state changes.</p> 791 <p>We recommend that you call {@code 792CreateStub()} to instantiate your {@code IStub} during your activity's {@link 793android.app.Activity#onCreate onCreate()} method, after {@code startDownloadServiceIfRequired()} 794starts the download. </p> 795 <p>For example, in the previous code sample for {@link android.app.Activity#onCreate 796onCreate()}, you can respond to the {@code startDownloadServiceIfRequired()} result like this:</p> 797 798<pre> 799 // Start the download service (if required) 800 int startResult = 801 DownloaderClientMarshaller.startDownloadServiceIfRequired(this, 802 pendingIntent, SampleDownloaderService.class); 803 // If download has started, initialize activity to show progress 804 if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) { 805 // Instantiate a member instance of IStub 806 mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this, 807 SampleDownloaderService.class); 808 // Inflate layout that shows download progress 809 setContentView(R.layout.downloader_ui); 810 return; 811 } 812</pre> 813 814 <p>After the {@link android.app.Activity#onCreate onCreate()} method returns, your activity 815receives a call to {@link android.app.Activity#onResume onResume()}, which is where you should then 816call {@code connect()} on the {@code IStub}, passing it your application's {@link 817android.content.Context}. Conversely, you should call 818{@code disconnect()} in your activity's {@link android.app.Activity#onStop onStop()} callback.</p> 819<pre> 820@Override 821protected void onResume() { 822 if (null != mDownloaderClientStub) { 823 mDownloaderClientStub.connect(this); 824 } 825 super.onResume(); 826} 827 828@Override 829protected void onStop() { 830 if (null != mDownloaderClientStub) { 831 mDownloaderClientStub.disconnect(this); 832 } 833 super.onStop(); 834} 835</pre> 836 <p>Calling {@code connect()} on the {@code IStub} binds your activity to the {@code 837DownloaderService} such that your activity receives callbacks regarding changes to the download 838state through the {@code IDownloaderClient} interface.</p> 839 </li> 840</ol> 841 842 843 844<h3 id="Progress">Receiving download progress</h3> 845 846<p>To receive updates regarding the download progress and to interact with the {@code 847DownloaderService}, you must implement the Downloader Library's {@code IDownloaderClient} interface. 848Usually, the activity you use to start the download should implement this interface in order to 849display the download progress and send requests to the service.</p> 850 851<p>The required interface methods for {@code IDownloaderClient} are:</p> 852 853<dl> 854 <dt>{@code onServiceConnected(Messenger m)}</dt> 855 <dd>After you instantiate the {@code IStub} in your activity, you'll receive a call to this 856method, which passes a {@link android.os.Messenger} object that's connected with your instance 857of {@code DownloaderService}. To send requests to the service, such as to pause and resume 858downloads, you must call {@code DownloaderServiceMarshaller.CreateProxy()} to receive the {@code 859IDownloaderService} interface connected to the service. 860 <p>A recommended implementation looks like this:</p> 861<pre> 862private IDownloaderService mRemoteService; 863... 864 865@Override 866public void onServiceConnected(Messenger m) { 867 mRemoteService = DownloaderServiceMarshaller.CreateProxy(m); 868 mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger()); 869} 870</pre> 871 <p>With the {@code IDownloaderService} object initialized, you can send commands to the 872downloader service, such as to pause and resume the download ({@code requestPauseDownload()} 873and {@code requestContinueDownload()}).</p> 874</dd> 875 <dt>{@code onDownloadStateChanged(int newState)}</dt> 876 <dd>The download service calls this when a change in download state occurs, such as the 877download begins or completes. 878 <p>The <code>newState</code> value will be one of several possible values specified in 879by one of the {@code IDownloaderClient} class's {@code STATE_*} constants.</p> 880 <p>To provide a useful message to your users, you can request a corresponding string 881for each state by calling {@code Helpers.getDownloaderStringResourceIDFromState()}. This 882returns the resource ID for one of the strings bundled with the Downloader 883Library. For example, the string "Download paused because you are roaming" corresponds to {@code 884STATE_PAUSED_ROAMING}.</p></dd> 885 <dt>{@code onDownloadProgress(DownloadProgressInfo progress)}</dt> 886 <dd>The download service calls this to deliver a {@code DownloadProgressInfo} object, 887which describes various information about the download progress, including estimated time remaining, 888current speed, overall progress, and total so you can update the download progress UI.</dd> 889</dl> 890<p class="note"><strong>Tip:</strong> For examples of these callbacks that update the download 891progress UI, see the {@code SampleDownloaderActivity} in the sample app provided with the 892Apk Expansion package.</p> 893 894<p>Some public methods for the {@code IDownloaderService} interface you might find useful are:</p> 895 896<dl> 897 <dt>{@code requestPauseDownload()}</dt> 898 <dd>Pauses the download.</dd> 899 <dt>{@code requestContinueDownload()}</dt> 900 <dd>Resumes a paused download.</dd> 901 <dt>{@code setDownloadFlags(int flags)}</dt> 902 <dd>Sets user preferences for network types on which its OK to download the files. The 903current implementation supports one flag, {@code FLAGS_DOWNLOAD_OVER_CELLULAR}, but you can add 904others. By default, this flag is <em>not</em> enabled, so the user must be on Wi-Fi to download 905expansion files. You might want to provide a user preference to enable downloads over 906the cellular network. In which case, you can call: 907<pre> 908mRemoteService 909 .setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR); 910</pre> 911</dd> 912</dl> 913 914 915 916 917<h2 id="ExpansionPolicy">Using APKExpansionPolicy</h2> 918 919<p>If you decide to build your own downloader service instead of using the Google Play 920<a href="#AboutLibraries">Downloader Library</a>, you should still use the {@code 921APKExpansionPolicy} that's provided in the License Verification Library. The {@code 922APKExpansionPolicy} class is nearly identical to {@code ServerManagedPolicy} (available in the 923Google Play License Verification Library) but includes additional handling for the APK expansion 924file response extras.</p> 925 926<p class="note"><strong>Note:</strong> If you <em>do use</em> the <a 927href="#AboutLibraries">Downloader Library</a> as discussed in the previous section, the 928library performs all interaction with the {@code APKExpansionPolicy} so you don't have to use 929this class directly.</p> 930 931<p>The class includes methods to help you get the necessary information about the available 932expansion files:</p> 933 934<ul> 935 <li>{@code getExpansionURLCount()}</li> 936 <li>{@code getExpansionURL(int index)}</li> 937 <li>{@code getExpansionFileName(int index)}</li> 938 <li>{@code getExpansionFileSize(int index)}</li> 939</ul> 940 941<p>For more information about how to use the {@code APKExpansionPolicy} when you're <em>not</em> 942using the <a 943href="#AboutLibraries">Downloader Library</a>, see the documentation for <a 944href="{@docRoot}google/play/licensing/adding-licensing.html">Adding Licensing to Your App</a>, 945which explains how to implement a license policy such as this one.</p> 946 947 948 949 950 951 952 953<h2 id="ReadingTheFile">Reading the Expansion File</h2> 954 955<p>Once your APK expansion files are saved on the device, how you read your files 956depends on the type of file you've used. As discussed in the <a href="#Overview">overview</a>, your 957expansion files can be any kind of file you 958want, but are renamed using a particular <a href="#Filename">file name format</a> and are saved to 959{@code <shared-storage>/Android/obb/<package-name>/}.</p> 960 961<p>Regardless of how you read your files, you should always first check that the external 962storage is available for reading. There's a chance that the user has the storage mounted to a 963computer over USB or has actually removed the SD card.</p> 964 965<p class="note"><strong>Note:</strong> When your application starts, you should always check whether 966the external storage space is available and readable by calling {@link 967android.os.Environment#getExternalStorageState()}. This returns one of several possible strings 968that represent the state of the external storage. In order for it to be readable by your 969application, the return value must be {@link android.os.Environment#MEDIA_MOUNTED}.</p> 970 971 972<h3 id="GettingFilenames">Getting the file names</h3> 973 974<p>As described in the <a href="#Overview">overview</a>, your APK expansion files are saved 975using a specific file name format:</p> 976 977<pre class="classic no-pretty-print"> 978[main|patch].<expansion-version>.<package-name>.obb 979</pre> 980 981<p>To get the location and names of your expansion files, you should use the 982{@link android.os.Environment#getExternalStorageDirectory()} and {@link 983android.content.Context#getPackageName()} methods to construct the path to your files.</p> 984 985<p>Here's a method you can use in your application to get an array containing the complete path 986to both your expansion files:</p> 987 988<pre> 989// The shared path to all app expansion files 990private final static String EXP_PATH = "/Android/obb/"; 991 992static String[] getAPKExpansionFiles(Context ctx, int mainVersion, 993 int patchVersion) { 994 String packageName = ctx.getPackageName(); 995 Vector<String> ret = new Vector<String>(); 996 if (Environment.getExternalStorageState() 997 .equals(Environment.MEDIA_MOUNTED)) { 998 // Build the full path to the app's expansion files 999 File root = Environment.getExternalStorageDirectory(); 1000 File expPath = new File(root.toString() + EXP_PATH + packageName); 1001 1002 // Check that expansion file path exists 1003 if (expPath.exists()) { 1004 if ( mainVersion > 0 ) { 1005 String strMainPath = expPath + File.separator + "main." + 1006 mainVersion + "." + packageName + ".obb"; 1007 File main = new File(strMainPath); 1008 if ( main.isFile() ) { 1009 ret.add(strMainPath); 1010 } 1011 } 1012 if ( patchVersion > 0 ) { 1013 String strPatchPath = expPath + File.separator + "patch." + 1014 mainVersion + "." + packageName + ".obb"; 1015 File main = new File(strPatchPath); 1016 if ( main.isFile() ) { 1017 ret.add(strPatchPath); 1018 } 1019 } 1020 } 1021 } 1022 String[] retArray = new String[ret.size()]; 1023 ret.toArray(retArray); 1024 return retArray; 1025} 1026</pre> 1027 1028<p>You can call this method by passing it your application {@link android.content.Context} 1029and the desired expansion file's version.</p> 1030 1031<p>There are many ways you could determine the expansion file version number. One simple way is to 1032save the version in a {@link android.content.SharedPreferences} file when the download begins, by 1033querying the expansion file name with the {@code APKExpansionPolicy} class's {@code 1034getExpansionFileName(int index)} method. You can then get the version code by reading the {@link 1035android.content.SharedPreferences} file when you want to access the expansion 1036file.</p> 1037 1038<p>For more information about reading from the shared storage, see the <a 1039href="{@docRoot}guide/topics/data/data-storage.html#filesExternal">Data Storage</a> 1040documentation.</p> 1041 1042 1043 1044<h3 id="ZipLib">Using the APK Expansion Zip Library</h3> 1045 1046<div class="sidebox-wrapper"> 1047<div class="sidebox"> 1048 <h3>Reading media files from a ZIP</h3> 1049 <p>If you're using your expansion files to store media files, a ZIP file still allows you to 1050use Android media playback calls that provide offset and length controls (such as {@link 1051android.media.MediaPlayer#setDataSource(FileDescriptor,long,long) MediaPlayer.setDataSource()} and 1052{@link android.media.SoundPool#load(FileDescriptor,long,long,int) SoundPool.load()}). In order for 1053this to work, you must not perform additional compression on the media files when creating the ZIP 1054packages. For example, when using the <code>zip</code> tool, you should use the <code>-n</code> 1055option to specify the file suffixes that should not be compressed:</p> 1056<p><code>zip -n .mp4;.ogg main_expansion media_files</code></p> 1057</div> 1058</div> 1059 1060<p>The Google Market Apk Expansion package includes a library called the APK 1061Expansion Zip Library (located in {@code 1062<sdk>/extras/google/google_market_apk_expansion/zip_file/}). This is an optional library that 1063helps you read your expansion 1064files when they're saved as ZIP files. Using this library allows you to easily read resources from 1065your ZIP expansion files as a virtual file system.</p> 1066 1067<p>The APK Expansion Zip Library includes the following classes and APIs:</p> 1068 1069<dl> 1070 <dt>{@code APKExpansionSupport}</dt> 1071 <dd>Provides some methods to access expansion file names and ZIP files: 1072 1073 <dl style="margin-top:1em"> 1074 <dt>{@code getAPKExpansionFiles()}</dt> 1075 <dd>The same method shown above that returns the complete file path to both expansion 1076files.</dd> 1077 <dt>{@code getAPKExpansionZipFile(Context ctx, int mainVersion, int 1078patchVersion)}</dt> 1079 <dd>Returns a {@code ZipResourceFile} representing the sum of both the main file and 1080patch file. That is, if you specify both the <code>mainVersion</code> and the 1081<code>patchVersion</code>, this returns a {@code ZipResourceFile} that provides read access to 1082all the data, with the patch file's data merged on top of the main file.</dd> 1083 </dl> 1084 </dd> 1085 1086 <dt>{@code ZipResourceFile}</dt> 1087 <dd>Represents a ZIP file on the shared storage and performs all the work to provide a virtual 1088file system based on your ZIP files. You can get an instance using {@code 1089APKExpansionSupport.getAPKExpansionZipFile()} or with the {@code ZipResourceFile} by passing it the 1090path to your expansion file. This class includes a variety of useful methods, but you generally 1091don't need to access most of them. A couple of important methods are: 1092 1093 <dl style="margin-top:1em"> 1094 <dt>{@code getInputStream(String assetPath)}</dt> 1095 <dd>Provides an {@link java.io.InputStream} to read a file within the ZIP file. The 1096<code>assetPath</code> must be the path to the desired file, relative to 1097the root of the ZIP file contents.</dd> 1098 <dt>{@code getAssetFileDescriptor(String assetPath)}</dt> 1099 <dd>Provides an {@link android.content.res.AssetFileDescriptor} for a file within the 1100ZIP file. The <code>assetPath</code> must be the path to the desired file, relative to 1101the root of the ZIP file contents. This is useful for certain Android APIs that require an {@link 1102android.content.res.AssetFileDescriptor}, such as some {@link android.media.MediaPlayer} APIs.</dd> 1103 </dl> 1104 </dd> 1105 1106 <dt>{@code APEZProvider}</dt> 1107 <dd>Most applications don't need to use this class. This class defines a {@link 1108android.content.ContentProvider} that marshals the data from the ZIP files through a content 1109provider {@link android.net.Uri} in order to provide file access for certain Android APIs that 1110expect {@link android.net.Uri} access to media files. For example, this is useful if you want to 1111play a video with {@link android.widget.VideoView#setVideoURI VideoView.setVideoURI()}.</p></dd> 1112</dl> 1113 1114<h4>Reading from a ZIP file</h4> 1115 1116<p>When using the APK Expansion Zip Library, reading a file from your ZIP usually requires the 1117following:</p> 1118 1119<pre> 1120// Get a ZipResourceFile representing a merger of both the main and patch files 1121ZipResourceFile expansionFile = 1122 APKExpansionSupport.getAPKExpansionZipFile(appContext, 1123 mainVersion, patchVersion); 1124 1125// Get an input stream for a known file inside the expansion file ZIPs 1126InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip); 1127</pre> 1128 1129<p>The above code provides access to any file that exists in either your main expansion file or 1130patch expansion file, by reading from a merged map of all the files from both files. All you 1131need to provide the {@code getAPKExpansionFile()} method is your application {@code 1132android.content.Context} and the version number for both the main expansion file and patch 1133expansion file.</p> 1134 1135<p>If you'd rather read from a specific expansion file, you can use the {@code 1136ZipResourceFile} constructor with the path to the desired expansion file:</p> 1137 1138<pre> 1139// Get a ZipResourceFile representing a specific expansion file 1140ZipResourceFile expansionFile = new ZipResourceFile(filePathToMyZip); 1141 1142// Get an input stream for a known file inside the expansion file ZIPs 1143InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip); 1144</pre> 1145 1146<p>For more information about using this library for your expansion files, look at 1147the sample application's {@code SampleDownloaderActivity} class, which includes additional code to 1148verify the downloaded files using CRC. Beware that if you use this sample as the basis for 1149your own implementation, it requires that you <strong>declare the byte size of your expansion 1150files</strong> in the {@code xAPKS} array.</p> 1151 1152 1153 1154 1155<h2 id="Testing">Testing Your Expansion Files</h2> 1156 1157<p>Before publishing your application, there are two things you should test: Reading the 1158expansion files and downloading the files.</p> 1159 1160 1161<h3 id="TestingReading">Testing file reads</h3> 1162 1163<p>Before you upload your application to Google Play, you 1164should test your application's ability to read the files from the shared storage. All you need to do 1165is add the files to the appropriate location on the device shared storage and launch your 1166application:</p> 1167 1168<ol> 1169 <li>On your device, create the appropriate directory on the shared storage where Google 1170Play will save your files. 1171 <p>For example, if your package name is {@code com.example.android}, you need to create 1172the directory {@code Android/obb/com.example.android/} on the shared storage space. (Plug in 1173your test device to your computer to mount the shared storage and manually create this 1174directory.)</p> 1175 </li> 1176 <li>Manually add the expansion files to that directory. Be sure that you rename your files to 1177match the <a href="#Filename">file name format</a> that Google Play will use. 1178 <p>For example, regardless of the file type, the main expansion file for the {@code 1179com.example.android} application should be {@code main.0300110.com.example.android.obb}. 1180The version code can be whatever value you want. Just remember:</p> 1181 <ul> 1182 <li>The main expansion file always starts with {@code main} and the patch file starts with 1183{@code patch}.</li> 1184 <li>The package name always matches that of the APK to which the file is attached on 1185Google Play. 1186 </ul> 1187 </li> 1188 <li>Now that the expansion file(s) are on the device, you can install and run your application to 1189test your expansion file(s).</li> 1190</ol> 1191 1192<p>Here are some reminders about handling the expansion files:</p> 1193<ul> 1194 <li><strong>Do not delete or rename</strong> the {@code .obb} expansion files (even if you unpack 1195the data to a different location). Doing so will cause Google Play (or your app itself) to 1196repeatedly download the expansion file.</li> 1197 <li><strong>Do not save other data into your <code>obb/</code> 1198directory</strong>. If you must unpack some data, save it into the location specified by {@link 1199android.content.Context#getExternalFilesDir getExternalFilesDir()}.</li> 1200</ul> 1201 1202 1203 1204<h3 id="TestingReading">Testing file downloads</h3> 1205 1206<p>Because your application must sometimes manually download the expansion files when it first 1207opens, it's important that you test this process to be sure your application can successfully query 1208for the URLs, download the files, and save them to the device.</p> 1209 1210<p>To test your application's implementation of the manual download procedure, 1211you can publish it to the alpha or beta channel, so it will only be available to 1212authorized testers. 1213If everything works as expected, your application should begin downloading the expansion 1214files as soon as the main activity starts.</p> 1215 1216<p class="note"><strong>Note:</strong> Previously you could test an app by 1217uploading an unpublished "draft" version. This functionality is no longer 1218supported; instead, you must publish it to the alpha or beta distribution 1219channel. For more information, see <a 1220href="{@docRoot}google/play/billing/billing_testing.html#draft_apps">Draft Apps 1221are No Longer Supported</a>. 1222 1223<h2 id="Updating">Updating Your Application</h2> 1224 1225<p>One of the great benefits to using expansion files on Google Play is the ability to 1226update your application without re-downloading all of the original assets. Because Google Play 1227allows you to provide two expansion files with each APK, you can use the second file as a "patch" 1228that provides updates and new assets. Doing so avoids the 1229need to re-download the main expansion file which could be large and expensive for users.</p> 1230 1231<p>The patch expansion file is technically the same as the main expansion file and neither 1232the Android system nor Google Play perform actual patching between your main and patch expansion 1233files. Your application code must perform any necessary patches itself.</p> 1234 1235<p>If you use ZIP files as your expansion files, the <a href="#ZipLib">APK Expansion Zip 1236Library</a> that's included with the Apk Expansion package includes the ability to merge 1237your 1238patch file with the main expansion file.</p> 1239 1240<p class="note"><strong>Note:</strong> Even if you only need to make changes to the patch 1241expansion file, you must still update the APK in order for Google Play to perform an update. 1242If you don't require code changes in the application, you should simply update the <a 1243href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a> in the 1244manifest.</p> 1245 1246<p>As long as you don't change the main expansion file that's associated with the APK 1247in the Developer Console, users who previously installed your application will not 1248download the main expansion file. Existing users receive only the updated APK and the new patch 1249expansion file (retaining the previous main expansion file).</p> 1250 1251<p>Here are a few issues to keep in mind regarding updates to expansion files:</p> 1252 1253<ul> 1254 <li>There can be only two expansion files for your application at a time. One main expansion 1255file and one patch expansion file. During an update to a file, Google Play deletes the 1256previous version (and so must your application when performing manual updates).</li> 1257 <li>When adding a patch expansion file, the Android system does not actually patch your 1258application or main expansion file. You must design your application to support the patch data. 1259However, the Apk Expansion package includes a library for using ZIP files 1260as expansion files, which merges the data from the patch file into the main expansion file so 1261you can easily read all the expansion file data.</li> 1262</ul> 1263 1264 1265 1266<!-- Tools are not ready. 1267 1268<h3>Using OBB tool and APIs</h3> 1269 1270<pre> 1271$ mkobb.sh -d /data/myfiles -k my_secret_key -o /data/data.obb 1272$ obbtool a -n com.example.myapp -v 1 -s seed_from_mkobb /data/data.obb 1273</pre> 1274 1275<pre> 1276storage = (StorageManager) getSystemService( STORAGE_SERVICE ); 1277storage.mountObb( obbFilepath, "my_secret_key", myListener ); 1278obbContentPath = storage.getMountedObbPath( obbFilepath ); 1279</pre> 1280--> 1281