Android NDK祕籍--編譯靜態庫、呼叫靜態庫

一隻斑馬在搬碼發表於2019-04-21

此篇文章的目的:

將C/C++的原始碼直接編譯成靜態庫,只能提供給底層呼叫。

注:比較適用於將第三方開源庫編譯成靜態庫,也可以將自己寫的原始碼編譯成靜態庫,給自己或其他人的底層呼叫。

本文目錄:

1.開發環境配置

2.編譯靜態庫方法

3.呼叫靜態庫方法

4.示例

1.開發環境配置

環境配置:

開發工具:Android Studio 3.0.1

Android SDK Tools:額外勾選CMake、LLDB、NDK三個選項

JDK版本:JDK 1.8

NDK版本:18.1.5063045

編譯方式:CMake

第三方C/C++開源庫:cJSON

新建Android專案配置:

Include C++ Support:進行配置NDK環境,勾選。主要是自動建立cpp目錄和CMakeLists.txt,並自動在gradle中進行了ndk配置

C++ Standard:選擇使用哪種C++標準,選擇Toolchain Default。會使用預設的CMake設定。

Exceptions Support:啟用對C++異常處理的支援,勾選。

Runtime Type Information Support:啟用對執行時型別資訊的支援,勾選。

2.編譯靜態庫方法

詳見Demo1

(1)先按照上述Android專案配置新建工程。

(2)以cJSON開源庫為例,github上下載最新版本的cJSON原始碼,放入到專案的cpp目錄下:

EF3KpR.jpg

(3)配置CMakeLists.txt檔案:

cmake_minimum_required(VERSION 3.4.1)

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

#設定靜態庫和動態庫的輸出路徑
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(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)

#引入標頭檔案
include_directories(
        ${LOCAL_PATH}/libs/source/cjson/
        )

#增加靜態庫
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  #連結該靜態庫進行測試, 如果不測試, 則不需要連結

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
複製程式碼

(4)配置build.gradle檔案,主要是配置編譯哪種CPU型別的庫,使用abiFilters,有些複雜的C/C++原始碼在編譯不同CPU型別的庫時,還需要一些其他的配置,使用arguments,具體配置根據需求進行更改,配置如下:

android {
    ...
    defaultConfig {
        ...
        externalNativeBuild {
            cmake {
				//arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang"  
                //不寫此行則預設使用CMake配置的變數列表
                cppFlags "-frtti -fexceptions"
                //abiFilters /*"armeabi",*/ "armeabi-v7a", "arm64-v8a", "x86", "x86_64" 				//不寫此行則預設編譯後面4個CPU型別的庫
            }
        }
    }
    ...
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
}
複製程式碼

(5)在命令列使用gradlew assemble命令進行編譯,靜態庫會輸出在設定的資料夾中。

3.呼叫靜態庫方法

詳見Demo2

(1)先按照上述Android專案配置新建工程。

(2)以cJSON開源庫為例,將編譯好的靜態庫放入到專案的cpp目錄下:

EF3a9A.jpg

(3)引入標頭檔案,並使用C++類重新進行了封裝:

#ifndef __Cjson_Utils_H__
#define __Cjson_Utils_H__

//此處匯入C++的標頭檔案
//#include "CObjectUtils.h"

#ifdef __cplusplus
extern "C" {
#endif

//此處匯入C的標頭檔案
#include "cJSON.h"
#include "cJSON_Utils.h"

#ifdef __cplusplus
};
#endif

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

    static const char* getCjsonVersion();
};

#endif
複製程式碼

(4)配置CMakeLists.txt檔案:

cmake_minimum_required(VERSION 3.4.1)

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

#設定變數
set(LOCAL_PATH ${CMAKE_SOURCE_DIR}/src/main/cpp)

#引入標頭檔案
include_directories(
        ${LOCAL_PATH}/libs/include/cjson/
        ${LOCAL_PATH}/cjsonutils/
        )

#匯入靜態庫
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  #建立的C++工具類, 用於對C原始碼進行封裝, 便於簡化介面
             )

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  #連結靜態庫

                       # Links the target library to the log library
                       # included in the NDK.
                       ${log-lib} )
複製程式碼

(5)配置build.gradle,使用預設的即可,然後就可以正常呼叫了。

4.示例

Demo1地址:AndroidNdkCompileStaticLibrary

Demo2地址:AndroidNdkInvokeStaticLibrary

相關文章