Android Studio的CMakeList.txt中针对不同ABI编译不同代码

一、前言

最近需要处理针对不同 ABI 版本(如:“arm64-v8a”, “armeabi-v7a”, “x86”, “x86_64”)编译不同代码的事情。在 Android Studio 的工程中折腾了挺久,在这做个简单的总结。

二、Eclipse 的 Android 工程针对不同ABI编译不同代码

如果 Android 工程由 Eclipse 创建,则需要在 Application.mk 和 Android.mk 中指定编译的代码即可。如下:

# Application.mk 文件
...
APP_ABI += x86_64
APP_ABI := armeabi
APP_ABI += arm64-v8a
...
# Android.mk 文件
...
LOCAL_SRC_FILES := test.cpp

ifeq ($(TARGET_ARCH_ABI), x86_64)
	LOCAL_SRC_FILES += test2.cpp
endif

ifeq ($(TARGET_ARCH_ABI), armeabi)
	LOCAL_SRC_FILES += test3.cpp 
endif

ifeq ($(TARGET_ARCH_ABI), arm64-v8a)
	LOCAL_SRC_FILES += test4.cpp
endif
...

然后在代码使用时,针对不同的 ABI 平台使用不同的代码,如下:

int g_nTest = 0;
#if defined(__arm__)
extern int add1(int m, int n);
g_nTest  = add1(2, 92);
#endif

#if defined(__i386__)
extern int add2(int m, int n);
g_nTest  = add2(2, 92);
#endif

#if defined(__aarch64__)
extern int add3(int m, int n);
g_nTest  = add3(2, 92);
#endif

三、Android Studio的CMakeList.txt中针对不同ABI编译不同代码

1、测试工程中 CMakeList.txt 代码:

cmake_minimum_required(VERSION 3.4.1)


# 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.
SET (SRC_LIST native-lib.cpp)
#abiFilters "armeabi", "armeabi-v7a" , "arm64-v8a", "x86", "x86_64", "mips", "mips64"
if(${ANDROID_ABI} MATCHES "arm64-v8a") #针对不同 ABI 设置不同的编译代码 或 if(${ANDROID_ABI} STREQUAL "areambi")
    SET (SRC_LIST ${SRC_LIST} test.cpp)
elseif(${ANDROID_ABI} MATCHES  "x86")
    SET (SRC_LIST ${SRC_LIST} test.cpp)
elseif(${ANDROID_ABI} MATCHES  "armeabi")
    SET (SRC_LIST ${SRC_LIST} test2.cpp)
elseif(${ANDROID_ABI} MATCHES  "armeabi-v7a")
    SET (SRC_LIST ${SRC_LIST} test2.cpp)
endif()

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_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_link_libraries( # Specifies the target library.
        native-lib

        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

1、SET (SRC_LIST native-lib.cpp) 中 SRC_LIST 有类似链表的功能,可通过 SET (SRC_LIST ${SRC_LIST} test2.cpp) 向其中加入内容;

2、${ANDROID_ABI} 是 ABI 的默认值,CMakeList.txt 中字符串 “=” 是用 MATCHES 表示;

3、add_library 的作用是添加一个动态或静态库,当有多个 add_library 时,可以在 编译好的apk中看到多个动态欧静态库;add_library 的库名称都需要加到 target_link_libraries 下以参与链接;

4、find_library 的作用是查找系统库,这些库的名称也需要加到 target_link_libraries 下以参与链接;

5、target_link_libraries 中记录所有需要链接的库。

2、cpp 中代码

#include <jni.h>
#include <string>
#include <android/log.h>
#define TAG "py_dc" // 这个是自定义的LOG的标识
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,TAG ,__VA_ARGS__) // 定义LOGD类型
extern int add(int m, int n); #该函数实现在 test1()test2() 中都有,但稍有不同

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_fortestcpp_MainActivity_stringFromJNI(
        JNIEnv *env,
        jobject /* this */) {
    std::string hello = "Hello from C++";
#if defined(__aarch64__)
    LOGD("add %d",add(2, 92));
#elif defined(__arm__)
    LOGD("add %d",add(2, 92));
#elif defined(__i386__)
    LOGD("add %d",add(2, 92));
#endif

    return env->NewStringUTF(hello.c_str());
}
// test.cpp 中实现
int add(int m, int n){
    return m + n;
}
// test2.cpp 中实现
int add(int m, int n){
    return m + n + 2;
}

3、apk中导出so查看对应函数

分别导出 arm64-v8a 和 armeabi-v7a 版本 so,用 IDA 打开,可看到不同的 add 实现,如下:
armeabi-v7a 版本的so
 arm64-v8a中对应so

四、小结

1、参考 CMakeLists.txt编写常用命令
测试工程demo:测试工程demo

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值