使用AndroidStudio編譯armeabi-v7a,arm64-v8a庫文件步驟:
1.新建項目
2.修改CMakeLists.txt文件
# For more information about using CMake with Android Studio, read the # documentation: https://d.android.com/studio/projects/add-native-code.html # Sets the minimum version of CMake required to build the native library. #指定cmake的最小版本號 cmake_minimum_required(VERSION 3.4.1) #定義項目名稱,可以不定義 #project(demo) # Creates and names a library, sets it as either STATIC # or SHARED, and provides the relative paths to its source code. # You can define multiple libraries, and CMake builds them for you. # Gradle automatically packages shared libraries with your APK. #設置so庫的輸出路徑 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/libs/${ANDROID_ABI}) #設置編譯類型 #add_executable(demo demo.cpp) # 生成可執行文件 #生成動態共享庫 add_library( # 設置編譯成so庫的名稱 native-lib # 生成動態庫或共享庫,此處如果SHARED改為STATIC,其含義是生成靜態庫 SHARED # 提供一個需要編譯的源文件的相對路徑,native-lib.cpp就是需要編譯的源文件 native-lib.cpp ) #明確指定編譯時需要編譯哪些源文件 #add_library(demo demo.cpp test.cpp util.cpp) #aux_source_directory(dir VAR) 發現一個目錄下所有的源代碼文件並將列表存儲在一個變量中。 #例如:aux_source_directory(. SRC_LIST) # 搜索當前目錄下的所有.cpp文件 #add_library(demo ${SRC_LIST}) # Searches for a specified prebuilt library and stores the path as a # variable. Because CMake includes system libraries in the search path by # default, you only need to specify the name of the public NDK library # you want to add. CMake verifies that the library exists before # completing its build. #查找到指定的預編譯庫,並將它的路徑存儲在變量中 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 ) # Specifies libraries CMake should link to your target library. You # can link multiple libraries, such as libraries you define in this # build script, prebuilt third-party libraries, or system libraries. #設置target需要鏈接的庫 target_link_libraries( # Specifies the target library.目標庫 native-lib # Links the target library to the log library 目標庫需要鏈接的庫,log-lib是上面find_library指定的變量名 # included in the NDK. ${log-lib} )
3.修改app的build.gradle文件
//ndk必須定義在defaultConfig目錄下,設置需要生成的cpu平台,以下這兩個就能夠兼容絕大多數的Android平台 ndk{ abiFilters 'armeabi-v7a','arm64-v8a' }
4.執行編譯
編譯后會在cpp文件夾下自動新建一個libs文件里面就會存放各個平台對應的動態共享so庫