Android Studio gardle 配置 ndk 指定 ABI: abiFilters 详解

一、ABI 是什么

ABI 是 Application Binary Interface 的缩写。

不同 Android 手机使用不同的 CPU,因此支持不同的指令集。CPU 与指令集的每种组合都有其自己的应用二进制界面(或 ABI)。 ABI 可以非常精确地定义应用的机器代码在运行时如何与系统交互。 您必须为应用要使用的每个 CPU 架构指定 ABI。

典型的 ABI 包含以下信息:

  • 机器代码应使用的 CPU 指令集。
  • 运行时内存存储和加载的字节顺序。
  • 可执行二进制文件(例如程序和共享库)的格式,以及它们支持的内容类型。
  • 用于解析内容与系统之间数据的各种约定。这些约定包括对齐限制,以及系统如何使用堆栈和在调用函数时注册。
  • 运行时可用于机器代码的函数符号列表 - 通常来自非常具体的库集。

 

二、如何在 gardle 中配置

默认情况下,cmake 会输出 4 种 ABI("armeabi-v7a" , "arm64-v8a", "x86", "x86_64"),如下所示:

我们也可以通过 abiFilters 来指定我们需要的 ABI:

abiFilters "armeabi", "armeabi-v7a" , "arm64-v8a", "x86", "x86_64", "mips", "mips64"

 

三、支持的 ABI 详解

ABI支持的指令集说明
armeabi
  • ARMV5TE 和更高版本
  • Thumb-1
无硬浮点。
armeabi-v7a
  • armeabi
  • Thumb-2
  • VFPv3-D16
  • 其他(可选)
与 ARMv5、v6 设备不兼容。
arm64-v8a
  • AArch-64
 
x86
  • x86 (IA-32)
  • MMX
  • SSE/2/3
  • SSSE3
不支持 MOVBE 或 SSE4。
x86_64
  • x86-64
  • MMX
  • SSE/2/3
  • SSSE3
  • SSE4.1、4.2
  • POPCNT
 
mips
  • MIPS32r1 及更高版本
使用硬浮点,并且假设 CPU:FPU 时钟比率为 2:1 以获取最大兼容性。 不提供 micromips 或 MIPS16。
mips64
  • MIPS64r6
 

参考:https://developer.android.com/ndk/guides/abis

其它:

​NDK 学习系列:Android NDK 从入门到精通(汇总篇)

Android Studio配置NDK的步骤如下: 1.下载NDK:打开Android Studio,依次点击File -> Project Structure -> SDK Location,在SDK Location窗口中找到Android NDK location,点击Download按钮下载NDK。 2.配置NDK路径:下载完成后,回到SDK Location窗口,将下载好的NDK路径复制到Android NDK location中。 3.配置build.gradle文件:在app目录下的build.gradle文件中添加以下代码: ```gradle android { ... defaultConfig { ... externalNativeBuild { cmake { cppFlags "" abiFilters "" } } } ... externalNativeBuild { cmake { path "CMakeLists.txt" } } } ``` 4.配置CMakeLists.txt文件:在项目根目录下创建CMakeLists.txt文件,并添加以下代码: ```cmake cmake_minimum_required(VERSION 3.4.1) 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 ) # Searches for a specified prebuilt library and stores the path as a # variable. Because system libraries are included 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 the # build script, prebuilt third-party libraries, or system libraries. target_link_libraries( # Specifies the target library. native-lib # Links the target library to the log library # included in the NDK. ${log-lib} ) ``` 5.编写C++代码:在app/src/main/cpp目录下创建native-lib.cpp文件,并添加以下代码: ```c++ #include <jni.h> #include <string> extern "C" JNIEXPORT jstring JNICALL Java_com_example_ndkdemo_MainActivity_stringFromJNI( JNIEnv* env, jobject /* this */) { std::string hello = "Hello from C++"; return env->NewStringUTF(hello.c_str()); } ``` 6.运行程序:点击Android Studio中的Run按钮,即可在模拟器或真机上运行程序。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值