AndroidStudio使用Cmake編譯armeabi-v7a,arm64-v8a的so庫
阿新 • • 發佈:2020-11-24
使用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庫