Add C and C++ code to Your project

You can add C and C++ code to your Android project by placing the code into a cpp
directory in your project module. When you build your project, this code is
compiled into a native library that Gradle can package with your APK.
Your Java or Kotlin code can then call functions in your native library through the
Java Native Interface (JNI). If you want to learn more about using the JNI
framework, read JNI tips for
Android.

Android Studio's default build tool for native libraries is CMake. Android
Studio also supports ndk-build due
to the large number of existing projects that use the build toolkit to
compile their native code. If you want to import an existing ndk-build
library into your Android Studio project, learn how to
link Gradle to
your native library project.
However, if you are creating a new native library, you should use CMake.

Download the NDK and build tools

To compile and debug native code for your app, you need the following
components:

The Android Native Development Kit
(NDK): a toolset that allows you to use C and C++ code with
Android, and provides platform libraries that allow you to manage native
activities and access physical device components, such as sensors and touch
input.

CMake: an
external build tool that works alongside Gradle to build your native
library. You do not need this component if you only plan to use ndk-build.

Specify the CMake version you want Gradle to use in your
module's build.gradle file:

android {
...
externalNativeBuild {
cmake {
...
version "3.7.1"
}
}
}

Either add the path to the CMake installation to your
PATH environment variable or include it in
your project's local.properties file, as shown below.
If Gradle is unable to find the version of CMake you specified in
your build.gradle file, you get a build error.

# If you set this property, Gradle no longer uses PATH to find CMake.
cmake.dir="path-to-cmake"

If you don't already have the Ninja build system installed on your
workstation, go to the
official Ninja website,
and download and install the latest version of Ninja available for your OS.
Make sure to also add the path to the Ninja installation to your
PATH environment variable.

Warning: Support for using CMake 3.7 and higher with Android Studio is
a preview feature. If you experience any issues, please
report a bug.

Create a new project with C/C++ support

In the Configure your new project section of the wizard,
check the Include C++ Support checkbox.

Click Next.

Complete all other fields and the next few sections of the wizard as
normal.

In the Customize C++ Support section of the wizard, you
can customize your project with the following options:

C++ Standard: use the drop-down list to select which
standardization of C++ you want to use. Selecting Toolchain
Default uses the default CMake setting.

Exceptions Support: check this box if you want to enable
support for C++ exception handling. If enabled, Android Studio adds the
-fexceptions flag to cppFlags in your
module-level build.gradle file, which Gradle passes to
CMake.

Runtime Type Information Support: check this box if
you want support for RTTI. If enabled, Android Studio adds the
-frtti flag to cppFlags in your module-level
build.gradle file, which Gradle passes to CMake.

Click Finish.

After Android Studio finishes creating your new project, open the
Project pane from the left side of the IDE and select the
Android view. As shown in figure 2, Android Studio adds the
cpp and External Build Files groups:

Note: This view does not reflect the actual file hierarchy
on disk, but groups similar files to simplify navigating your project.

The cpp group is where you can find all the native
source files, headers, and prebuilt libraries that are a part of your
project. For new projects, Android Studio creates a sample C++ source file,
native-lib.cpp, and places it in the src/main/cpp/
directory of your app module. This sample code provides a simple C++
function, stringFromJNI(), that returns the string "Hello from
C++". You can learn how to add additional source files to your project in
the section about how to Create new native source
files.

Known Issue: Android Studio currently shows you only the header
files that have matching source files—even if you specify other headers in
your
CMake build script. See
Issue #38068472

The External Build Files group is where you can find
build scripts for CMake or ndk-build. Similar to how
build.gradle files tell Gradle how to build your app, CMake and
ndk-build require a build script to know how to build your native library.
For new projects, Android Studio creates a CMake build script,
CMakeLists.txt, and places it in your module’s root directory.
To learn more about the contents of this build script, read
Configure CMake.

Build and run the sample app

When you click Run, Android Studio
builds and launches an app that displays the text "Hello from C++" on your
Android device or emulator. The following overview describes the events that
occur in order to build and run the sample app:

Gradle calls upon your external build script,
CMakeLists.txt.

CMake follows commands in the build script to compile a C++ source
file, native-lib.cpp, into a shared object library and names
it libnative-lib.so, which Gradle then packages into the APK.

During runtime, the app's MainActivity loads the native
library using System.loadLibrary(). The library’s native function,
stringFromJNI(), is now availableto the app.

MainActivity.onCreate() calls stringFromJNI(),
which returns "Hello from C++", and uses it to update the TextView.

Note:Instant
Run is not compatible with components of your project written in native
code.

If you want to verify that Gradle packages the native library in the APK, you
can use the APK Analyzer:

Select Build > Analyze APK.

Select the APK from the app/build/outputs/apk/ directory and
click OK.

As shown in figure 3, you can see libnative-lib.so in the
APK Analyzer window under lib/<ABI>/.

Figure 3. Locating a native library using the APK
Analyzer.

Tip: If you want to experiment with other Android apps that
use native code, click File > New > Import Sample and
select a sample project from the Ndk list.

Create new C/C++ source files

To add new C/C++ source files to an existing project, proceed as follows:

If you don't already have a cpp/ directory in the main source
set of your app, create one as follows:

Open the Project pane from the left side of the IDE and
select the Project view from the drop-down menu.

Navigate to your-module > src, right-click
on the main directory, and select New >
Directory.

Enter cpp as the directory name and click
OK.

Right-click on the cpp/ directory and select New >
C/C++ Source File.

Enter a name for your source file, such as native-lib.

From the Type drop-down menu, select the file extension
for your source file, such as .cpp.

You can add other file types to the drop-down menu, such as
.cxx or .hxx, by clicking Edit File Types. In the C/C++ dialog box that pops up,
select another file extension from the Source Extension and
Header Extension drop-down menus and click
OK.

If you also want to create a header file, check the Create an
associated header checkbox.

Click OK.

After you add new C/C++ files to you project, you still need to
configure CMake to include them in
your native library.

Migrate from ndkCompile

If you're using the deprecated ndkCompile, you should migrate to
using either CMake or ndk-build. Because ndkCompile generates
an intermediate Android.mk file for you, migrating to
ndk-build may be a simpler choice.

To migrate from ndkCompile to ndk-build, proceed as follows:

Compile your project with ndkCompile at least once by
selecting Build > Make Project. This generates the
Android.mk file for you.

Locate the auto-generated Android.mk file by navigating to
project-root/module-root/build/intermediates/ndk/debug/Android.mk.

Relocate the Android.mk file to some other directory, such as
the same directory as your module-level build.gradle file.
This makes sure that Gradle doesn't delete the script file when running
the clean task.

Open the Android.mk file and edit any paths in the script
such that they are relative to the current location of the script file.