Android NDK祕籍--編譯靜態庫、調用靜態庫

此篇文章的目的:android

將C/C++的源碼直接編譯成靜態庫,只能提供給底層調用。c++

注:比較適用於將第三方開源庫編譯成靜態庫,也能夠將本身寫的源碼編譯成靜態庫,給本身或其餘人的底層調用。git

本文目錄:github

1.開發環境配置json

2.編譯靜態庫方法ide

3.調用靜態庫方法工具

4.示例開發工具

1.開發環境配置

環境配置:測試

開發工具:Android Studio 3.0.1gradle

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

相關文章
相關標籤/搜索