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

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 Linux: Run from your installed location
    b) On Mac OS X: Double-click on Android Studio in your Application folder
    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, select the default to import previous settings or other available options. 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 "Basic Activity" as the template.
  6. Keep clicking "Next" until you reach the "Customize C++ Support" dialog, and select "C++11" for "C++ Standard". Keep the default values for all other fields.
  7. Click "Finish" to complete application creation. When Android Studio finishes creating your project, it should look like the following:
  8. (Optional) If you have a physical Android device available, connect it to your workstation with a USB cable; otherwise, create an Emulator..
  9. From the toolbar, click Sync , Make, and then Run .
  10. In the "Select Deployment Target" dialog, select either your target physical device or Emulator, and click "OK". You will see the following on your target device or Emulator:

  1. From the "Project" pane on the left side of the IDE, expand "External Build Files" and double-click "CMakeLists.txt" to browse the cmake build script Android Studio generated for your sample project. It should looks like:
# Sets the minimum version of CMake required to build the native
# library. You should either keep the default value or only pass a
# value of 3.4.0 or lower.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds it for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.

             # Sets the library as a shared library.

             # Provides a relative path to your source file(s).
             # Associated headers in the same location as their source
             # file are automatically included.
             src/main/cpp/native-lib.cpp )

# Searches for a specified prebuilt library and stores the path as a
# variable. Because system libraries are included in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              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 Android Studio. In the android.externalNativeBuild.cmake{} block, you can see the bridging code from gradle to cmake:
// Set up parameters for jni build, including cmake command // line ones. For complete list of parameters, refer to:
// or android.
externalNativeBuild {
   cmake {
       cppFlags "-std=c++11"

// Inform the location of the top level CMakeLists.txt
// File path is relative to the hosting directory
// of this build.gradle file
externalNativeBuild {
   cmake {
       path "CMakeLists.txt"

Android CMake toolchain provides a group of variables for your CMake scripts to use and configure build parameters, refer to ndk documentation and your android.toolchain.cmake inside NDK-r13 (for ndk-r12, Android Studio uses the version inside SDK).

  1. Open the generated C++ source file native-lib.cpp, select header file "jni.h", right click on it to bring the pop up menu, select "Go To" → "Implementation(s)":

    jni.h will open inside IDE.
  2. Close jni.h source window and go back to native-lib.cpp; click the left edge of the code line [std::string hello = "Hello from C++";] to set a breakpoint

Note: the generated prototype should have JNIEXPORT JNICALL in it, later versions of Android Studio fixed this.

  1. Click the Debug button, your target device should prompt "Waiting For Debugger" message:

    Do not tap "FORCE CLOSE".
  2. Wait until Android Studio connects to the debugger on your device (it might take 1 - 2 minutes, 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 F8 function key and perform other debugging activities. Press F9 function key to resume execution--you should see app finishes on target device!

With Android Studio 2.2.0, it is simple to create a project which includes C/C++ code; begin your new native projects with Android Studio!

What we've covered with Android Studio

Next Steps

Learn More

Tell us how we did