Android .obb detailed explanation

1 Introduction

Before the Android APP Bundle (aab), projects listed on Google Play were packaged into APKs and uploaded. However, Google has restrictions on the APK size. It was 50M at the beginning and later expanded to 100M. However, some APKs have more resources and may be larger than 100M. Based on this situation, Google allows uploading an additional extension file. This file is the .obb file we will explain in detail today. Whenever you upload an APK using the Google Play Console, you have the option to add one or two expansion files to the APK. Each file is limited to 2 GB in size and can be in any format of your choice. However, in order to save bandwidth when downloading, we recommend that you use compressed files. Conceptually, each expansion file plays a different role:

  • The main expansion file is the main expansion file where the additional resources necessary for the application are located.
  • Patch expansion files are optional and are used to provide small updates to the main expansion file.

Although you can use these two expansion files in the way you need, we recommend that you use them as follows: the main expansion file provides the main resources and should be updated as little as possible; the patch expansion file should be smaller and used as a "patch carrier". Update with every major release or as needed.

However, even if an app update only requires a new patch expansion file, you still must upload a new APK and update the versionCode in the manifest  . (The Play Console does not allow you to upload expansion files into an existing APK.)

2.Generate .obb

Generally, we will package some large resources in the APP or the APK for upgrade into a zip, for example, the name is: MYOBB.zip, and then rename the packaged ZIP to: main.versionCode.Package name.obb, for example : main.22070479.cool.obb.android.obb

3. Upload the obb file to Google Play Console along with the APK:

Open the Google Console backend and find the corresponding project:   https://play.google.com/console/u/0/developers

Upload APK  

 

 After uploading the APK, click More and choose to upload the expansion file (.obb)

 

 

 

 

 In this way, the APK and obb files are uploaded to Google Play together. Internal test users can download and install them. After the test is OK, they can be released to the official online environment. When users download the APK, the obb files we uploaded will also will be downloaded to your phone simultaneously

4. Download the corresponding obb file when downloading the APK.

After downloading the APK, the previously uploaded obb file will also be downloaded simultaneously. When Google Play downloads the expansion file to the device, it will be saved to the system's shared storage location. To ensure proper operation of the application, do not delete, move, or rename expansion files. If your app must download itself from Google Play, you must save the file to the same location.

The location where the download is stored:

Root directory: getExternalStorageDirectory()  + /Android/obb/<package-name>/XXX.obb

For example:

        String obb_filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + getPackageName() + "/main.22070479.cool.obb.android.obb";

 At this time, after opening the APP, you will start to decompress and use the obb file.

5. Unzip and use the files in OBB:

First, the APP must have read and write permissions to operate on the obb file:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

After getting read and write permissions, start decompressing obb:

   public void unZipObb(String srcFilePath, String dstFolderPath) {
        Log.d("MainActivity", "unZipObb : srcFilePath=" + srcFilePath + ", dstFolderPath=" + dstFolderPath);
        try {
            String obbFilePath = srcFilePath;
            if (obbFilePath == null) {
                Log.d("MainActivity", "unZipObb error : obbFilePath == null");
                return;
            } else {
                File obbFile = new File(obbFilePath);
                if (!obbFile.exists()) {
                    //下载obb文件
                    Log.d("MainActivity", "unZipObb error : !obbFile.exists()");
                } else {
                    File outputFolder = new File(dstFolderPath);
                    if (!outputFolder.exists()) {
                        //目录未创建 没有解压过
                        outputFolder.mkdirs();
                        unZip(obbFile, outputFolder.getAbsolutePath());
                    } else {
                        //目录已创建 判断是否解压过
                        if (outputFolder.listFiles() == null) {
                            //解压过的文件被删除
                            unZip(obbFile, outputFolder.getAbsolutePath());
                        } else {
                            //此处可添加文件对比逻辑
                            Log.d("MainActivity", "unZipObb error : outputFolder.listFiles() != null");
                        }
                    }
                }
            }
        } catch (Exception e) {
            Log.d("MainActivity", "unZipObb error : Exception");
            e.printStackTrace();
        }
    }

    public void createDirectoryIfNeeded(String folderPath) {
        File folder = new File(folderPath);
        if (!folder.exists() || !folder.isDirectory()) {
            folder.mkdirs();
        }
    }

    public void unZip(File zipFile, String outPathString) {
        Log.d("MainActivity", "unZip " + zipFile.getName() + " to " + outPathString);
        try {
            createDirectoryIfNeeded(outPathString);
            ZipInputStream inZip = new ZipInputStream(new FileInputStream(zipFile));
            ZipEntry zipEntry;
            String szName;
            while ((zipEntry = inZip.getNextEntry()) != null) {
                szName = zipEntry.getName();
                if (zipEntry.isDirectory()) {
                    szName = szName.substring(0, szName.length() - 1);
                    File folder = new File(outPathString + File.separator + szName);
                    folder.mkdirs();
                } else {
                    File file = new File(outPathString + File.separator + szName);
                    createDirectoryIfNeeded(file.getParent());
                    file.createNewFile();
                    FileOutputStream out = new FileOutputStream(file);
                    int len;
                    byte[] buffer = new byte[1024];
                    while ((len = inZip.read(buffer)) != -1) {
                        out.write(buffer, 0, len);
                        out.flush();
                    }
                    out.close();
                }
            }
            inZip.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

6. After getting the files in OBB, you can use the decompressed files:

    String srcPNG1 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhOOB/MYOBB/11.png";
        String srcPNG2 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhOOB/MYOBB/12.png";
        String srcPNG3 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhOOB/MYOBB/13.png";
        String srcPNG4 = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhOOB/MYOBB/14.png";

        mChangeBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                int position = mCount % 4;
                switch (position) {
                    case 0:
                        Glide.with(MainActivity.this).load(srcPNG1).into(mShowImageView);
                        break;
                    case 1:
                        Glide.with(MainActivity.this).load(srcPNG2).into(mShowImageView);
                        break;
                    case 2:
                        Glide.with(MainActivity.this).load(srcPNG3).into(mShowImageView);
                        break;
                    case 3:
                        Glide.with(MainActivity.this).load(srcPNG4).into(mShowImageView);
                        break;
                }
                mCount++;

            }
        });

Summary: The entire life cycle of OBB is introduced in detail, from generating OBB -----> uploading to Google Play ----> user downloading and synchronizing OBB -----> decompressing OBB -----> using OBB document

Reference documentation: https://developer.android.com/google/play/expansion-files#Overview

 OBB Demo GitHub :  https://github.com/JasonZhangHG/OBBDemo.git

Guess you like

Origin blog.csdn.net/Jason_HD/article/details/128422650
Recommended