AndroidStudio使用Cmake编译armeabi-v7a,arm64-v8a的so库
使用AndroidStudio编译armeabi-v7a,arm64-v8a库文件步骤:
1.新建项目
2.修改CMakeLists.txt文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | # 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文件
1 2 3 4 | //ndk必须定义在defaultConfig目录下,设置需要生成的cpu平台,以下这两个就能够兼容绝大多数的Android平台 ndk{ abiFilters 'armeabi-v7a' , 'arm64-v8a' } |
4.执行编译
编译后会在cpp文件夹下自动新建一个libs文件里面就会存放各个平台对应的动态共享so库
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探