ndk-samples
Android NDK samples with Android Studio
Official Google sample apps showing how to use the Android NDK to write C or C++ code inside Android apps, useful for games, high-performance computing, and reusing native libraries.
This repository contains sample Android apps that demonstrate how to use the Android NDK, which is a set of tools that lets you write parts of an Android app in C or C++ rather than Java or Kotlin. Native code is useful in situations where you need high performance, want to reuse existing C or C++ libraries, or are building a game that renders its own graphics directly without using the standard Android interface components.
Each sample is a small focused app that shows one specific NDK feature or API in isolation. The samples are intentionally simplified to keep the demonstrations clear, which also means they cut some corners that a real production app would not. The README is direct about this: these samples are reference material and not a template to copy as a starting point for a new project. For that, the Android Studio new project wizard has templates designed specifically for native apps.
To build and run the samples you open the whole project in Android Studio and select whichever sample you want from the toolbar. You can also build from the command line using the gradle build tool. The only manual step is installing a specific version of CMake, the build system used to compile the C++ code, through the Android Studio SDK Manager.
The repository also documents a few practices used consistently across the samples that are worth understanding. One is a preferred way of registering C functions so that Android can call them from Java code. Another is the use of version scripts, which are configuration files that control which functions in a compiled C++ library are visible from outside it. Using them produces smaller and faster libraries.
The project is maintained by Google's Android team and is open source under the Apache 2.0 license.
Where it fits
- Open a specific sample in Android Studio to see a minimal working example of a single NDK feature like audio or graphics before adding it to your own app.
- Build the samples from the command line with Gradle to understand how CMake integrates with the Android build system.
- Study the recommended C function registration pattern the samples use consistently to learn the correct way to call C code from Java.
- Use the version script examples to learn how to control which functions in your C++ library are exposed, reducing library size and improving load speed.