Android - How release build slow down downloading?

40 Views Asked by At

A strange issue I am facing since a long while downloading files form server. In debug variant, file is getting download quite fast while in release variant its taking too much time.

Here is the code to download file.

 ServiceAPI.ServiceInterface service = ServiceAPI.getServiceAPIClient(getContext(), accessTokenHash, Utils.getDateTime(d, 3));
        call  = service.getDataSets(imei, Utils.getDateTime(d, 3), strChecksum);
        executor = Executors.newSingleThreadExecutor();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, final Response<ResponseBody> response) {
                if (response.isSuccessful()) {
                    executor.execute(new FileWriter(response));
                } else {
                    resetFlags();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                t.printStackTrace();
                resetFlags();
            }
        });
        
        class FileWriter implements Runnable {
    Response<ResponseBody> response;
    public FileWriter(Response<ResponseBody> response) {
        this.response = response;
    }

    @Override
    public void run() {
        boolean isPassed = writeResponseBodyToDisk(response.body(), Double.valueOf(response.headers().get("file-size")));
        uiHandler.post(new Runnable() {
            @Override
            public void run() {
                if (isPassed) {
                    isDataSetRefreshWorkRunning = false;

                    if (getMvpView() != null) {
                        getMvpView().showDatasetSuccess();
                    }
                    PrefsHelper.setDatasetRequestSent(false);
                    PrefsHelper.setDatasetDataLoaded(true);
                } else {
                    resetFlags();
                }

            }
        });
    }
}

private boolean writeResponseBodyToDisk(ResponseBody body, double fileSize) {
    boolean isFileWrite = false;
    try {
        InputStream is = body.byteStream();

        File folder = new File(context.getFilesDir().getCanonicalPath() + "/" + AppConfig.DATA_SETS);
        if (!folder.exists()) {
            folder.mkdir();
        }
        File file = new File(folder, "dataset.zip");
        FileOutputStream fos = new FileOutputStream(file);
        // The buffer
        final byte[] buffer = new byte[8192];
        int len;
        int totalReadBytes = 0;
        int currentProgress, lastProgress = 0;

        while (((len = is.read(buffer)) != -1)) {
            fos.write(buffer, 0, len);
            totalReadBytes +=len;
            currentProgress = (totalReadBytes) / (int) (fileSize / 100);

            // A reason to check progress is to avoid unwanted calls to update UI
            if (!call.isCanceled()) {
                if (currentProgress > lastProgress && currentProgress <=100) {
                    lastProgress = currentProgress;
                    if (getMvpView() != null) {
                        getMvpView().showProgressUpdate(currentProgress);
                    }
                }
            } else {
                return false;
            }
        }
        isFileWrite = true;

        fos.close();
        is.close();

        String zipFile = context.getFilesDir().getCanonicalPath()+ "/" + AppConfig.DATA_SETS + "/dataset.zip";

        String unzipLocation = context.getFilesDir().getCanonicalPath() + AppConfig.DATA_SETS;
        new Decompress().unpackZip(zipFile, unzipLocation);
        // delete the zip file

        new File(zipFile).delete();
        parseDatasetConfig();

    } catch (final IOException e) {
        e.printStackTrace();
    }

    return isFileWrite;
}

Here is gradle:

buildTypes {
        debug {
            buildConfigField "java.util.Date", "BUILDTIME", "new java.util.Date(" + getDateAsMillis() + "L)"
        }
        release {
            buildConfigField "java.util.Date", "BUILDTIME", "new java.util.Date(" + getDateAsMillis() + "L)"
        }
    }
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.docukeystore
    }
    debug {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.txt'
        signingConfig signingConfigs.docukeystore
    }
}

How build variant play role to define download speed?

0

There are 0 best solutions below