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