Main Contents:

  • Create a new project that supports C/C++
  • Add C/C++ code to an existing project and compile it
    • Use a combination of CMake and CMakeLists
    • Use the combination of NDK-build, Android.mk and Application.mk

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

  • Android Native Development Kit (NDK)This toolset allows you to use C and C++ code for Android and provides a number of 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 can be used with Gradle to build native libraries. This component is not required if you only plan to use the NdK-build.
  • LLDB: a debugger that Android Studio uses toDebugging native code.

No, click SDK Manager to download it.

Create a new project that supports C/C++

Android Studio 2.2 or later, the default tool for building native libraries is CMake. Creating a project that supports native code (C/C++) requires the following steps:

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

  2. Click Next.

  3. Fill in all other fields as normal and complete the next sections of the wizard.

  4. 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 C++ Standard 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 this check box is enabled, Android Studio adds the -FExceptions flag to cppFlags in the module-level build.gradle file, which Gradle passes to CMake.
  • Runtime Type Information Support: Select this check box if you wish to Support RTTI. If this check box is enabled, Android Studio adds the -frtti flag to cppFlags in the module-level build.gradle file, which Gradle passes to CMake.
  1. Click Finish.

After Android Studio completes the creation of the new Project, open the Project pane from the left side of the IDE and select Android View. As shown, Android Studio will add CPP and External Build Files groups:

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

In the CPP group, you can find all the original source files, headers, and pre-built libraries that belong to 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 the application module. This sample code provides a simple C++ function, stringFromJNI(), that returns the string “Hello from C++”.

In the External Build Files group, you can find Build scripts for CMake or NdK-Build. Just as the build.gradle file instructs Gradle how to build your application, CMake and NdK-Build require a build script to understand how to build your native libraries. For new projects, Android Studio creates a CMake build script, cmakelists.txt, and places it in the module’s root directory.

When you click Run, Android Studio will build and launch an application that displays the text “Hello from C++” on your Android device or emulator.


Add C/C++ code to an existing project and compile it

Build native code using a combination of CMake and CMakeLists

Create a new source file (C/C ++ source file)

To create a CPP/directory containing the new source files in the main source set of your application module, follow these steps:

  1. Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu.
  2. Navigate to your module -> SRC, right-click the main Directory, and select New -> Directory.
  3. Enter a name for the directory (for examplecpp) and clickOK.
  4. Right click on the directory you just created and select New -> C/C++ Source File.
  5. Enter a name for your source file, for examplenative-lib.
  6. fromTypeFrom the drop-down menu, select a file extension for your source file, for example.cpp.
    • Click on theEdit File Types, you can add other file types from the drop-down menu, for example.cxx 或 .hxx. In the pop-upC/C++Dialog box fromSource Extension 和 Header ExtensionSelect another file extension from the drop-down menu and clickOK.
  7. If you also want to Create a header file, select the Create AN Associated Header check box.
  8. Click OK.

Create the CMake build script

  1. Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu.
  2. Right-click the root directory of your module and select New > File.
  3. Enter “cmakelists.txt” as the file name and click OK.

Note: The CMake build script is a plain text file and must be named cmakelists.txt.

To instruct CMake to create a native library from native source, add the cmake_minimum_required() and add_library() commands to your build script:

# Sets the minimum version of CMake required to build your native library.
# This ensures that a certain set of CMake features is available to
# your build.Cmake_minimum_required (VERSION 3.4.1 track)# Specifies a library name, specifies whether the library is STATIC or
# SHARED, and provides relative paths to the source code. You can
# define multiple libraries by adding multiple add.library() commands,
# and CMake builds them for you. When you build your app, Gradle
# automatically packages shared libraries with your APK.

add_library( # Specifies the name of the library.
             native-lib

             # Sets the library as a shared library.
             SHARED

             # Provides a relative path to your source file(s).
             src/main/cpp/native-lib.cpp )

Copy the code

Load the library in Your Java code

The static {System. LoadLibrary (" native - lib "); }Copy the code

Gradle associated CMake

Use the AS UI function menu
  1. Open the Project pane from the left side of the IDE and select the Android View.
  2. Right-click the module you want to associate with the native library (for example, the APP module) and choose from the menuLink C++ Project with Gradle. From the drop-down menu, select CMake. Use the fields next to Project Path to specify the cmakelists.txt script file for your external CMake Project.
Manually configure Gradle

To manually configure Gradle to associate it with your native library, you need to add the externalNativeBuild {} block to the module-level build. Gradle file and configure it with cmake {} :

android { ... defaultConfig {... } buildTypes {... } // Encapsulates your external native build configurations. externalNativeBuild { // Encapsulates your CMake build configurations. cmake { // Provides a relative path to your CMake build script. path"CMakeLists.txt"}}}Copy the code

To use the NDK – build,Android.mkApplication. Mk combined to build native code

Create the JNI folder

For your module (such as app module), right-click the menu ->New->Folder->JNI Folder to create a JNI Folder.

Jni directory to create C/C++ source files

See the CMake section above how to create a source file

Create android. mk and application. mk in jni directory

Android.mk :

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE    := hello-jni
LOCAL_SRC_FILES := hello-jni.c

include $(BUILD_SHARED_LIBRARY)
Copy the code

Application.mk :

APP_ABI :=all
Copy the code

The final file structure

Configuration gradle

Use the menu
  1. Open the Project pane from the left side of the IDE and select the Android View.
  2. Right-click the module you want to associate with the native library (for example, the APP module) and choose from the menuLink C++ Project with Gradle. From the drop-down menu, selectndk-build. Use the field next to Project Path to specify the android. mk file Path.
Manual configuration
android { ... defaultConfig {... } buildTypes {... } externalNativeBuild { ndkBuild { path'src/main/jni/Android.mk'}}}Copy the code

The demo address

As uses 3.1.4

CMake form

The NDK – build form