Switch FileDownloader to OkDownload Seamlessly

This article will show you how to switch FileDownloader to OkDownload seamlessly as much as possible.

Jerome Ran
3 min readNov 8, 2019

FileDownloader is an excellent download engine for Android. It has multiple advantages.

  • Simple to use.
  • Support independent process.
  • Download a single task with multiple blocks.
  • High-concurrency.
  • Support breakpoint.

These advantages make FileDownloader accessible. However, FileDownloader isn’t lightweight enough, and the unit test coverage is deficient, and it’s tough to test.

I have to say that independent process support became a disadvantage because Android 8 has more strict limitations for background service. As a result, FileDownloadService must be perceptible when starting the download in the background. For compatibility with Android 8 and after, I add a new notification configuration since FileDownloader 1.7.6, and you can refer to here to know the detail. But it’s terrible to do system compatible work in a library. We cannot solve some bugs from the library’s point of view; for example, issue 1209.

For further features and enhances, we develop a new library named OkDownload. It extends all advantages of FileDownloader and owns more highlights. We’ve provided a detailed wiki to explain why to choose OkDownload.

OkDownload is lightweight. You can use the core library like this:

// core
com.liulishuo.okdownload:okdownload:1.0.7-SNAPSHOT
// provide sqlite to store breakpoints
com.liulishuo.okdownload:sqlite:1.0.7-SNAPSHOT
// provide okhttp to connect to backend
com.liulishuo.okdownload:okhttp:1.0.7-SNAPSHOT

If you want to use OkDownload, but you’re using FileDownload, you don’t want to rewrite all the code. Just add a new library:

// use OkDownload and keep all your FileDownloader code effective
com.liulishuo.okdownload:filedownloader:1.0.7-SNAPSHOT

I improved this library base on FileDownloader 1.7.7. So it’s better to upgrade your FileDownloader to 1.7.7 and then use this library. And the corresponding OkDownload version is 1.0.7-SNAPSHOT.

This is a specific example:

Our goal is a seamless switch, so there is no spare change ideally. In most cases, it is. However, there always are some differences that cannot be flattened. The first big problem is the database. Not only FileDownloader but also OkDownload uses the database to save the breakpoint info. In OkDownload, the table structure and data structure is very different from FileDownloader. The migration work is backbreaking. In the meantime, OkDownload doesn’t use temp files, so the old file cannot be used in OkDownload despite the database data is migrated. In other words, the information about unfinished tasks save by FileDownloader will be useless after switching to OkDownload. But this isn’t a big problem for you, because OkDownload will re-download these files, and after that, everything will be fine. I provide a method named discardFileDownloadDatabase in the FileDownloader class. You can delete the old database as you wish. By the way, if you customed FileDownloadDatabase, it will not have any effect.

In FileDownloader, you can customize an IdGenerator to determine the task id, and it will not work at OkDownload anymore. Task id isn’t keeping the same. OkDownload may change it in the next start. If you have the logic base on the persisting task id, such as saving task id to the database, you need to change it because the task id is not invariable now. In the FileDownloader demo, we have a TasksManagerDemoActivity class to show how to manages multiple tasks. It did the thing I said before, and I have a pull request to show how to solve it.

OkDownload deprecated and removed many methods; most of them will not have a real influence on you. I sincerely hope you guys can try OkDownload 1.0.7-SNAPSHOT to switch your FileDownloader to OkDownload. And I have shown you a switch example through the pull request. It’s effortless for you guys, and I’m looking forward to your feedback; any question can open an issue on OkDownload.

--

--