Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to implement Expansion Library in Android

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 580
    Comment on it

    Android has some of its policies due to which it don't allow you to upload apk more than the size of 50 MB. Many Apps contains too many media files that may increase the size leading to the exceeding the limit of 50 MB.

    So if your App's apk is exceeding the 50MB barrier, no need to worry. Android provides Expansion library that lets you upload the apk if the size is more than 50MB. There are two files through which you can upload the App's files, one is main file and another is patch file.

    Main file is the primary expansion file that is required for the additional resources your app has. Patch file is the optional expansion file for later updates to the main file.

    Each of these files can be up to 2GB. These files can be in any format but its recommended in compressed format. The best one is the zip compressed format. File name format:

    [main|patch].<expansion-version>.<package-name>.obb

    For example: If your package name is com.example.android and android version is 1 (mentioned in AndroidManifest.xml file). For main expansion file, the file name will be:

    main.1.com.example.android.obb

    How to put files in expansion file.

    Suppose if you have files as pictures, create a folder name it as given above in file format (For example: main.1.com.example.android.obb). After naming the folder, copy all your pictures in this folder.

    After copying all the files, compress the folder in zip format. And the name of the folder should be like as given below.

    Example: main.1.com.example.android.obb.zip

    Now your main expansion file is ready to upload and its time to implement that in code.

    Storage location of your expansion file.

    When the user tries to download the app from play store then the expansion file will get downloaded automatically.The storage location of your expansion file will be the shared storage location.

    /Android/obb//

    Working with Expansion File in Android

    First of all you need to download two library from Android SDK Manager.

    1. Google Play Licensing Library Package
    2. Google Play APK Expansion Library Package

    After downloading these two libraries, include them in your project.

    Code to get the absolute path of the location containing files is below:

    static String getAPKExpansionFiles(Context ctx, int mainVersion) {
            String packageName = ctx.getPackageName();
            String path=null;
            File main=null;
            String strMainPath=null;
            if (Environment.getExternalStorageState()
                    .equals(Environment.MEDIA_MOUNTED)) {
                // Build the full path to the app's expansion files
                File root = Environment.getExternalStorageDirectory();
                System.out.print("Main packageName:::::" + root);
                File expPath = new File(root.toString() + EXP_PATH  );
       // Check that expansion file path exists
        if (expPath.exists()) {
            if ( mainVersion > 0 ) {
                strMainPath = expPath + File.separator + "main." +
                        mainVersion + "." + packageName + ".obb";
    
                main = new File(strMainPath);
                main.getParentFile().mkdirs();
                System.out.print("Main path::::"+ main.getAbsolutePath());
                if ( main.isFile() ) {
                    path=strMainPath;
                }
            }
        }
    } 
    return main.getAbsolutePath(); }
    

    Add the below permissions in your AndroidManifest.xml file.

    <uses-permission android:name="com.android.vending.CHECK_LICENSE">
    <uses-permission android:name="android.permission.INTERNET">
    <uses-permission android:name="android.permission.WAKE_LOCK">
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission></uses-permission>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: