summaryrefslogtreecommitdiffstats
path: root/docs/html/google/play/expansion-files.jd
blob: 271c19210ffa06f41e45c35e7ffff936f5344eaf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
page.title=APK Expansion Files
@jd:body


<div id="qv-wrapper">
<div id="qv">
<h2>Quickview</h2>
<ul>
  <li>Recommended for most apps that exceed the 50MB APK limit</li>
  <li>You can provide up to 4GB of additional data for each APK</li>
  <li>Google Play hosts and serves the expansion files at no charge</li>
  <li>The files can be any file type you want and are saved to the device's shared storage</li>
</ul>

<h2>In this document</h2>
<ol>
  <li><a href="#Overview">Overview</a>
    <ol>
      <li><a href="#Filename">File name format</a></li>
      <li><a href="#StorageLocation">Storage location</a></li>
      <li><a href="#DownloadProcess">Download process</a></li>
      <li><a href="#Checklist">Development checklist</a></li>
    </ol>
  </li>
  <li><a href="#Rules">Rules and Limitations</a></li>
  <li><a href="#Downloading">Downloading the Expansion Files</a>
    <ol>
      <li><a href="#AboutLibraries">About the Downloader Library</a></li>
      <li><a href="#Preparing">Preparing to use the Downloader Library</a></li>
      <li><a href="#Permissions">Declaring user permissions</a></li>
      <li><a href="#DownloaderService">Implementing the downloader service</a></li>
      <li><a href="#AlarmReceiver">Implementing the alarm receiver</a></li>
      <li><a href="#Download">Starting the download</a></li>
      <li><a href="#Progress">Receiving download progress</a></li>
    </ol>
  </li>
  <li><a href="#ExpansionPolicy">Using APKExpansionPolicy</a></li>
  <li><a href="#ReadingTheFile">Reading the Expansion File</a>
    <ol>
      <li><a href="#GettingFilenames">Getting the file names</a></li>
      <li><a href="#ZipLib">Using the APK Expansion Zip Library</a></li>
    </ol>
  </li>
  <li><a href="#Testing">Testing Your Expansion Files</a>
    <ol>
      <li><a href="#TestingReading">Testing file reads</a></li>
      <li><a href="#TestingReading">Testing file downloads</a></li>
    </ol>
  </li>
  <li><a href="#Updating">Updating Your Application</a></li>
</ol>

<h2>See also</h2>
<ol>
  <li><a href="{@docRoot}google/play/licensing/index.html">Application Licensing</a></li>
  <li><a href="{@docRoot}google/play/publishing/multiple-apks.html">Multiple
APK Support</a></li>
</ol>
</div>
</div>



<p>Google Play currently requires that your APK file be no more than 50MB. For most
applications, this is plenty of space for all the application's code and assets.
However, some apps need more space for high-fidelity graphics, media files, or other large assets.
Previously, if your app exceeded 50MB, you had to host and download the additional resources
yourself when the user opens the app. Hosting and serving the extra files can be costly, and the
user experience is often less than ideal. To make this process easier for you and more pleasant
for users, Google Play allows you to attach two large expansion files that supplement your
APK.</p>

<p>Google Play hosts the expansion files for your application and serves them to the device at
no cost to you. The expansion files are saved to the device's shared storage location (the
SD card or USB-mountable partition; also known as the "external" storage) where your app can access
them. On most devices, Google Play downloads the expansion file(s) at the same time it
downloads the APK, so your application has everything it needs when the user opens it for the
first time. In some cases, however, your application must download the files from Google Play
when your application starts.</p>



<h2 id="Overview">Overview</h2>

<p>Each time you upload an APK using the Google Play Android Developer Console, you have the option to
add one or two expansion files to the APK. Each file can be up to 2GB and it can be any format you
choose, but we recommend you use a compressed file to conserve bandwidth during the download.
Conceptually, each expansion file plays a different role:</p>

<ul>
  <li>The <strong>main</strong> expansion file is the
primary expansion file for additional resources required by your application.</li>
  <li>The <strong>patch</strong> expansion file is optional and intended for small updates to the
main expansion file.</li>
</ul>

<p>While you can use the two expansion files any way you wish, we recommend that the main
expansion file deliver the primary assets and should rarely if ever updated; the patch expansion
file should be smaller and serve as a “patch carrier,” getting updated with each major
release or as necessary.</p>

<p>However, even if your application update requires only a new patch expansion file, you still must
upload a new APK with an updated <a
href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code
versionCode}</a> in the manifest. (The
Developer Console does not allow you to upload an expansion file to an existing APK.)</p>

<p class="note"><strong>Note:</strong> The patch expansion file is semantically the same as the
main expansion file&mdash;you can use each file any way you want. The system does
not use the patch expansion file to perform patching for your app. You must perform patching
yourself or be able to distinguish between the two files.</p>



<h3 id="Filename">File name format</h3>

<p>Each expansion file you upload can be any format you choose (ZIP, PDF, MP4, etc.). You can also
use the <a href="{@docRoot}tools/help/jobb.html">JOBB</a> tool to encapsulate and encrypt a set
of resource files and subsequent patches for that set. Regardless of the file type, Google Play
considers them opaque binary blobs and renames the files using the following scheme:</p>

<pre class="classic no-pretty-print">
[main|patch].&lt;expansion-version&gt;.&lt;package-name&gt;.obb
</pre>

<p>There are three components to this scheme:</p>

<dl>
  <dt>{@code main} or {@code patch}</dt>
    <dd>Specifies whether the file is the main or patch expansion file. There can be
only one main file and one patch file for each APK.</dd>
  <dt>{@code &lt;expansion-version&gt;}</dt>
    <dd>This is an integer that matches the version code of the APK with which the expansion is
<em>first</em> associated (it matches the application's <a
href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code android:versionCode}</a>
value).
    <p>"First" is emphasized because although the Developer Console allows you to
re-use an uploaded expansion file with a new APK, the expansion file's name does not change&mdash;it
retains the version applied to it when you first uploaded the file.</p></dd>
  <dt>{@code &lt;package-name&gt;}</dt>
    <dd>Your application's Java-style package name.</dd>
</dl>

<p>For example, suppose your APK version is 314159 and your package name is com.example.app. If you
upload a main expansion file, the file is renamed to:</p>
<pre class="classic no-pretty-print">main.314159.com.example.app.obb</pre>


<h3 id="StorageLocation">Storage location</h3>

<p>When Google Play downloads your expansion files to a device, it saves them to the system's
shared storage location. To ensure proper behavior, you must not delete, move, or rename the
expansion files. In the event that your application must perform the download from Google Play
itself, you must save the files to the exact same location.</p>

<p>The specific location for your expansion files is:</p>

<pre class="classic no-pretty-print">
&lt;shared-storage&gt;/Android/obb/&lt;package-name&gt;/
</pre>

<ul>
  <li>{@code &lt;shared-storage&gt;} is the path to the shared storage space, available from
{@link android.os.Environment#getExternalStorageDirectory()}.</li>
  <li>{@code &lt;package-name&gt;} is your application's Java-style package name, available
from {@link android.content.Context#getPackageName()}.</li>
</ul>

<p>For each application, there are never more than two expansion files in this directory.
One is the main expansion file and the other is the patch expansion file (if necessary). Previous
versions are overwritten when you update your application with new expansion files.</p>

<p>If you must unpack the contents of your expansion files, <strong>do not</strong> delete the
{@code .obb} expansion files afterwards and <strong>do not</strong> save the unpacked data
in the same directory. You should save your unpacked files in the directory
specified by {@link android.content.Context#getExternalFilesDir getExternalFilesDir()}. However,
if possible, it's best if you use an expansion file format that allows you to read directly from
the file instead of requiring you to unpack the data. For example, we've provided a library
project called the <a href="#ZipLib">APK Expansion Zip Library</a> that reads your data directly
from the ZIP file.</p>

<p class="note"><strong>Note:</strong> Unlike APK files, any files saved on the shared storage can
be read by the user and other applications.</p>

<p class="note"><strong>Tip:</strong> If you're packaging media files into a ZIP, you can use media
playback calls on the files with offset and length controls (such as {@link
android.media.MediaPlayer#setDataSource(FileDescriptor,long,long) MediaPlayer.setDataSource()} and
{@link android.media.SoundPool#load(FileDescriptor,long,long,int) SoundPool.load()}) without the
need to unpack your ZIP. In order for this to work, you must not perform additional compression on
the media files when creating the ZIP packages. For example, when using the <code>zip</code> tool,
you should use the <code>-n</code> option to specify the file suffixes that should not be
compressed: <br/>
<code>zip -n .mp4;.ogg main_expansion media_files</code></p>


<h3 id="DownloadProcess">Download process</h3>

<p>Most of the time, Google Play downloads and saves your expansion files at the same time it
downloads the APK to the device. However, in some cases Google Play
cannot download the expansion files or the user might have deleted previously downloaded expansion
files. To handle these situations, your app must be able to download the files
itself when the main activity starts, using a URL provided by Google Play.</p>

<p>The download process from a high level looks like this:</p>

<ol>
  <li>User selects to install your app from Google Play.</li>
  <li>If Google Play is able to download the expansion files (which is the case for most
devices), it downloads them along with the APK.
     <p>If Google Play is unable to download the expansion files, it downloads the
APK only.</p>
  </li>
  <li>When the user launches your application, your app must check whether the expansion files are
already saved on the device.
    <ol>
      <li>If yes, your app is ready to go.</li>
      <li>If no, your app must download the expansion files over HTTP from Google Play. Your app
must send a request to the Google Play client using the Google Play's <a
href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service, which
responds with the name, file size, and URL for each expansion file. With this information, you then
download the files and save them to the proper <a href="#StorageLocation">storage location</a>.</li>
    </ol>
  </li>
</ol>

<p class="caution"><strong>Caution:</strong> It is critical that you include the necessary code to
download the expansion files from Google Play in the event that the files are not already on the
device when your application starts. As discussed in the following section about <a
href="#Downloading">Downloading the Expansion Files</a>, we've made a library available to you that
greatly simplifies this process and performs the download from a service with a minimal amount of
code from you.</p>




<h3 id="Checklist">Development checklist</h3>

<p>Here's a summary of the tasks you should perform to use expansion files with your
application:</p>

<ol>
  <li>First determine whether your application absolutely requires more than 50MB per installation.
Space is precious and you should keep your total application size as small as possible. If your app
uses more than 50MB in order to provide multiple versions of your graphic assets for multiple screen
densities, consider instead publishing <a
href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> in which each APK
contains only the assets required for the screens that it targets.</li>
  <li>Determine which application resources to separate from your APK and package them in a
file to use as the main expansion file.
    <p>Normally, you should only use the second patch expansion file when performing updates to
the main expansion file. However, if your resources exceed the 2GB limit for the main
expansion file, you can use the patch file for the rest of your assets.</p>
  </li>
  <li>Develop your application such that it uses the resources from your expansion files in the
device's <a href="#StorageLocation">shared storage location</a>.
    <p>Remember that you must not delete, move, or rename the expansion files.</p>
    <p>If your application doesn't demand a specific format, we suggest you create ZIP files for
your expansion files, then read them using the <a href="#ZipLib">APK Expansion Zip
Library</a>.</p>
  </li>
  <li>Add logic to your application's main activity that checks whether the expansion files
are on the device upon start-up. If the files are not on the device, use Google Play's <a
href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service to request URLs
for the expansion files, then download and save them.
    <p>To greatly reduce the amount of code you must write and ensure a good user experience
during the download, we recommend you use the <a href="#AboutLibraries">Downloader
Library</a> to implement your download behavior.</p>
    <p>If you build your own download service instead of using the library, be aware that you
must not change the name of the expansion files and must save them to the proper
<a href="#StorageLocation">storage location</a>.</p></li>
</ol>

<p>Once you've finished your application development, follow the guide to <a href="#Testing">Testing
Your Expansion Files</a>.</p>






<h2 id="Rules">Rules and Limitations</h2>

<p>Adding APK expansion files is a feature available when you upload your application using the
Developer Console. When uploading your application for the first time or updating an
application that uses expansion files, you must be aware of the following rules and limitations:</p>

<ol type="I">
  <li>Each expansion file can be no more than 2GB.</li>
  <li>In order to download your expansion files from Google Play, <strong>the user must have
acquired your application from Google Play</strong>. Google Play will not
provide the URLs for your expansion files if the application was installed by other means.</li>
  <li>When performing the download from within your application, the URL that Google Play
provides for each file is unique for every download and each one expires shortly after it is given
to your application.</li>
  <li>If you update your application with a new APK or upload <a
href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> for the same
application, you can select expansion files that you've uploaded for a previous APK. <strong>The
expansion file's name does not change</strong>&mdash;it retains the version received by the APK to
which the file was originally associated.</li>
  <li>If you use expansion files in combination with <a
href="{@docRoot}google/play/publishing/multiple-apks.html">multiple APKs</a> in order to
provide different expansion files for different devices, you still must upload separate APKs
for each device in order to provide a unique  <a
href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a>
value and declare different <a href="{@docRoot}google/play/filters.html">filters</a> for
each APK.</li>
  <li>You cannot issue an update to your application by changing the expansion files
alone&mdash;<strong>you must upload a new APK</strong> to update your app. If your changes only
concern the assets in your expansion files, you can update your APK simply by changing the <a
href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a> (and
perhaps also the <a href="{@docRoot}guide/topics/manifest/manifest-element.html#vname">{@code
versionName}</a>).</p></li>
  <li><strong>Do not save other data into your <code>obb/</code>
directory</strong>. If you must unpack some data, save it into the location specified by {@link
android.content.Context#getExternalFilesDir getExternalFilesDir()}.</li>
  <li><strong>Do not delete or rename the {@code .obb} expansion file</strong> (unless you're
performing an update). Doing so will cause Google Play (or your app itself) to repeatedly
download the expansion file.</li>
  <li>When updating an expansion file manually, you must delete the previous expansion file.</li>
</ol>









<h2 id="Downloading">Downloading the Expansion Files</h2>

<p>In most cases, Google Play downloads and saves your expansion files to the device at the same
time it installs or updates the APK. This way, the expansion files are available when your
application launches for the first time. However, in some cases your app must download the
expansion files itself by requesting them from a URL provided to you in a response
from Google Play's <a
href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service.</p>

<p>The basic logic you need to download your expansion files is the following:</p>

<ol>
  <li>When your application starts, look for the expansion files on the <a
href="#StorageLocation">shared storage location</a> (in the
<code>Android/obb/&lt;package-name&gt;/</code> directory).
    <ol type="a">
      <li>If the expansion files are there, you're all set and your application can continue.</li>
      <li>If the expansion files are <em>not</em> there:
        <ol>
          <li>Perform a request using Google Play's <a
href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> to get your
app's expansion file names, sizes, and URLs.</li>
          <li>Use the URLs provided by Google Play to download the expansion files and save
the expansion files. You <strong>must</strong> save the files to the <a
href="#StorageLocation">shared storage location</a>
(<code>Android/obb/&lt;package-name&gt;/</code>) and use the exact file name provided
by Google Play's response.
            <p class="note"><strong>Note:</strong> The URL that Google Play provides for your
expansion files is unique for every download and each one expires shortly after it is given to
your application.</p>
          </li>
        </ol>
      </li>
    </ol>
  </li>
</ol>


<p>If your application is free (not a paid app), then you probably haven't used the <a
href="{@docRoot}google/play/licensing/index.html">Application Licensing</a> service. It's primarily
designed for you to enforce
licensing policies for your application and ensure that the user has the right to
use your app (he or she rightfully paid for it on Google Play). In order to facilitate the
expansion file functionality, the licensing service has been enhanced to provide a response
to your application that includes the URL of your application's expansion files that are hosted
on Google Play. So, even if your application is free for users, you need to include the
License Verification Library (LVL) to use APK expansion files. Of course, if your application
is free, you don't need to enforce license verification&mdash;you simply need the
library to perform the request that returns the URL of your expansion files.</p>

<p class="note"><strong>Note:</strong> Whether your application is free or not, Google Play
returns the expansion file URLs only if the user acquired your application from Google Play.</p>

<p>In addition to the LVL, you need a set of code that downloads the expansion files
over an HTTP connection and saves them to the proper location on the device's shared storage.
As you build this procedure into your application, there are several issues you should take into
consideration:</p>

<ul>
  <li>The device might not have enough space for the expansion files, so you should check
before beginning the download and warn the user if there's not enough space.</li>
  <li>File downloads should occur in a background service in order to avoid blocking the user
interaction and allow the user to leave your app while the download completes.</li>
  <li>A variety of errors might occur during the request and download that you must
gracefully handle.</li>
  <li>Network connectivity can change during the download, so you should handle such changes and
if interrupted, resume the download when possible.</li>
  <li>While the download occurs in the background, you should provide a notification that
indicates the download progress, notifies the user when it's done, and takes the user back to
your application when selected.</li>
</ul>


<p>To simplify this work for you, we've built the <a href="#AboutLibraries">Downloader Library</a>,
which requests the expansion file URLs through the licensing service, downloads the expansion files,
performs all of the tasks listed above, and even allows your activity to pause and resume the
download. By adding the Downloader Library and a few code hooks to your application, almost all the
work to download the expansion files is already coded for you. As such, in order to provide the best
user experience with minimal effort on your behalf, we recommend you use the Downloader Library to
download your expansion files. The information in the following sections explain how to integrate
the library into your application.</p>

<p>If you'd rather develop your own solution to download the expansion files using the Google
Play URLs, you must follow the <a href="{@docRoot}google/play/licensing/index.html">Application
Licensing</a> documentation to perform a license request, then retrieve the expansion file names,
sizes, and URLs from the response extras. You should use the <a href="#ExpansionPolicy">{@code
APKExpansionPolicy}</a> class (included in the License Verification Library) as your licensing
policy, which captures the expansion file names, sizes, and URLs from the licensing service..</p>



<h3 id="AboutLibraries">About the Downloader Library</h3>

<p>To use APK expansion files with your application and provide the best user experience with
minimal effort on your behalf, we recommend you use the Downloader Library that's included in the
Google Play APK Expansion Library package. This library downloads your expansion files in a
background service, shows a user notification with the download status, handles network
connectivity loss, resumes the download when possible, and more.</p>

<p>To implement expansion file downloads using the Downloader Library, all you need to do is:</p>

<ul>
  <li>Extend a special {@link android.app.Service} subclass and {@link
android.content.BroadcastReceiver} subclass that each require just a few
lines of code from you.</li>
  <li>Add some logic to your main activity that checks whether the expansion files have
already been downloaded and, if not, invokes the download process and displays a
progress UI.</li>
  <li>Implement a callback interface with a few methods in your main activity that
receives updates about the download progress.</li>
</ul>

<p>The following sections explain how to set up your app using the Downloader Library.</p>


<h3 id="Preparing">Preparing to use the Downloader Library</h3>

<p>To use the Downloader Library, you need to
download two packages from the SDK Manager and add the appropriate libraries to your
application.</p>

<p>First, open the <a href="{@docRoot}sdk/exploring.html">Android SDK Manager</a>, expand
<em>Extras</em> and download:</p>
<ul>
  <li><em>Google Play Licensing Library package</em></li>
  <li><em>Google Play APK Expansion Library package</em></li>
</ul>

<p>If you're using Eclipse, create a project for each library and add it to your app:</p>
<ol>
  <li>Create a new Library Project for the License Verification Library and Downloader
Library. For each library:
    <ol>
      <li>Begin a new Android project.</li>
      <li>Select <strong>Create project from existing
source</strong> and choose the library from the {@code &lt;sdk&gt;/extras/google/} directory
({@code market_licensing/} for the License Verification Library or {@code
market_apk_expansion/downloader_library/} for the Downloader Library).</li>
      <li>Specify a <em>Project Name</em> such as "Google Play License Library" and "Google Play
Downloader
Library"</li>
      <li>Click <strong>Finish</strong>.</li>
    </ol>
<p class="note"><strong>Note:</strong> The Downloader Library depends on the License
Verification Library. Be sure to add the License
Verification Library to the Downloader Library's project properties (same process as
steps 2 and 3 below).</p>
  </li>
  <li>Right-click the Android project in which you want to use APK expansion files and
select <strong>Properties</strong>.</li>
  <li>In the <em>Library</em> panel, click <strong>Add</strong> to select and add each of the
libraries to your application.</li>
</ol>

<p>Or, from a command line, update your project to include the libraries:</p>
<ol>
  <li>Change directories to the <code>&lt;sdk&gt;/tools/</code> directory.</li>
  <li>Execute <code>android update project</code> with the {@code --library} option to add both the
LVL and the Downloader Library to your project. For example:
<pre class="no-pretty-print">
android update project --path ~/Android/MyApp \
--library ~/android_sdk/extras/google/market_licensing \
--library ~/android_sdk/extras/google/market_apk_expansion/downloader_library
</pre>
  </li>
</ol>

<p>With both the License Verification Library and Downloader Library added to your
application, you'll be able to quickly integrate the ability to download expansion files from
Google Play. The format that you choose for the expansion files and how you read them
from the shared storage is a separate implementation that you should consider based on your
application needs.</p>

<p class="note"><strong>Tip:</strong> The Apk Expansion package includes a sample
application
that shows how to use the Downloader Library in an app. The sample uses a third library
available in the Apk Expansion package called the APK Expansion Zip Library. If
you plan on
using ZIP files for your expansion files, we suggest you also add the APK Expansion Zip Library to
your application. For more information, see the section below
about <a href="#ZipLib">Using the APK Expansion Zip Library</a>.</p>



<h3 id="Permissions">Declaring user permissions</h3>

<p>In order to download the expansion files, the Downloader Library
requires several permissions that you must declare in your application's manifest file. They
are:</p>

<pre>
&lt;manifest ...>
    &lt;!-- Required to access Google Play Licensing -->
    &lt;uses-permission android:name="com.android.vending.CHECK_LICENSE" />

    &lt;!-- Required to download files from Google Play -->
    &lt;uses-permission android:name="android.permission.INTERNET" />

    &lt;!-- Required to keep CPU alive while downloading files (NOT to keep screen awake) -->
    &lt;uses-permission android:name="android.permission.WAKE_LOCK" />

    &lt;!-- Required to poll the state of the network connection and respond to changes -->
    &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    &lt;!-- Required to check whether Wi-Fi is enabled -->
    &lt;uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

    &lt;!-- Required to read and write the expansion files on shared storage -->
    &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
&lt;/manifest>
</pre>

<p class="note"><strong>Note:</strong> By default, the Downloader Library requires API
level 4, but the APK Expansion Zip Library requires API level 5.</p>


<h3 id="DownloaderService">Implementing the downloader service</h3>

<p>In order to perform downloads in the background, the Downloader Library provides its
own {@link android.app.Service} subclass called {@code DownloaderService} that you should extend. In
addition to downloading the expansion files for you, the {@code DownloaderService} also:</p>

<ul>
  <li>Registers a {@link android.content.BroadcastReceiver} that listens for changes to the
device's network connectivity (the {@link android.net.ConnectivityManager#CONNECTIVITY_ACTION}
broadcast) in order to pause the download when necessary (such as due to connectivity loss) and
resume the download when possible (connectivity is acquired).</li>
  <li>Schedules an {@link android.app.AlarmManager#RTC_WAKEUP} alarm to retry the download for
cases in which the service gets killed.</li>
  <li>Builds a custom {@link android.app.Notification} that displays the download progress and
any errors or state changes.</li>
  <li>Allows your application to manually pause and resume the download.</li>
  <li>Verifies that the shared storage is mounted and available, that the files don't already exist,
and that there is enough space, all before downloading the expansion files. Then notifies the user
if any of these are not true.</li>
</ul>

<p>All you need to do is create a class in your application that extends the {@code
DownloaderService} class and override three methods to provide specific application details:</p>

<dl>
  <dt>{@code getPublicKey()}</dt>
    <dd>This must return a string that is the Base64-encoded RSA public key for your publisher
account, available from the profile page on the Developer Console (see <a
href="{@docRoot}google/play/licensing/setting-up.html">Setting Up for Licensing</a>).</dd>
  <dt>{@code getSALT()}</dt>
    <dd>This must return an array of random bytes that the licensing {@code Policy} uses to
create an <a
href="{@docRoot}google/play/licensing/adding-licensing.html#impl-Obfuscator">{@code
Obfuscator}</a>. The salt ensures that your obfuscated {@link android.content.SharedPreferences}
file in which your licensing data is saved will be unique and non-discoverable.</dd>
  <dt>{@code getAlarmReceiverClassName()}</dt>
    <dd>This must return the class name of the {@link android.content.BroadcastReceiver} in
your application that should receive the alarm indicating that the download should be
restarted (which might happen if the downloader service unexpectedly stops).</dd>
</dl>

<p>For example, here's a complete implementation of {@code DownloaderService}:</p>

<pre>
public class SampleDownloaderService extends DownloaderService {
    // You must use the public key belonging to your publisher account
    public static final String BASE64_PUBLIC_KEY = "YourLVLKey";
    // You should also modify this salt
    public static final byte[] SALT = new byte[] { 1, 42, -12, -1, 54, 98,
            -100, -12, 43, 2, -8, -4, 9, 5, -106, -107, -33, 45, -1, 84
    };

    &#64;Override
    public String getPublicKey() {
        return BASE64_PUBLIC_KEY;
    }

    &#64;Override
    public byte[] getSALT() {
        return SALT;
    }

    &#64;Override
    public String getAlarmReceiverClassName() {
        return SampleAlarmReceiver.class.getName();
    }
}
</pre>

<p class="caution"><strong>Notice:</strong> You must update the {@code BASE64_PUBLIC_KEY} value
to be the public key belonging to your publisher account. You can find the key in the Developer
Console under your profile information. This is necessary even when testing
your downloads.</p>

<p>Remember to declare the service in your manifest file:</p>
<pre>
&lt;application ...>
    &lt;service android:name=".SampleDownloaderService" />
    ...
&lt;/application>
</pre>



<h3 id="AlarmReceiver">Implementing the alarm receiver</h3>

<p>In order to monitor the progress of the file downloads and restart the download if necessary, the
{@code DownloaderService} schedules an {@link android.app.AlarmManager#RTC_WAKEUP} alarm that
delivers an {@link android.content.Intent} to a {@link android.content.BroadcastReceiver} in your
application. You must define the {@link android.content.BroadcastReceiver} to call an API
from the Downloader Library that checks the status of the download and restarts
it if necessary.</p>

<p>You simply need to override the {@link android.content.BroadcastReceiver#onReceive
onReceive()} method to call {@code
DownloaderClientMarshaller.startDownloadServiceIfRequired()}.</p>

<p>For example:</p>

<pre>
public class SampleAlarmReceiver extends BroadcastReceiver {
    &#64;Override
    public void onReceive(Context context, Intent intent) {
        try {
            DownloaderClientMarshaller.startDownloadServiceIfRequired(context, intent,
                    SampleDownloaderService.class);
        } catch (NameNotFoundException e) {
            e.printStackTrace();
        }
    }
}
</pre>

<p>Notice that this is the class for which you must return the name
in your service's {@code getAlarmReceiverClassName()} method (see the previous section).</p>

<p>Remember to declare the receiver in your manifest file:</p>
<pre>
&lt;application ...>
    &lt;receiver android:name=".SampleAlarmReceiver" />
    ...
&lt;/application>
</pre>



<h3 id="Download">Starting the download</h3>

<p>The main activity in your application (the one started by your launcher icon) is
responsible for verifying whether the expansion files are already on the device and initiating
the download if they are not.</p>

<p>Starting the download using the Downloader Library requires the following
procedures:</p>

<ol>
  <li>Check whether the files have been downloaded.
    <p>The Downloader Library includes some APIs in the {@code Helper} class to
help with this process:</p>
  <ul>
    <li>{@code getExtendedAPKFileName(Context, c, boolean mainFile, int
versionCode)}</li>
    <li>{@code doesFileExist(Context c, String fileName, long fileSize)}</li>
  </ul>
    <p>For example, the sample app provided in the Apk Expansion package calls the
following method in the activity's {@link android.app.Activity#onCreate onCreate()} method to check
whether the expansion files already exist on the device:</p>
<pre>
boolean expansionFilesDelivered() {
    for (XAPKFile xf : xAPKS) {
        String fileName = Helpers.getExpansionAPKFileName(this, xf.mIsBase, xf.mFileVersion);
        if (!Helpers.doesFileExist(this, fileName, xf.mFileSize, false))
            return false;
    }
    return true;
}
</pre>
    <p>In this case, each {@code XAPKFile} object holds the version number and file size of a known
expansion file and a boolean as to whether it's the main expansion file. (See the sample
application's {@code SampleDownloaderActivity} class for details.)</p>
    <p>If this method returns false, then the application must begin the download.</p>
  </li>
  <li>Start the download by calling the static method {@code
DownloaderClientMarshaller.startDownloadServiceIfRequired(Context c, PendingIntent
notificationClient, Class&lt;?> serviceClass)}.
    <p>The method takes the following parameters:</p>
    <ul>
      <li><code>context</code>: Your application's {@link android.content.Context}.</li>
      <li><code>notificationClient</code>: A {@link android.app.PendingIntent} to start your main
activity. This is used in the {@link android.app.Notification} that the {@code DownloaderService}
creates to show the download progress. When the user selects the notification, the system
invokes the {@link android.app.PendingIntent} you supply here and should open the activity
that shows the download progress (usually the same activity that started the download).</li>
      <li><code>serviceClass</code>: The {@link java.lang.Class} object for your implementation of
{@code DownloaderService}, required to start the service and begin the download if necessary.</li>
    </ul>
    <p>The method returns an integer that indicates
whether or not the download is required. Possible values are:</p>
    <ul>
      <li>{@code NO_DOWNLOAD_REQUIRED}: Returned if the files already
exist or a download is already in progress.</li>
      <li>{@code LVL_CHECK_REQUIRED}: Returned if a license verification is
required in order to acquire the expansion file URLs.</li>
      <li>{@code DOWNLOAD_REQUIRED}: Returned if the expansion file URLs are already known,
but have not been downloaded.</li>
    </ul>
    <p>The behavior for {@code LVL_CHECK_REQUIRED} and {@code DOWNLOAD_REQUIRED} are essentially the
same and you normally don't need to be concerned about them. In your main activity that calls {@code
startDownloadServiceIfRequired()}, you can simply check whether or not the response is {@code
NO_DOWNLOAD_REQUIRED}. If the response is anything <em>other than</em> {@code NO_DOWNLOAD_REQUIRED},
the Downloader Library begins the download and you should update your activity UI to
display the download progress (see the next step). If the response <em>is</em> {@code
NO_DOWNLOAD_REQUIRED}, then the files are available and your application can start.</p>
    <p>For example:</p>
<pre>
&#64;Override
public void onCreate(Bundle savedInstanceState) {
    // Check if expansion files are available before going any further
    if (!expansionFilesDelivered()) {
        // Build an Intent to start this activity from the Notification
        Intent notifierIntent = new Intent(this, MainActivity.getClass());
        notifierIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        ...
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
                notifierIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        // Start the download service (if required)
        int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
                        pendingIntent, SampleDownloaderService.class);
        // If download has started, initialize this activity to show download progress
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // This is where you do set up to display the download progress (next step)
            ...
            return;
        } // If the download wasn't necessary, fall through to start the app
    }
    startApp(); // Expansion files are available, start the app
}
</pre>
  </li>
  <li>When the {@code startDownloadServiceIfRequired()} method returns anything <em>other
than</em> {@code NO_DOWNLOAD_REQUIRED}, create an instance of {@code IStub} by
calling {@code DownloaderClientMarshaller.CreateStub(IDownloaderClient client, Class&lt;?>
downloaderService)}. The {@code IStub} provides a binding between your activity to the downloader
service such that your activity receives callbacks about the download progress.
    <p>In order to instantiate your {@code IStub} by calling {@code CreateStub()}, you must pass it
an implementation of the {@code IDownloaderClient} interface and your {@code DownloaderService}
implementation. The next section about <a href="#Progress">Receiving download progress</a> discusses
the {@code IDownloaderClient} interface, which you should usually implement in your {@link
android.app.Activity} class so you can update the activity UI when the download state changes.</p>
    <p>We recommend that you call {@code
CreateStub()} to instantiate your {@code IStub} during your activity's {@link
android.app.Activity#onCreate onCreate()} method, after {@code startDownloadServiceIfRequired()}
starts the download. </p>
    <p>For example, in the previous code sample for {@link android.app.Activity#onCreate
onCreate()}, you can respond to the {@code startDownloadServiceIfRequired()} result like this:</p>
<pre>
        // Start the download service (if required)
        int startResult = DownloaderClientMarshaller.startDownloadServiceIfRequired(this,
                        pendingIntent, SampleDownloaderService.class);
        // If download has started, initialize activity to show progress
        if (startResult != DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED) {
            // Instantiate a member instance of IStub
            mDownloaderClientStub = DownloaderClientMarshaller.CreateStub(this,
                    SampleDownloaderService.class);
            // Inflate layout that shows download progress
            setContentView(R.layout.downloader_ui);
            return;
        }
</pre>

    <p>After the {@link android.app.Activity#onCreate onCreate()} method returns, your activity
receives a call to {@link android.app.Activity#onResume onResume()}, which is where you should then
call {@code connect()} on the {@code IStub}, passing it your application's {@link
android.content.Context}. Conversely, you should call
{@code disconnect()} in your activity's {@link android.app.Activity#onStop onStop()} callback.</p>
<pre>
&#64;Override
protected void onResume() {
    if (null != mDownloaderClientStub) {
        mDownloaderClientStub.connect(this);
    }
    super.onResume();
}

&#64;Override
protected void onStop() {
    if (null != mDownloaderClientStub) {
        mDownloaderClientStub.disconnect(this);
    }
    super.onStop();
}
</pre>
    <p>Calling {@code connect()} on the {@code IStub} binds your activity to the {@code
DownloaderService} such that your activity receives callbacks regarding changes to the download
state through the {@code IDownloaderClient} interface.</p>
  </li>
</ol>



<h3 id="Progress">Receiving download progress</h3>

<p>To receive updates regarding the download progress and to interact with the {@code
DownloaderService}, you must implement the Downloader Library's {@code IDownloaderClient} interface.
Usually, the activity you use to start the download should implement this interface in order to
display the download progress and send requests to the service.</p>

<p>The required interface methods for {@code IDownloaderClient} are:</p>

<dl>
  <dt>{@code onServiceConnected(Messenger m)}</dt>
    <dd>After you instantiate the {@code IStub} in your activity, you'll receive a call to this
method, which passes a {@link android.os.Messenger} object that's connected with your instance
of {@code DownloaderService}. To send requests to the service, such as to pause and resume
downloads, you must call {@code DownloaderServiceMarshaller.CreateProxy()} to receive the {@code
IDownloaderService} interface connected to the service.
    <p>A recommended implementation looks like this:</p>
<pre>
private IDownloaderService mRemoteService;
...

&#64;Override
public void onServiceConnected(Messenger m) {
    mRemoteService = DownloaderServiceMarshaller.CreateProxy(m);
    mRemoteService.onClientUpdated(mDownloaderClientStub.getMessenger());
}
</pre>
    <p>With the {@code IDownloaderService} object initialized, you can send commands to the
downloader service, such as to pause and resume the download ({@code requestPauseDownload()}
and {@code requestContinueDownload()}).</p>
</dd>
  <dt>{@code onDownloadStateChanged(int newState)}</dt>
    <dd>The download service calls this when a change in download state occurs, such as the
download begins or completes.
      <p>The <code>newState</code> value will be one of several possible values specified in
by one of the {@code IDownloaderClient} class's {@code STATE_*} constants.</p>
      <p>To provide a useful message to your users, you can request a corresponding string
for each state by calling {@code Helpers.getDownloaderStringResourceIDFromState()}. This
returns the resource ID for one of the strings bundled with the Downloader
Library. For example, the string "Download paused because you are roaming" corresponds to {@code
STATE_PAUSED_ROAMING}.</p></dd>
  <dt>{@code onDownloadProgress(DownloadProgressInfo progress)}</dt>
    <dd>The download service calls this to deliver a {@code DownloadProgressInfo} object,
which describes various information about the download progress, including estimated time remaining,
current speed, overall progress, and total so you can update the download progress UI.</dd>
</dl>
<p class="note"><strong>Tip:</strong> For examples of these callbacks that update the download
progress UI, see the {@code SampleDownloaderActivity} in the sample app provided with the
Apk Expansion package.</p>

<p>Some public methods for the {@code IDownloaderService} interface you might find useful are:</p>

<dl>
  <dt>{@code requestPauseDownload()}</dt>
    <dd>Pauses the download.</dd>
  <dt>{@code requestContinueDownload()}</dt>
    <dd>Resumes a paused download.</dd>
  <dt>{@code setDownloadFlags(int flags)}</dt>
    <dd>Sets user preferences for network types on which its OK to download the files. The
current implementation supports one flag, {@code FLAGS_DOWNLOAD_OVER_CELLULAR}, but you can add
others. By default, this flag is <em>not</em> enabled, so the user must be on Wi-Fi to download
expansion files. You might want to provide a user preference to enable downloads over
the cellular network. In which case, you can call:
<pre>
mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);
</pre>
</dd>
</dl>




<h2 id="ExpansionPolicy">Using APKExpansionPolicy</h2>

<p>If you decide to build your own downloader service instead of using the Google Play
<a href="#AboutLibraries">Downloader Library</a>, you should still use the {@code
APKExpansionPolicy} that's provided in the License Verification Library. The {@code
APKExpansionPolicy} class is nearly identical to {@code ServerManagedPolicy} (available in the
Google Play License Verification Library) but includes additional handling for the APK expansion
file response extras.</p>

<p class="note"><strong>Note:</strong> If you <em>do use</em> the <a
href="#AboutLibraries">Downloader Library</a> as discussed in the previous section, the
library performs all interaction with the {@code APKExpansionPolicy} so you don't have to use
this class directly.</p>

<p>The class includes methods to help you get the necessary information about the available
expansion files:</p>

<ul>
  <li>{@code getExpansionURLCount()}</li>
  <li>{@code getExpansionURL(int index)}</li>
  <li>{@code getExpansionFileName(int index)}</li>
  <li>{@code getExpansionFileSize(int index)}</li>
</ul>

<p>For more information about how to use the {@code APKExpansionPolicy} when you're <em>not</em>
using the <a
href="#AboutLibraries">Downloader Library</a>, see the documentation for <a
href="{@docRoot}google/play/licensing/adding-licensing.html">Adding Licensing to Your App</a>,
which explains how to implement a license policy such as this one.</p>







<h2 id="ReadingTheFile">Reading the Expansion File</h2>

<p>Once your APK expansion files are saved on the device, how you read your files
depends on the type of file you've used. As discussed in the <a href="#Overview">overview</a>, your
expansion files can be any kind of file you
want, but are renamed using a particular <a href="#Filename">file name format</a> and are saved to
{@code &lt;shared-storage&gt;/Android/obb/&lt;package-name&gt;/}.</p>

<p>Regardless of how you read your files, you should always first check that the external
storage is available for reading. There's a chance that the user has the storage mounted to a
computer over USB or has actually removed the SD card.</p>

<p class="note"><strong>Note:</strong> When your application starts, you should always check whether
the external storage space is available and readable by calling {@link
android.os.Environment#getExternalStorageState()}. This returns one of several possible strings
that represent the state of the external storage. In order for it to be readable by your
application, the return value must be {@link android.os.Environment#MEDIA_MOUNTED}.</p>


<h3 id="GettingFilenames">Getting the file names</h3>

<p>As described in the <a href="#Overview">overview</a>, your APK expansion files are saved
using a specific file name format:</p>

<pre class="classic no-pretty-print">
[main|patch].&lt;expansion-version&gt;.&lt;package-name&gt;.obb
</pre>

<p>To get the location and names of your expansion files, you should use the
{@link android.os.Environment#getExternalStorageDirectory()} and {@link
android.content.Context#getPackageName()} methods to construct the path to your files.</p>

<p>Here's a method you can use in your application to get an array containing the complete path
to both your expansion files:</p>

<pre>
// The shared path to all app expansion files
private final static String EXP_PATH = "/Android/obb/";

static String[] getAPKExpansionFiles(Context ctx, int mainVersion, int patchVersion) {
    String packageName = ctx.getPackageName();
    Vector&lt;String> ret = new Vector&lt;String>();
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        // Build the full path to the app's expansion files
        File root = Environment.getExternalStorageDirectory();
        File expPath = new File(root.toString() + EXP_PATH + packageName);

        // Check that expansion file path exists
        if (expPath.exists()) {
            if ( mainVersion > 0 ) {
                String strMainPath = expPath + File.separator + "main." +
                        mainVersion + "." + packageName + ".obb";
                File main = new File(strMainPath);
                if ( main.isFile() ) {
                        ret.add(strMainPath);
                }
            }
            if ( patchVersion > 0 ) {
                String strPatchPath = expPath + File.separator + "patch." +
                        mainVersion + "." + packageName + ".obb";
                File main = new File(strPatchPath);
                if ( main.isFile() ) {
                        ret.add(strPatchPath);
                }
            }
        }
    }
    String[] retArray = new String[ret.size()];
    ret.toArray(retArray);
    return retArray;
}
</pre>

<p>You can call this method by passing it your application {@link android.content.Context}
and the desired expansion file's version.</p>

<p>There are many ways you could determine the expansion file version number. One simple way is to
save the version in a {@link android.content.SharedPreferences} file when the download begins, by
querying the expansion file name with the {@code APKExpansionPolicy} class's {@code
getExpansionFileName(int index)} method. You can then get the version code by reading the {@link
android.content.SharedPreferences} file when you want to access the expansion
file.</p>

<p>For more information about reading from the shared storage, see the <a
href="{@docRoot}guide/topics/data/data-storage.html#filesExternal">Data Storage</a>
documentation.</p>



<h3 id="ZipLib">Using the APK Expansion Zip Library</h3>

<div class="sidebox-wrapper">
<div class="sidebox">
  <h3>Reading media files from a ZIP</h3>
  <p>If you're using your expansion files to store media files, a ZIP file still allows you to
use Android media playback calls that provide offset and length controls (such as {@link
android.media.MediaPlayer#setDataSource(FileDescriptor,long,long) MediaPlayer.setDataSource()} and
{@link android.media.SoundPool#load(FileDescriptor,long,long,int) SoundPool.load()}). In order for
this to work, you must not perform additional compression on the media files when creating the ZIP
packages. For example, when using the <code>zip</code> tool, you should use the <code>-n</code>
option to specify the file suffixes that should not be compressed:</p>
<p><code>zip -n .mp4;.ogg main_expansion media_files</code></p>
</div>
</div>

<p>The Google Market Apk Expansion package includes a library called the APK
Expansion Zip Library (located in {@code
&lt;sdk>/extras/google/google_market_apk_expansion/zip_file/}). This is an optional library that
helps you read your expansion
files when they're saved as ZIP files. Using this library allows you to easily read resources from
your ZIP expansion files as a virtual file system.</p>

<p>The APK Expansion Zip Library includes the following classes and APIs:</p>

<dl>
  <dt>{@code APKExpansionSupport}</dt>
    <dd>Provides some methods to access expansion file names and ZIP files:

      <dl style="margin-top:1em">
        <dt>{@code getAPKExpansionFiles()}</dt>
          <dd>The same method shown above that returns the complete file path to both expansion
files.</dd>
        <dt>{@code getAPKExpansionZipFile(Context ctx, int mainVersion, int
patchVersion)}</dt>
          <dd>Returns a {@code ZipResourceFile} representing the sum of both the main file and
patch file. That is, if you specify both the <code>mainVersion</code> and the
<code>patchVersion</code>, this returns a {@code ZipResourceFile} that provides read access to
all the data, with the patch file's data merged on top of the main file.</dd>
      </dl>
    </dd>

  <dt>{@code ZipResourceFile}</dt>
    <dd>Represents a ZIP file on the shared storage and performs all the work to provide a virtual
file system based on your ZIP files. You can get an instance using {@code
APKExpansionSupport.getAPKExpansionZipFile()} or with the {@code ZipResourceFile} by passing it the
path to your expansion file. This class includes a variety of useful methods, but you generally
don't need to access most of them. A couple of important methods are:

      <dl style="margin-top:1em">
        <dt>{@code getInputStream(String assetPath)}</dt>
          <dd>Provides an {@link java.io.InputStream} to read a file within the ZIP file. The
<code>assetPath</code> must be the path to the desired file, relative to
the root of the ZIP file contents.</dd>
        <dt>{@code getAssetFileDescriptor(String assetPath)}</dt>
          <dd>Provides an {@link android.content.res.AssetFileDescriptor} for a file within the
ZIP file. The <code>assetPath</code> must be the path to the desired file, relative to
the root of the ZIP file contents. This is useful for certain Android APIs that require  an {@link
android.content.res.AssetFileDescriptor}, such as some {@link android.media.MediaPlayer} APIs.</dd>
      </dl>
    </dd>

  <dt>{@code APEZProvider}</dt>
    <dd>Most applications don't need to use this class. This class defines a {@link
android.content.ContentProvider} that marshals the data from the ZIP files through a content
provider {@link android.net.Uri} in order to provide file access for certain Android APIs that
expect {@link android.net.Uri} access to media files. For example, this is useful if you want to
play a video with {@link android.widget.VideoView#setVideoURI VideoView.setVideoURI()}.</p></dd>
</dl>

<h4>Reading from a ZIP file</h4>

<p>When using the APK Expansion Zip Library, reading a file from your ZIP usually requires the
following:</p>

<pre>
// Get a ZipResourceFile representing a merger of both the main and patch files
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(appContext,
        mainVersion, patchVersion);

// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
</pre>

<p>The above code provides access to any file that exists in either your main expansion file or
patch expansion file, by reading from a merged map of all the files from both files. All you
need to provide the {@code getAPKExpansionFile()} method is your application {@code
android.content.Context} and the version number for both the main expansion file and patch
expansion file.</p>

<p>If you'd rather read from a specific expansion file, you can use the {@code
ZipResourceFile} constructor with the path to the desired expansion file:</p>

<pre>
// Get a ZipResourceFile representing a specific expansion file
ZipResourceFile expansionFile = new ZipResourceFile(filePathToMyZip);

// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
</pre>

<p>For more information about using this library for your expansion files, look at
the sample application's {@code SampleDownloaderActivity} class, which includes additional code to
verify the downloaded files using CRC. Beware that if you use this sample as the basis for
your own implementation, it requires that you <strong>declare the byte size of your expansion
files</strong> in the {@code xAPKS} array.</p>




<h2 id="Testing">Testing Your Expansion Files</h2>

<p>Before publishing your application, there are two things you should test: Reading the
expansion files and downloading the files.</p>


<h3 id="TestingReading">Testing file reads</h3>

<p>Before you upload your application to Google Play, you
should test your application's ability to read the files from the shared storage. All you need to do
is add the files to the appropriate location on the device shared storage and launch your
application:</p>

<ol>
  <li>On your device, create the appropriate directory on the shared storage where Google
Play will save your files.
  <p>For example, if your package name is {@code com.example.android}, you need to create
the directory {@code Android/obb/com.example.android/} on the shared storage space. (Plug in
your test device to your computer to mount the shared storage and manually create this
directory.)</p>
  </li>
  <li>Manually add the expansion files to that directory. Be sure that you rename your files to
match the <a href="#Filename">file name format</a> that Google Play will use.
  <p>For example, regardless of the file type, the main expansion file for the {@code
com.example.android} application should be {@code main.0300110.com.example.android.obb}.
The version code can be whatever value you want. Just remember:</p>
  <ul>
    <li>The main expansion file always starts with {@code main} and the patch file starts with
{@code patch}.</li>
    <li>The package name always matches that of the APK to which the file is attached on
Google Play.
  </ul>
  </li>
  <li>Now that the expansion file(s) are on the device, you can install and run your application to
test your expansion file(s).</li>
</ol>

<p>Here are some reminders about handling the expansion files:</p>
<ul>
  <li><strong>Do not delete or rename</strong> the {@code .obb} expansion files (even if you unpack
the data to a different location). Doing so will cause Google Play (or your app itself) to
repeatedly download the expansion file.</li>
  <li><strong>Do not save other data into your <code>obb/</code>
directory</strong>. If you must unpack some data, save it into the location specified by {@link
android.content.Context#getExternalFilesDir getExternalFilesDir()}.</li>
</ul>



<h3 id="TestingReading">Testing file downloads</h3>

<p>Because your application must sometimes manually download the expansion files when it first
opens, it's important that you test this process to be sure your application can successfully query
for the URLs, download the files, and save them to the device.</p>

<p>To test your application's implementation of the manual download procedure, you must upload
your application to Google Play as a "draft" to make your expansion files available for
download:</p>

<ol>
  <li>Upload your APK and corresponding expansion files using the Google Play Developer
Console.</li>
  <li>Fill in the necessary application details (title, screenshots, etc.). You can come back and
finalize these details before publishing your application.
  <p>Click the <strong>Save</strong> button. <em>Do not click Publish.</em> This saves
the application as a draft, such that your application is not published for Google Play users,
but the expansion files are available for you to test the download process.</p></li>
  <li>Install the application on your test device using the Eclipse tools or <a
href="{@docRoot}tools/help/adb.html">{@code adb}</a>.</li>
  <li>Launch the app.</li>
</ol>

<p>If everything works as expected, your application should begin downloading the expansion
files as soon as the main activity starts.</p>




<h2 id="Updating">Updating Your Application</h2>

<p>One of the great benefits to using expansion files on Google Play is the ability to
update your application without re-downloading all of the original assets. Because Google Play
allows you to provide two expansion files with each APK, you can use the second file as a "patch"
that provides updates and new assets. Doing so avoids the
need to re-download the main expansion file which could be large and expensive for users.</p>

<p>The patch expansion file is technically the same as the main expansion file and neither
the Android system nor Google Play perform actual patching between your main and patch expansion
files. Your application code must perform any necessary patches itself.</p>

<p>If you use ZIP files as your expansion files, the <a href="#ZipLib">APK Expansion Zip
Library</a> that's included with the Apk Expansion package includes the ability to merge
your
patch file with the main expansion file.</p>

<p class="note"><strong>Note:</strong> Even if you only need to make changes to the patch
expansion file, you must still update the APK in order for Google Play to perform an update.
If you don't require code changes in the application, you should simply update the <a
href="{@docRoot}guide/topics/manifest/manifest-element.html#vcode">{@code versionCode}</a> in the
manifest.</p>

<p>As long as you don't change the main expansion file that's associated with the APK
in the Developer Console, users who previously installed your application will not
download the main expansion file. Existing users receive only the updated APK and the new patch
expansion file (retaining the previous main expansion file).</p>

<p>Here are a few issues to keep in mind regarding updates to expansion files:</p>

<ul>
  <li>There can be only two expansion files for your application at a time. One main expansion
file and one patch expansion file. During an update to a file, Google Play deletes the
previous version (and so must your application when performing manual updates).</li>
  <li>When adding a patch expansion file, the Android system does not actually patch your
application or main expansion file. You must design your application to support the patch data.
However, the Apk Expansion package includes a library for using ZIP files
as expansion files, which merges the data from the patch file into the main expansion file so
you can easily read all the expansion file data.</li>
</ul>



<!-- Tools are not ready.

<h3>Using OBB tool and APIs</h3>

<pre>
$ mkobb.sh -d /data/myfiles -k my_secret_key -o /data/data.obb
$ obbtool a -n com.example.myapp -v 1 -s seed_from_mkobb /data/data.obb
</pre>

<pre>
storage = (StorageManager) getSystemService( STORAGE_SERVICE );
storage.mountObb( obbFilepath, "my_secret_key", myListener );
obbContentPath = storage.getMountedObbPath( obbFilepath );
</pre>
-->