frostwire-jlibtorrent

repository·master·Indexed 19 days ago

https://github.com/frostwire/frostwire-jlibtorrent

A SWIG-based Java interface for the libtorrent library that enables Java and Android developers to build BitTorrent-based applications. It provides Java class wrappers and JNI binary libraries for Android, Windows, macOS, and Linux architectures.

Tokens
11.3K
Snippets
31
Records
42
Agent score
67%

What's inside frostwire-jlibtorrent

  1. Understand jlibtorrent build outputs

    master

    jlibtorrent is composed of platform-independent Java wrapper classes and platform-specific native JNI libraries (.dll, .so, .dylib).

    Successful builds generate two types of artifacts:

    1. Core wrapper library: jlibtorrent-w.x.y.z.jar (required for all platforms).
    2. Platform-specific binaries: JAR files containing the native libraries for specific architectures (e.g., jlibtorrent-windows-w.x.y.z.jar, jlibtorrent-linux-arm64-w.x.y.z.jar).

    Artifact locations:

    • JAR files: ./build/libs/
    • Raw native libraries: ./swig/bin/release/
  2. Understand jlibtorrent artifact structure

    master

    The jlibtorrent project uses a split artifact structure.

    • Main Artifact (jlibtorrent): Contains the platform-independent Java class files, source code (-sources.jar), and Javadoc (-javadoc.jar). This is the only artifact that includes sources and documentation.
    • Native Artifacts: Platform-specific artifacts (e.g., jlibtorrent-windows, jlibtorrent-linux, jlibtorrent-macosx-arm64) contain only the binary native libraries (.dll, .so, or .dylib). These do not include sources or Javadoc because the Java API documentation is already provided in the main artifact.
  3. Regenerate SWIG Bindings on macOS

    master

    To regenerate the SWIG bindings, you must use a macOS environment with local dependencies (libtorrent, boost, and openssl) checked out at the required versions. Ensure the local libtorrent repository is at the target commit before running the script.

    Prerequisites for SWIG

    • macOS with Xcode / command-line tools
    • SWIG 4.3.1 installed locally
    • JDK 17
    • Local libtorrent checkout at ~/src/libtorrent
    • Local boost at ~/src/boost_1_88_0
    • Local openssl at ~/src/openssl
    # 1. Prepare libtorrent
    cd ~/src/libtorrent
    git fetch origin <commit>
    git checkout RC_2_0
    git reset --hard <commit>
    git submodule update --init
    
    # 2. Run SWIG
    cd swig
    export SRC="${HOME}/src"
    export BOOST_MAJOR="1"
    export BOOST_VERSION="88"
    export BOOST_MINOR="0"
    export LIBTORRENT_ROOT="${SRC}/libtorrent"
    export OPENSSL_VERSION="3.5.2"
    ./run-swig.sh
  4. Build Windows, Linux, and Android binaries using Docker

    master

    To build non-macOS platforms, use a Docker-based approach. This ensures consistency and allows building Windows, Linux, and Android binaries in a single containerized environment.

    Prerequisites

    • Docker installed and running.
    • At least 8 GB of free RAM (16+ GB recommended).
    • 20-30 GB of free disk space.
    • Host Architecture: Must be x86_64 (Linux x86_64 or Windows with WSL2). Note: Android NDK tools are currently x86_64 only.

    Build Process

    Run the following command from the repository root to build all supported Windows, Linux, and Android variants:

    cd swig
    ./docker_build_binaries.sh

    Troubleshooting Docker

    • Unexpected container exit: Run cd swig && ./docker_run_interactive.sh to inspect logs.
    • Out of memory: Limit parallelism by setting export NUM_CPUS=4 before running the build.
    • Stale cache: Force a rebuild by running docker rmi jlibtorrent-android followed by ./docker_build_image.sh.
    cd swig
    ./docker_build_binaries.sh
  5. Manual use of .jar files

    master

    You can manually download and use the .jar files from the GitHub releases.

    Desktop OS Usage

    Every platform requires at least two .jar files:

    1. The Java class wrapper: jlibtorrent-w.x.y.z.jar.
    2. The JNI binary library for your specific OS and architecture (e.g., jlibtorrent-windows-w.x.y.z.jar for Windows x86_64).

    For desktop operating systems, you may need to extract the shared library (.dll, .so, or .dylib) from the secondary JAR and place it in a directory specified by the java.library.path.

    Android Usage

    Place the following 5 JARs in your project's libs folder:

    • jlibtorrent-w.x.y.z.jar
    • jlibtorrent-android-arm-w.x.y.z.jar
    • jlibtorrent-android-arm64-w.x.y.z.jar
    • jlibtorrent-android-x86-w.x.y.z.jar
    • jlibtorrent-android-x86_64-w.x.y.z.jar

    ProGuard Configuration

    If using ProGuard for obfuscation or minification, add this rule to prevent breaking the JNI link: -keep class com.frostwire.jlibtorrent.swig.libtorrent_jni {*;}

  6. Build the project on macOS

    master

    To build the project on macOS, use the provided build-macos.sh script. This script automatically selects the correct architecture build scripts.

    If you are making changes to the SWIG wrapping layer, you can use the --swig-only flag to skip other build steps. If you have already completed SWIG work and are only working on the Java Abstraction Layer, use the --build-only flag to skip the SWIG step and only build binaries and jars.

    # Full build on macOS
    ./build-macos.sh 
    
    # Build only SWIG related steps
    ./build-macos.sh --swig-only
    
    # Skip SWIG and build only binaries and jars
    ./build-macos.sh --build-only
  7. Verify Android 16 KB page size compatibility

    master

    To ensure your build is compatible with Android's 16 KB page size requirement, you can verify the configuration using three methods:

    1. During Build

    Check your build logs for the presence of the linker flag --max-page-size=16384. This flag ensures native shared libraries (.so files) are aligned to 16 KB page boundaries.

    2. On Device Testing

    Connect an Android device and use adb to check the current page size:

    3. APK Analysis

    Use Android Studio to analyze the generated APK for any warnings related to page size compatibility.

    # Check the page size on a device
    adb shell getconf PAGE_SIZE
  8. Optimize jlibtorrent build performance

    master

    To reduce build times and manage system resources during the binary build process, follow these optimizations:

    Reducing Build Time

    • Use fast storage: Use an NVMe or SSD for Docker volumes instead of spinning drives.
    • Preserve cache: Do not delete the build/libs directory between builds to allow reuse of cached artifacts.
    • Limit parallel jobs: If you are running low on memory, reduce the number of CPU cores used by setting the CORES environment variable.

    For full builds, the following specs are recommended:

    • CPU: 8+ cores (16+ cores recommended)
    • RAM: 16 GB minimum (32 GB recommended)
    • Disk: 50 GB SSD minimum (100 GB for comfortable cache)
    • Network: 100 Mbps+
    # Reduce parallel jobs to manage low memory
    export CORES=4
    ./docker_build_binaries.sh
  9. Build frostwire-jlibtorrent from source

    master

    You can build the project for different platforms using the provided scripts in the swig directory.

    Build macOS binaries

    Requirement: A macOS computer with Xcode Command Line Tools and Homebrew (including wget, pcre2, automake, autoconf, and bison).

    cd swig
    ./prepare-macos.sh   # One-time setup
    ./build-macos.sh     # Build for current architecture

    Build Windows, Linux, and Android

    Requirement: Docker installed and running, with 8+ GB RAM (16+ GB recommended) and 20-30 GB free disk space.

    cd swig
    ./docker_build_binaries.sh   # Builds Windows, Linux, Android

    All Android native libraries are built with 16 KB page size compatibility for Google Play requirements.

  10. Install frostwire-jlibtorrent via SBT

    master

    Add the FrostWire Maven repository and the required dependencies to your build.sbt file.

    // Add the custom FrostWire repository
    resolvers += "FrostWire Maven" at "https://dl.frostwire.com/maven"
    
    // Define the library version as a variable
    val jlibtorrentVersion = "2.0.13.6" // change version for latest
    
    // Add all the necessary library dependencies
    libraryDependencies ++= Seq(
      // ALL ARCHITECTURES need the java .class wrappers jlibtorrent.jar
      "com.frostwire" % "jlibtorrent" % jlibtorrentVersion,
    
      // ANDROID (needs all of them for most projects to maximize compatibility)
      "com.frostwire" % "jlibtorrent-android-arm" % jlibtorrentVersion,
      "com.frostwire" % "jlibtorrent-android-arm64" % jlibtorrentVersion,
      "com.frostwire" % "jlibtorrent-android-x86" % jlibtorrentVersion,
      "com.frostwire" % "jlibtorrent-android-x86_64" % jlibtorrentVersion,
    
      // WINDOWS x86_64
      "com.frostwire" % "jlibtorrent-windows" % jlibtorrentVersion,
    
      // MAC OS x86_64 (Intel)
      "com.frostwire" % "jlibtorrent-macosx-x86_64" % jlibtorrentVersion,
      // MAC OS arm64 (Apple Silicon)
      "com.frostwire" % "jlibtorrent-macosx-arm64" % jlibtorrentVersion,
    
      // LINUX x86_64
      "com.frostwire" % "jlibtorrent-linux-x86_64" % jlibtorrentVersion,
      // LINUX arm64
      "com.frostwire" % "jlibtorrent-linux-arm64" % jlibtorrentVersion
    )