gitmyhub

ndk-samples

C++ ★ 11k updated 4mo ago

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.

C++JavaKotlinAndroid NDKCMakeGradlesetup: moderatecomplexity 3/5

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