The purpose of this article:

C/C++ source code directly compiled into a static library, only available for low-level calls.

Note: more suitable for third-party open source libraries compiled into a static library, you can also write their own source code compiled into a static library, for yourself or others of the underlying call.

Contents of this article:

1. Configure the development environment

2. Compile static library methods

3. Call the static library method

Example 4.

1. Configure the development environment

Environment configuration:

Development tools: Android Studio 3.0.1

Android SDK Tools: Select CMake, LLDB, and NDK

JDK version: JDK 1.8

NDK version: 18.1.5063045

Compile method: CMake

Third party C/C++ open source library: cJSON

New Android project configuration:

Include C++ Support: select to configure the NDK environment. The CPP directory and cmakelists.txt are automatically created and the NDK is automatically configured in Gradle

C++ Standard: to choose which C++ Standard to use, select Toolchain Default. The default CMake Settings will be used.

Exceptions Support: enable Support for C++ exception handling. Check this box.

Runtime Type Information Support: Select to enable Runtime Type Information Support.

2. Compile static library methods

See not

(1) Build a new project according to the above Android project configuration.

(2) Take the cJSON open source library as an example. Download the latest version of cJSON source code on Github and put it in the CPP directory of the project:

(3) Configure cmakelists. TXT file:

cmake_minimum_required(VERSION 3.4.1)

# print LOG
message(STATUS "Cmake build type is: "${CMAKE_BUILD_TYPE})
message(STATUS "Cmake build android abi is: "${ANDROID_ABI})

Set the output path for static and dynamic libraries
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/libs/${CMAKE_BUILD_TYPE}/${ANDROID_ABI})
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/output/libs/${CMAKE_BUILD_TYPE}/${ANDROID_ABI})

# set variable
set(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)

# import header files
include_directories(
        ${LOCAL_PATH}/libs/source/cjson/
        )

Add static libraries
add_library( # Sets the name of the library.
             cjson

             # Sets the library as a shared library.
             STATIC

             # Provides a relative path to your source file(s).
             ${LOCAL_PATH}/libs/source/cjson/cJSON.c
             ${LOCAL_PATH}/libs/source/cjson/cJSON_Utils.c
             )

add_library( # Sets 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 )

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       native-lib
                       cjson  # link the static library for testing, if not, no link is required

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

(C/C++); (arguments.arguments.gradle); (C/C++);

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
				//arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"  
                // The list of variables configured by CMake is used by default if this line is not written
                cppFlags "-frtti -fexceptions"
                //abiFilters /*"armeabi",*/ "armeabi-v7a", "arm64-v8a", "x86", "x86_64" // do not write this line to compile the next 4 CPU libraries by default}}}... externalNativeBuild { cmake { path"CMakeLists.txt"}}}Copy the code

(5) Compile with gradlew Assemble on the command line, the static library will be output in the set folder.

3. Call the static library method

See Demo2

(1) Build a new project according to the above Android project configuration.

(2) Taking the cJSON open source library as an example, put the compiled static library into the CPP directory of the project:

(3) introduce headers and rewrap them with C++ classes:

#ifndef __Cjson_Utils_H__
#define __Cjson_Utils_H__

// import the C++ header file here
//#include "CObjectUtils.h"

#ifdef __cplusplus
extern "C" {
#endif

// Import the C header file here
#include "cJSON.h"
#include "cJSON_Utils.h"

#ifdef __cplusplus
};
#endif

class CCjsonUtils{
public:
    CCjsonUtils();
    virtual ~CCjsonUtils();

    static const char* getCjsonVersion(a);
};

#endif
Copy the code

(4) Configure cmakelists. TXT file:

cmake_minimum_required(VERSION 3.4.1)

# print LOG
message(STATUS "Cmake build type is: "${CMAKE_BUILD_TYPE})
message(STATUS "Cmake build android abi is: "${ANDROID_ABI})

# set variable
set(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)

# import header files
include_directories(
        ${LOCAL_PATH}/libs/include/cjson/
        ${LOCAL_PATH}/cjsonutils/
        )

# import static library
add_library(cjson STATIC IMPORTED)
set_target_properties(cjson    PROPERTIES IMPORTED_LOCATION    ${LOCAL_PATH}/libs/${ANDROID_ABI}/libcjson.a )

add_library( # Sets 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
             ${LOCAL_PATH}/cjsonutils/CCjsonUtils.cpp  # create C++ tool class, used to encapsulate C source code, easy to simplify the interface
             )

find_library( # Sets the name of the path variable.
              log-lib

              # Specifies the name of the NDK library that
              # you want CMake to locate.
              log )

target_link_libraries( # Specifies the target library.
                       native-lib
                       cjson  # Link static libraries

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

Build. Gradle = build.gradle = build.gradle = build.gradle = build.gradle

Example 4.

Not address: AndroidNdkCompileStaticLibrary

Demo2 address: AndroidNdkInvokeStaticLibrary