Skip to content

Advanced topics

Alex Gotev edited this page Jul 29, 2017 · 33 revisions

ProGuard

Add this to your ProGuard configuration:

-keep class net.gotev.uploadservice.** { *; }

This keeps all the classes of the library. If you want to fine tune it, you have to discover which parts of the library are you using. Open a terminal, navigate to your src folder and execute:

grep -roh . -e 'net.gotev.uploadservice.*' | sort | uniq

This will print out something like this:

net.gotev.uploadservice.BinaryUploadRequest;
net.gotev.uploadservice.Logger;
net.gotev.uploadservice.MultipartUploadRequest;
net.gotev.uploadservice.ServerResponse;
net.gotev.uploadservice.UploadInfo;
net.gotev.uploadservice.UploadNotificationAction;
net.gotev.uploadservice.UploadNotificationConfig;
net.gotev.uploadservice.UploadService;
net.gotev.uploadservice.UploadServiceBroadcastReceiver;
net.gotev.uploadservice.UploadStatusDelegate;
net.gotev.uploadservice.ftp.FTPUploadRequest;
net.gotev.uploadservice.ftp.UnixPermissions;
net.gotev.uploadservice.okhttp.OkHttpStack;

And those are the classes you are importing. Refer to this excellent article by Jeroen Mols to discover how to improve your ProGuard configuration file based on the output you got.

Tune exponential backoff algorithm

When an upload fails, Android Upload Service will automatically retry it if you have invoked setMaxRetries on the upload request object. To optimize network usage, every retry will be made some time after each failure. If a subsequent failure happens, the next attempt will be furtherly delayed, and so on. In this way if your network drops at some time, you will not experience a burst in subsequent upload attempts, which drains your battery.

Backoff algorithm is ruled by the following constants, which you can tune as you wish starting from release 3.0:

  • UploadService.INITIAL_RETRY_WAIT_TIME: sets the time to wait in milliseconds before the next attempt when an upload fails for the first time. From the second time onwards, this value will be multiplied by UploadService.BACKOFF_MULTIPLIER to get the time to wait before the next attempt.
  • UploadService.BACKOFF_MULTIPLIER: sets the backoff timer multiplier. By default is set to 10, so every time that an upload fails, the time to wait between retries will be multiplied by 10. E.g. if the first time the wait time is 1s, the second time it will be 10s and the third time it will be 100s.
  • UploadService.MAX_RETRY_WAIT_TIME: Sets the maximum time to wait in milliseconds between two upload attempts. This is useful because every time an upload fails, the wait time gets multiplied by UploadService.BACKOFF_MULTIPLIER and it's not convenient that the value grows indefinitely.

Refer to the code for further reference.

Tune the number of parallel uploads

By default, UploadService gets initialized with a pool of threads equal to the number of processors on your device, as you can see here. So, for example, if you have a quad-core device, the number of maximum parallel uploads will be 4. You can tune that by setting:

UploadService.UPLOAD_POOL_SIZE

in your Application initializer. If you set this value to 1, you will have only one upload task at a time.

How to use self-signed certificates

For security reasons, the library doesn't accept self-signed certificates by default when using HTTPS connections, but you can enable them if you need it. Read this.

Retrying failed uploads

Android Upload Service tries its best to upload files. You can in fact set the maximum number of automatic upload retries before notifying an error, by calling the setMaxRetries on the upload request. Since network on mobile devices is unreliable and it may also happen that your server is not reachable when you try to upload something, it may happen that your files are not uploaded even after 100 automatic retries (which is a bad practice btw 😄). So, what I suggest you to do is to keep a reference to the uploadID and the upload request object somewhere in your code, so you can trigger upload again by calling the startUpload method on the request object when certain conditions (e.g. server is reachable again) are met. How to properly implement that behaviour for your specific scenario is up to you. If you can't figure it out how to do it and you need some further help, you're welcome, but that's called consulting 😃 .

Uploading only when a connection is available

If you want to start uploads or retry them based on the remote server's reachability status, Android Host Monitor may be useful to you in combination with this library.

Custom HTTP stack implementation

By default, the library uses the HttpURLConnection stack to perform uploads. However, starting from release 2.1+, you can use also OkHttp (check Setup wiki page) or provide your own implementation! To provide a new stack implementation, you should implement the following interfaces in three classes:

  • HttpStack
  • HttpStackConnection
  • BodyWriter

Check interfaces source codes and default implementation. You can also see how to create a completely new project which provides a custom HTTP stack by looking at how the uploadservice-okhttp module is made.