In this codelab, you'll learn how to use Android Studio CMake template to start Android NDK project development in just a few clicks to arrive at:

What you'll learn

What you'll need

How will you use this tutorial?

Read it through only Read it and complete the exercises

How would rate your experience with building Android apps?

Novice Intermediate Proficient
  1. Find and start Android Studio on your development system:
    a) On Windows, Start > All apps > Android Studio
    b) On Mac OS X: Double-click on Android Studio in your Application folder
    c) On Linux: Run from your installed location

    If this is the first time you are running this version of Android Studio, Android Studio will prompt you to import settings from your previous installation, either import settings from a previous installation or accept default settings only . The "Welcome to Android Studio" window will appear.

  2. Select "Start a new Android Studio project".
  3. In the "Choose your project" dialog, choose "Native C++".

  4. Click "Next".
  5. In the "Configure your project" dialog, change "Application Name" to Hello-cmake, switch the language to "Kotlin" and be sure "Use androidx.* artifacts" is checked. Leave the other fields as they are.
  6. Click "Next".
  7. In the "Customize C++ Support" dialog, and select "C++11" for "C++ Standard", feel free to select different language standards for the project (This sample does not use any C++11/C++14 specific features). This setting creates and sets up "cppFlags" in your project's build.gradle file.

  8. Click "Finish" to complete application creation. After Android Studio finishes creating your project, it should look like the following:
  9. If you have a physical Android device available, connect it to your workstation with a USB cable; otherwise, create an Emulator.
  10. Run the project.

  1. From the "Project" pane on the left side of the IDE, expand the "External Build Files" node and double-click "CMakeLists.txt". This is the CMake build script that Android Studio generates for your sample project that produces file, is loaded by your project's Java code. The CMakeLists.txt should look similar to:
# Sets the minimum version of CMake required to build the native
# library.
cmake_minimum_required(VERSION 3.4.1)

# Creates the project's shared lib:
# The lib is loaded by this project's Java code in
#     System.loadLibrary("native-lib");
# The lib name in both places must match.
add_library( native-lib
             src/main/cpp/native-lib.cpp )

             log )

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in the

target_link_libraries( # Specifies the target library.

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )

  1. From the "Project" pane, open "Gradle Scripts" > "build.gradle(Module:app)". In the android.externalNativeBuild.cmake{} block, you can see the bridging code from Gradle to CMake:
// Sets up parameters for both jni build and cmake.
// For a complete list of parameters, see
externalNativeBuild {
   cmake {
       // cppFlags are configured according to your selection
       // of "Customize C++ Support", in this codelab's
       //    "Create a Sample App with the C++ Template",
       //    step 6
       cppFlags "-std=c++11"

// Specifies the location of the top level CMakeLists.txt
// The path is relative to the hosting directory
// of this build.gradle file
externalNativeBuild {
   cmake {
       path "src/main/cpp/CMakeLists.txt"
       version "3.10.2"


The Android CMake toolchain provides a group of variables for your CMake scripts to use and configure build parameters. To learn more, read Using CMake variables in Gradle.

  1. Under app > cpp, open the file "native-lib.cpp".
  2. It's possible to navigate to native code inside Android Studio. For example, to locate the prototype for native function "NewStringUTF()", simply right click on the "NewStringUTF" in env->NewStringUTF(hello.c_str()), and select "Go To" > "Implementation(s)".
    Android Studio will open jni.h inside IDE, and show you the function prototype right away.

  3. Close jni.h source window and go back to native-lib.cpp. Locate the following line of code: std::string hello = "Hello from C++". Set a breakpoint by clicking the left gutter along that line of code or place the caret on the line and press Control+F8 (or Command+F8 on Mac).

  1. Make sure your Android device is enabled with USB debugging, then click the debug button to deploy your app. Your target device should prompt "Waiting For Debugger" message:

    Do not tap "FORCE CLOSE".
    Note: Android Studio might prompt to install "Instant Run" before starting application; in that case, choosing either "Proceed without Instant Run" or "Install and Continue" is fine for this codelab.
  2. Wait until Android Studio connects to the debugger on your device (it might take a short time for the first time to connect debugger, depending on the device and Android platform version) and stops at the breakpoint:

  3. In the "Debug" window, click "env" inside the "Variables" tab to observe the contents of env pointer. You can now step over code with the F8 key and perform other debugging activities. Press F9 to resume execution--you should see the application finish execution on target device!

Starting from Android Studio 2.2.0, it is simple to create and manage a project that includes C/C++ code; begin your new C/C++ projects with Android Studio + CMake today!

What we've covered with Android Studio

Next Steps

Learn More

Tell us how we did