KDownloader

A lightweight Kotlin Multiplatform download manager for Android and iOS. Uses platform-native APIs with no external dependencies.
Features
- Kotlin DSL for easy configuration
- Progress tracking with callbacks
- State management (pending, downloading, paused, completed, failed, cancelled)
- Authentication support (Bearer token, Basic auth)
- Custom headers
- Network type restrictions (WiFi only)
- Platform-native implementations:
Installation
Add the dependency to your KMP module:
kotlin {
sourceSets {
commonMain.dependencies {
implementation("dev.onexeor:kdownloader:1.0.0")
}
}
}
Android Setup
Initialize KDownloader in your Application class:
class MyApp : Application() {
override fun onCreate() {
super.onCreate()
KDownloader.init(this)
}
}
Usage
Basic Download
val downloader = KDownloader()
downloader.download("https://example.com/file.pdf") {
fileName = "document.pdf"
onProgress { progress ->
println("${progress.percentage}%")
}
onComplete { filePath ->
println("Downloaded to: $filePath")
}
onError { error ->
println("Failed: ${error.message}")
}
}
With Authentication
downloader.download("https://api.example.com/private/file.zip") {
fileName = "data.zip"
auth {
bearer("your-access-token")
}
onComplete { println("Done: $it") }
}
With Custom Headers
downloader.download("https://example.com/file.pdf") {
headers {
"X-Custom-Header" to "value"
"Accept" to "application/octet-stream"
}
}
WiFi Only Download
downloader.download("https://example.com/large-file.zip") {
wifiOnly()
onStateChange { state ->
when (state) {
is DownloadState.Paused -> println("Waiting: ${state.reason}")
is DownloadState.Downloading -> println("Progress: ${state.progress.percentage}%")
is DownloadState.Completed -> println("Done!")
else -> {}
}
}
}
Task Control
val task = downloader.download("https://example.com/file.zip")
task.onProgress { println("${it.percentage}%") }
.onComplete { println("Done: $it") }
.onError { println("Error: ${it.message}") }
println("State: ${task.currentState}")
println("Progress: ${task.currentProgress.percentage}%")
task.cancel()
Configuration
val downloader = KDownloader(
KDownloaderConfig(
defaultDirectory = "MyApp/Downloads",
defaultNetworkType = NetworkType.ANY
)
)
API Reference
KDownloader
DownloadState
DownloadError Types
Auth
auth {
bearer("token")
basic("user", "password")
}
Platform Requirements
| Platform | Minimum Version |
|---|
| Android | API 24 (7.0) |
| iOS | 13.0 |
Roadmap
License
Copyright 2024 Viktor Savchik
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance the License.
You may obtain a copy of the License at
http:
Unless applicable law agreed to writing, software
distributed under the License distributed an BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express implied.
See the License the specific language governing permissions
limitations under the License.