Compiling 7-Zip SDK for iOS and Android
The 7-Zip SDK (primarily distributed as the LZMA SDK) can be fully compiled and integrated into both iOS and Android native mobile applications. Because the core compression and extraction algorithms are written in standard ANSI C and C++, the source code is platform-agnostic and does not rely on Windows-specific APIs. This article details how developers can compile the SDK across both mobile platforms, configure the necessary native build tools, and interface the native code with mobile app runtimes.
Why the 7-Zip SDK is Portable
The core logic of the 7-Zip SDK—commonly used for handling
.7z archives and LZMA/LZMA2 compression—is self-contained.
The C and C++ source code relies only on basic standard library
functions for memory management and I/O operations. Because standard
mobile compilers support ANSI C and modern C++, the code compiles
cleanly for target architectures including ARM64 (arm64-v8a for Android,
arm64 for iOS) and x86_64 for mobile emulators and simulators.
Compiling for Android via the NDK
To compile the 7-Zip SDK for Android native applications, you must use the Android Native Development Kit (NDK).
- Include the Source Files: Copy the required C or
C++ source files (such as
LzmaDec.c,LzmaEnc.c,7zDec.c, and archive handling files) into your project'scpporjnidirectory. - Build Configuration: Use CMake or
ndk-buildto define the native compilation targets. With CMake, add the source files to anadd_librarytarget configured to build a shared library (.so). - Bridge via JNI: Create Java Native Interface (JNI) wrapper functions to bridge Java/Kotlin calls with the SDK's native functions. This allows your Android runtime to pass file paths, buffers, or streams to the 7-Zip compression routines.
- Target Architectures: Configure your
build.gradlefile to compile native binaries forarm64-v8a,armeabi-v7a, andx86_64.
Compiling for iOS using Xcode
Compiling the SDK for iOS natively is straightforward due to Xcode's native support for C and C++.
- Add Files to Xcode: Import the native
.cand.hfiles directly into your Xcode workspace and assign them to your application target. - Configure Build Settings: Ensure your project
builds for standard iOS architectures (
arm64for physical devices andarm64/x86_64for simulators). - Language Interoperability:
- Objective-C: C functions can be called directly from Objective-C or Objective-C++ files.
- Swift: To use the SDK in Swift, expose the
necessary header files (like
7zTypes.handLzmaDec.h) inside the project’s Objective-C Bridging Header, or package the source files into a C-based Swift Package (SPM) or Framework.
Implementation Considerations
- Memory Usage: Compressing files via the 7-Zip SDK with high dictionary sizes requires significant RAM. On mobile devices, keep dictionary sizes modest (such as 16MB or 32MB) to prevent operating system terminations caused by out-of-memory errors.
- Full Archive Support vs. Stream Compression: If
your application only requires decompression of LZMA streams,
integrating minimal C files (like
LzmaDec.c) is recommended to keep the binary size low. If multi-file.7zcontainer support, encryption, or multi-threading is needed, include the full suite of container decoders (7zDec.c,7zIn.c, etc.). - Licensing: Most of the LZMA SDK is in the public domain, though certain archive-handling modules fall under the GNU LGPL. Verify the specific licensing of the source files you include to ensure compliance with mobile app store distribution rules.