Android NDK Installation Guide

Android Studio
android-ndk-installation-guide
Source: Developer.android.com

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:

  1. Android Studio: The recommended Integrated Development Environment (IDE) for Android development. Download the latest version from the official Android website.
  2. Java Development Kit (JDK): Required for the NDK to function. Use either the Oracle JDK or an open-source alternative like AdoptOpenJDK.
  3. Command Line Tools: Familiarity with command line tools is essential for managing the NDK and SDK.

Installing Android Studio

Download Android Studio

  1. Open your web browser and navigate to the official Android Studio download page.
  2. Click the "Download" button to get the Android Studio bundle.

Run the Installer

  1. Once the download completes, run the downloaded program.
  2. Follow the installation wizard to install all Android components.

Choose Installation Directories

  1. When prompted, choose installation directories for Android Studio and the Android SDK (e.g., C:\Android\android-studio and C:\Android\sdk).

Launch Android Studio

  1. After installation, launch Android Studio to ensure proper functionality.
  2. If prompted to import settings from a previous installation, select your preferred option and click "OK."

Close the Welcome Screen

  1. Close the welcome screen as it will not be needed for now.

Installing the Android NDK

Download the NDK

  1. Navigate to the Android NDK download page.
  2. Select the NDK package for your development platform. The latest Long Term Support (LTS) version is recommended.

Extract the NDK Archive

  1. Once downloaded, extract the archive to a directory of your choice (e.g., C:\Android\ndk).

Set Environment Variables

  1. Declare the Android SDK and NDK as environment variables to easily access Android utilities from the command line.
  2. Open the Environment Variables system window.
  3. 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)
  4. Prepend %ANDROID_SDK%\tools;%ANDROID_SDK%\platform-tools;%ANDROID_NDK%; at the beginning of your PATH environment variable.

Verify NDK Installation

  1. Open a command prompt or terminal.
  2. List the Android devices connected to your computer using adb devices.
  3. Check if the NDK is working by running ndk-build -version.

Configuring Android Studio for NDK

Open SDK Manager

  1. Open the Android SDK Manager, located in the ADB bundle directory's root.
  2. Click "New" to select all the packages, then click "Install packages…"
  3. Accept the licenses in the popup and start the installation of Android development packages by clicking "Install."

Install NDK and CMake

  1. With a project open, click Tools > SDK Manager.
  2. Click the SDK Tools tab.
  3. Select the NDK (Side by side) and CMake checkboxes.
  4. Click OK.
  5. A dialog box will show the space the NDK package consumes on disk. Click OK.
  6. When the installation completes, click Finish.
  7. 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

  1. With a project open, click Tools > SDK Manager.
  2. Click the SDK Tools tab.
  3. Select the Show Package Details checkbox.
  4. Select the NDK (Side by side) checkbox and the checkboxes below it corresponding to the NDK versions you want to install.
  5. Click OK.
  6. A dialog box will show the space the NDK package(s) consume. Click OK.
  7. When the installation completes, click Finish.

Configure Each Module with Specific Versions

  1. When using Android Studio 3.6 or higher, if you do not specify the version, the Android Gradle plugin chooses a compatible version.
  2. 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

  1. Open Android Studio.
  2. Click on File > New > New Project.
  3. Select "Empty Activity" and click "Next."
  4. Choose "C/C++" as the project type and click "Next."
  5. Choose the NDK version you want to use and click "Finish."

Add Native Source Files

  1. Open your project in Android Studio.
  2. Go to File > New > C/C++ Source File.
  3. Name your file (e.g., native-lib.cpp) and click "OK."
  4. Add your native code in this file.

Configure CMake

  1. Open your build.gradle file.

  2. Add the following configuration:
    groovy
    android {

    externalNativeBuild {
    cmake {

    version "cmake-version"
    }
    }
    }

  3. Ensure your CMakeLists.txt file is correctly configured to build your native library.

Debugging Native Code

Set Up LLDB

  1. The Native Development Kit (NDK) uses LLDB as its debugger.
  2. Ensure LLDB is installed and configured correctly.

Debugging Steps

  1. Open your project in Android Studio.
  2. Go to Run > Edit Configurations.
  3. Click the "+" button to create a new configuration.
  4. Choose "LLDB" as the debugger type and select your project.
  5. Set breakpoints in your native code and run the debugger.

Troubleshooting Common Issues

Environment Variables Not Set Correctly

  1. 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

  1. 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

  1. 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

  1. 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.

Was this page helpful?