What it is
A Java and Kotlin library on top of the C++ libtorrent: it gives an app, including an Android one, a built-in engine for transferring large files over a peer-to-peer network.
Where it fits
Fits apps that distribute large files to many devices: devices share pieces among themselves, and the load on your server drops.
What it is in plain words
libtorrent4j is a library that gives a Java or Kotlin app, including an Android one, a full BitTorrent engine. Inside is libtorrent, a mature C++ library, and libtorrent4j wraps it in an ordinary Java interface. An app can then receive and share large files over a peer-to-peer (P2P) network: devices exchange pieces of a file with each other instead of all fetching it from one server.
Where it helps a business
- An app distributes large files to many devices: content packs, offline maps, training videos, datasets. With P2P most of the traffic goes between the devices, and the load on your server drops.
- Devices in one office or on one local network should share a file among themselves instead of each taking it from the internet.
- A transfer of a large file must survive interruptions and resume from where it stopped, with integrity checked piece by piece.
How it works
- Pieces and hashes. A file is split into pieces, each with a hash. A device receives pieces from different peers in any order and checks every one, so a damaged piece is simply requested again.
- Finding peers. Peers are found through a tracker you run yourself or through the DHT, a distributed network with no central server. For a closed company network your own tracker is enough.
- Priorities. An app can ask for selected files first, or for pieces in order, so that playback of a video can start before the whole file arrives.
- Native code. The core is C++ compiled for each processor architecture (ABI), so the library ships native
.so files inside the APK.
libtorrent4j and alternatives
| libtorrent4j | HTTP from your own server or CDN | A separate client app |
|---|
| Load on your server | low, devices share among themselves | all traffic through the server | low |
| Integration with the app | full, files go straight into the app | full | none, the user transfers files by hand |
| App size | larger by the native libraries | no change | no change |
| Complexity | higher: native code, background work, network | low | low for you, high for the user |
Common mistakes
- Battery and mobile data. P2P keeps the radio busy and uses traffic in both directions. Give the user a "Wi-Fi only" setting and limits on upload speed.
- Background work on Android. The system stops a background process; a long transfer needs a foreground service with a visible notification.
- The APK grows. Native libraries for every ABI add tens of megabytes. Split builds by architecture or use an App Bundle.
- Behind NAT. Without incoming connections, devices find fewer peers and speed drops. This is normal and is accounted for in expectations.
- Rights to the content. The technology transfers any file; what the app shares must be your own content or content with a licence that allows it.
When you do not need it
If files come from a single server of your own and there are not many users, an ordinary HTTP request is simpler and more predictable. A CDN takes the load off the server without native code or P2P.