工具下載:在SDK-Tool中下載CMake, LLDB ,NDK。


項目創建


配置最后頁面的這兩項也選上,方便代碼調試。

配置庫名稱及庫的輸出路徑和格式:
1.配置CMakeLists.txt
#設置編譯時CMake的最低需求版本
cmake_minimum_required(VERSION 3.4.1)
#設置生成的so動態庫最后輸出的路徑
#set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/../jniLibs/${ANDROID_ABI})
add_library(
#添加的庫名
test-lib
#庫的類型:SHARED表示動態so庫,STATIC表示靜態a庫
SHARED
#編譯的cpp文件源文件路徑
src/main/cpp/native-lib.cpp)
#設置NDK本地庫里你想使用的庫的引用名
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)
#設置目標so庫生成時,要關聯的庫
target_link_libraries( # Specifies the target library.
test-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
2.配置gradle文件,指定so庫CPU框架

有多個cpp/c文件需要編譯的情況處理方式:
1.指定編譯路徑下所有的文件
#對應需要編譯cpp的文件路徑
file(GLOB my_srcs "src/main/cpp/*")
add_library( test-lib SHARED ${my_srcs})
2.將每個cpp文件變為一個庫
add_library( test1-lib SHARED native1-lib.cpp)
add_library(test2-lib SHARED native2-lib.cpp)

