First project for Android
This page builds LZ4 for two Android ABIs and links it into an app’s native library. You need otok
(installed) and the Android NDK; otok finds the newest NDK of the Android SDK that Android Studio
installs, or the one ANDROID_NDK_HOME names. It works the same on macOS, Windows and Linux.
Describe the project
Section titled “Describe the project”Put otok.toml in your app module’s directory (app/):
[otok]format = 1
[android]architectures = { android = ["arm64-v8a", "x86_64"] }deployment = { android = "24" }
[deps.lz4]recipe = "apotocki/lz4"version = "~> 1.10"[android]is the target. Its architectures are the ABIs you ship, as Gradle’sabiFiltersnames them;arm64-v8aalone when you omit them.deploymentis the API level, your app’sminSdkor lower.
Install
Section titled “Install”otok installotok.lock created: added lz4 1.11.0 (r1) ...toolchain of 'android': Android NDK 30.0.16248370 at '…/ndk/30.0.16248370'...packaging lz4 1.11.0 for androidoutput 'Otok': 1 copied, 0 unchanged, 0 removedapp/Otok/ otok-config.cmake for CMake: Gradle's externalNativeBuild Acknowledgements.md the licenses of everything built lz4/ include/ lz4.h, lz4frame.h, ... lib/arm64-v8a/liblz4.a lib/x86_64/liblz4.aCommit otok.toml and otok.lock; add Otok/ to .gitignore.
Run otok install before Gradle builds the app, and again after changing otok.toml.
Link it into your native library
Section titled “Link it into your native library”app/src/main/cpp/CMakeLists.txt:
cmake_minimum_required(VERSION 3.28)project(hello C)
find_package(Otok CONFIG REQUIRED PATHS "${CMAKE_CURRENT_SOURCE_DIR}/../../../Otok" NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH)
add_library(hello SHARED hello.c)target_link_libraries(hello PRIVATE otok::LZ4)app/build.gradle.kts:
android { defaultConfig { minSdk = 24 ndk { abiFilters += listOf("arm64-v8a", "x86_64") } } externalNativeBuild { cmake { path = file("src/main/cpp/CMakeLists.txt") } }}otok::LZ4 brings the headers and the library of the ABI Gradle builds. An ABI the output was not built for is an
error when CMake configures, with the cure:
otok: …/Otok has no libraries for the architecture 'x86': add it to 'architectures' of [android] in otok.toml andrun otok installDynamic libraries and the C++ runtime
Section titled “Dynamic libraries and the C++ runtime”With linkage = "dynamic" the libraries are shared objects; Gradle packs those your native library links into the
APK by itself. Dynamic libraries in C++ use the shared C++ runtime by default (stl = "shared" in [android]), and
so must your app: pass -DANDROID_STL=c++_shared:
android { defaultConfig { externalNativeBuild { cmake { arguments += "-DANDROID_STL=c++_shared" } } }}otok-config.cmake refuses an app whose runtime does not match its libraries’. Static libraries link into your
native library and need neither.
The NDK of your app
Section titled “The NDK of your app”The libraries are compiled against the C++ headers of the NDK otok built them with, and link with that NDK or a newer
one. otok-config.cmake refuses an app whose NDK is older, naming it:
otok: the libraries of …/Otok were built with NDK 30.0.16248370 and link with it or a newer one, not with NDK29.0.14206865 at '…/ndk/29.0.14206865': build with that NDK or a newer one, or install the libraries with this one('android-ndk' in otok's config.toml or ANDROID_NDK_HOME) and run otok installGradle takes the NDK of ndkVersion in build.gradle.kts. Scripts can ask otok which NDK it uses: otok metadata
gives its path and version in toolchain of the android target.
- Several targets in one project: the same
otok.tomlfor your iOS app. - Options, profiles and configurations.
- The
otok.tomlreference.
© 2026 Alexander A Pototskiy and contributors. Text CC BY 4.0; examplesCC0.