Introduction
The Android Native Development Kit (NDK) allows developers to use C and C++ code with Android. It provides platform libraries for managing native activities and accessing physical device components like sensors and touch input. The NDK is particularly useful for squeezing extra performance out of a device, achieving low latency, or running computationally intensive applications like games or physics simulations. This guide walks you through installing and configuring the Android NDK on Windows, macOS, and Linux.
Prerequisites
Before starting, ensure you have the following:
- Android Studio: The recommended Integrated Development Environment (IDE) for Android development. Download the latest version from the official Android website.
- Java Development Kit (JDK): Required for the NDK to function. Use either the Oracle JDK or an open-source alternative like AdoptOpenJDK.
- Command Line Tools: Familiarity with command line tools is essential for managing the NDK and SDK.
Installing Android Studio
Download Android Studio
- Open your web browser and navigate to the official Android Studio download page.
- Click the "Download" button to get the Android Studio bundle.
Run the Installer
- Once the download completes, run the downloaded program.
- Follow the installation wizard to install all Android components.
Choose Installation Directories
- When prompted, choose installation directories for Android Studio and the Android SDK (e.g.,
C:\Android\android-studio
andC:\Android\sdk
).
Launch Android Studio
- After installation, launch Android Studio to ensure proper functionality.
- If prompted to import settings from a previous installation, select your preferred option and click "OK."
Close the Welcome Screen
- Close the welcome screen as it will not be needed for now.
Installing the Android NDK
Download the NDK
- Navigate to the Android NDK download page.
- Select the NDK package for your development platform. The latest Long Term Support (LTS) version is recommended.
Extract the NDK Archive
- Once downloaded, extract the archive to a directory of your choice (e.g.,
C:\Android\ndk
).
Set Environment Variables
- Declare the Android SDK and NDK as environment variables to easily access Android utilities from the command line.
- Open the Environment Variables system window.
- Inside the System variables list, add:
ANDROID_SDK
variable with the SDK installation directory (e.g.,C:\Android\sdk
)ANDROID_NDK
variable with the NDK installation directory (e.g.,C:\Android\ndk
)
- Prepend
%ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%ANDROID_NDK%;
at the beginning of yourPATH
environment variable.
Verify NDK Installation
- Open a command prompt or terminal.
- List the Android devices connected to your computer using
adb devices
. - Check if the NDK is working by running
ndk-build -version
.
Configuring Android Studio for NDK
Open SDK Manager
- Open the Android SDK Manager, located in the ADB bundle directory's root.
- Click "New" to select all the packages, then click "Install packages…"
- Accept the licenses in the popup and start the installation of Android development packages by clicking "Install."
Install NDK and CMake
- With a project open, click Tools > SDK Manager.
- Click the SDK Tools tab.
- Select the NDK (Side by side) and CMake checkboxes.
- Click OK.
- A dialog box will show the space the NDK package consumes on disk. Click OK.
- When the installation completes, click Finish.
- Your project will automatically sync the build file and perform a build. Resolve any errors that occur.
Configuring Specific Versions of NDK
Install Specific Versions of NDK
- With a project open, click Tools > SDK Manager.
- Click the SDK Tools tab.
- Select the Show Package Details checkbox.
- Select the NDK (Side by side) checkbox and the checkboxes below it corresponding to the NDK versions you want to install.
- Click OK.
- A dialog box will show the space the NDK package(s) consume. Click OK.
- When the installation completes, click Finish.
Configure Each Module with Specific Versions
- When using Android Studio 3.6 or higher, if you do not specify the version, the Android Gradle plugin chooses a compatible version.
- To configure each module with a specific version of the NDK, add the following to your module's
build.gradle
file:
groovy
android {
…
externalNativeBuild {
cmake {
…
version "cmake-version"
}
}
}
Using CMake with NDK
Create or Import a Native Project
- Open Android Studio.
- Click on File > New > New Project.
- Select "Empty Activity" and click "Next."
- Choose "C/C++" as the project type and click "Next."
- Choose the NDK version you want to use and click "Finish."
Add Native Source Files
- Open your project in Android Studio.
- Go to File > New > C/C++ Source File.
- Name your file (e.g.,
native-lib.cpp
) and click "OK." - Add your native code in this file.
Configure CMake
-
Open your
build.gradle
file. -
Add the following configuration:
groovy
android {
…
externalNativeBuild {
cmake {
…
version "cmake-version"
}
}
} -
Ensure your
CMakeLists.txt
file is correctly configured to build your native library.
Debugging Native Code
Set Up LLDB
- The Native Development Kit (NDK) uses LLDB as its debugger.
- Ensure LLDB is installed and configured correctly.
Debugging Steps
- Open your project in Android Studio.
- Go to Run > Edit Configurations.
- Click the "+" button to create a new configuration.
- Choose "LLDB" as the debugger type and select your project.
- Set breakpoints in your native code and run the debugger.
Troubleshooting Common Issues
Environment Variables Not Set Correctly
- Ensure your environment variables are set correctly. If not, set them manually or use a script to automate the process.
NDK Not Recognized by Android Studio
- If the NDK is not recognized by Android Studio, try reinstalling it or checking if it is correctly installed in the specified directory.
CMake Configuration Issues
- If there are issues with CMake configuration, check if your
CMakeLists.txt
file is correctly configured and if all dependencies are included.
Gradle Build Errors
- If there are errors during the Gradle build process, check if all dependencies are correctly included in your
build.gradle
file and if there are any syntax errors.
Final Thoughts
Installing and configuring the Android NDK can seem complex at first, but following these steps will help you set up your development environment efficiently. Always check for updates and follow best practices to ensure smooth development and debugging of your native applications. The NDK is a powerful tool for achieving high performance and low latency in your applications, making it an essential part of any Android developer's toolkit.