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 "New Project" dialog, change "Application Name" to Hello-cmake, and check the "Include C++ Support" checkbox; leave other fields to their default settings.

  4. Click "Next".
  5. In the "Add an Activity to Mobile" dialog, select "Empty Activity" as the template.
  6. Keep clicking "Next" until you reach the "Customize C++ Support" dialog, and select "C++11" for "C++ Standard" just to demonstrate how to select different language standards for the project ( this sample does not have to be C++11 or C++14 ). In effect, this creates and sets up "cppFlags" in your project's build.gradle file.

  7. Click "Finish" to complete application creation. After Android Studio finishes creating your project, it should look like the following:
  8. If you have a physical Android device available, connect it to your workstation with a USB cable; otherwise, create an Emulator.
  9. Run the project.
  10. In the "Select Deployment Target" dialog, select a device and click "OK". You will see the following on your target device (either your physical device or your emulator):

  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
# build script, prebuilt third-party libraries, or system libraries.

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 "CMakeLists.txt"

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. You could navigate JNI code inside Android Studio. For example, to find out the prototype for JNI function "NewStringUTF()", simply right click on "NewStringUTF", and select "Go To" > "Implementation(s)".
    Android Studio will open jni.h inside IDE, and show you the function prototype right away.

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