天天看點

Android NDK添加NEON以及cpufeatures支援

本人使用Android studio3.0進行NDK開發,由于Android develop官網文檔是針對2.2版本以下,這裡為2.2以上版本的cmakelist配置做以下紀錄:

一、添加NEON支援:

在build.gradle(app)中添加:

externalNativeBuild {
    cmake {
        cppFlags ""
        arguments "-DLOCAL_ARM_NEON=true" //NEON支援
    }
}      

二、添加cpufeatures支援:

1.添加cpufeatures靜态庫:

include_directories(${ANDROID_NDK}/sources/android/cpufeatures)
add_library(cpufeatures STATIC
        ${ANDROID_NDK}/sources/android/cpufeatures/cpu-features.c)      

2.配置參數:

if (${ANDROID_ABI} STREQUAL "armeabi-v7a")
        # make a list of neon files and add neon compiling flags to them
        set(neon_SRCS src/main/cpp/native-lib.cpp)

        set_property(SOURCE ${neon_SRCS}
                APPEND_STRING PROPERTY COMPILE_FLAGS " -mfpu=neon")
        add_definitions("-DHAVE_NEON=1")
        #add_definitions("-DLOCAL_ARM_NEON=1")
elseif (${ANDROID_ABI} STREQUAL "x86")
        set(neon_SRCS src/main/cpp/native-lib.cpp)
        set_property(SOURCE ${neon_SRCS} APPEND_STRING PROPERTY COMPILE_FLAGS
                " -mssse3  -Wno-unknown-attributes \
                   -Wno-deprecated-declarations \
                   -Wno-constant-conversion \
                   -Wno-static-in-inline")
        add_definitions(-DHAVE_NEON_X86=1 -DHAVE_NEON=1)
        #add_definitions("-DLOCAL_ARM_NEON=1")
else ()
        set(neon_SRCS)
endif ()      

3.添加到link:

target_link_libraries( # Specifies the target library.
        native-lib
        cpufeatures

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


target_include_directories(native-lib PRIVATE
        ${ANDROID_NDK}/sources/android/cpufeatures)