美文网首页
[原创]cmake和Android.mk两种编译c/c++双编译

[原创]cmake和Android.mk两种编译c/c++双编译

作者: 吉凶以情迁 | 来源:发表于2021-09-04 16:32 被阅读0次

    Android.mk

    在gradle 安卓项目的build.gradle文件下的android{} 内插入

     externalNativeBuild {
    
        ndkBuild {
                        path 'src/main/jni/Android.mk'
                    }
    
    

    在gradle 安卓项目的build.gradle文件下的android.defaultConfig{} 内插入

    android{
     defaultConfig {
       ndkBuild {
                        abiFilters "armeabi-v7a",'arm64-v8a','x86_64'
                     abiFilters "armeabi-v7a",'arm64-v8a'
                    }
    
          }
     }
    
    

    Cmake

    在gradle 安卓项目的build.gradle文件下的android{} 内插入

     externalNativeBuild {
    
      cmake {
                    path "CMakeLists.txt"
                }
    
    

    在gradle 安卓项目的build.gradle文件下的android.defaultConfig{} 内插入

    android{
     defaultConfig {
                    cmake {
    //            arguments '-DBUILD_TESTING=OFF', '-DANDROID_TOOLCHAIN=gcc'
                        arguments "-DANDROID_STL=c++_shared", "-DSHARED=false", "-DDEBUG=OFF", "-DFUCKDEBUG=ON", "-Wno-error=format-security"
    //打开hook zz日志。
    //解决roid.injected.build.abi'gradle标志设置的ABIs [armeabi-v7a,armeabi]包含的'ARMEABI'不是此项目的目标
                        cppFlags "-fexceptions", "-fpermissive", "-fno-exceptions"
                       
          }
     }
    }
    

    Android.mk内容概览

    
    #APP_ABI := armeabi-v7a
    APP_ABI := armeabi armeabi-v7a x86
    #APP_ABI := armeabi-v7a x86
    #APP_ABI := armeabi-v7a x86 arm64-v8a
    APP_PLATFORM := android-14
    APP_STL := c++_static
    APP_OPTIM := release
    VA_ROOT          := $(call my-dir)
    NDK_MODULE_PATH  := $(NDK_MODULE_PATH):$(VA_ROOT)
    
    

    Android.mk

    LOCAL_PATH := $(call my-dir)
    MAIN_LOCAL_PATH := $(call my-dir)
    include $(CLEAR_VARS)
    LOCAL_MODULE := prncon
    # -DFUCKDEBUG open log
    LOCAL_CFLAGS := -Wno-error=format-security -fpermissive -DLOG_TAG=\"FUCK_QSSQ\" -DQSSQ_SAFE
    LOCAL_CFLAGS += -fno-rtti -fno-exceptions
    LOCAL_CPPFLAGS += -std=c++14
    LOCAL_CFLAGS += -fvisibility=hidden # hidden
    LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)
    LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)/Foundation
    LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)/Jni
    LOCAL_C_INCLUDES += $(MAIN_LOCAL_PATH)/Include
    
    LOCAL_SRC_FILES := Jni/VAJni.cpp \
                        My/JniHelper.cpp \
                        My/QSSQ_hkkq.cpp \
                        My/base64.c \
                        My/base6_with_test.c \
                        My/bytesencrypt.cpp \
    
    LOCAL_LDLIBS := -llog -latomic
    LOCAL_STATIC_LIBRARIES := fb
    
    include $(BUILD_SHARED_LIBRARY)
    include $(MAIN_LOCAL_PATH)/fb/Android.mk
    

    CmakeLists.txt文件概览

    
    cmake_minimum_required(VERSION 3.4.1)
    set(CMAKE_CXX_STANDARD 14)
    
    #include_directories(src/main/cpp/Foundation)
    include_directories(src/main/cpp/include)
    include_directories(src/main/cpp/include_io_redirect)
    include_directories(src/main/cpp)
    add_subdirectory("src/main/cpp/hookzz")
    
    
    #[[message("compile  static lib ${PROJECT_SOURCE_DIR}")
    add_library(hookzzstatic STATIC  IMPORTED)
    set_target_properties(hookzzstatic
            PROPERTIES IMPORTED_LOCATION
            ${PROJECT_SOURCE_DIR}/libhookzz.a)]]
    message("------------------------------------------------")
    
    add_library( # Sets the name of the library.
            qssqredirect
            # Sets the library as a shared library.
            SHARED
            src/main/cpp/android/Foundation/JniHelper.cpp
            src/main/cpp/QSSQ_hkkq.cpp
            src/main/cpp/base64.c
            src/main/cpp/bytesencrypt.cpp
            )
    
    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)
    
    
    
    target_link_libraries( # Specifies the target library.
            qssqredirect
            hookzz
            # Links the target library to the log library
            # included in the NDK.
            ${log-lib}
    #        ${atomic-lib}
            )
    
    
    #add_definitions(-std=c++11)
    ENABLE_LANGUAGE(ASM)
    
    if (FUCKDEBUG)
        message("---------open log FUCKDEBUG---------------")
        add_definitions(-DFUCKDEBUG)
    endif ()
    

    双支持

    在项目build.gradle 下定义

    ext{
        USE_CMAKE=false
    }
    

    在android项目build.gradle定义判断即可


    image.png image.png

    gradle中 宏定义的读取

    gradle.中

                    cmake {
                        arguments "-DFUCKDEBUG=ON", "-Wno-error=format-security"
    }
    

    cmakeList.txt

    
    if (FUCKDEBUG)
        message("---------open log FUCKDEBUG---------------")
        add_definitions(-DFUCKDEBUG)
    

    android.mk

    # -DFUCKDEBUG open log
    

    c代码

       #if defined(FUCKDEBUG)
            #pragma message("-----MY LOG_ ON----")
    #endif
    

    判断cpu架构的代码

    __arm__             armeabi
    __arm__             armeabi-v7
    __aarch64__         arm64-v8a
    __i386__            x86
    __x86_64__          x86_64
    
    # ifdef __arm__ 
            #pragma message(" arm arch")
    #endif
    

    相关文章

      网友评论

          本文标题:[原创]cmake和Android.mk两种编译c/c++双编译

          本文链接:https://www.haomeiwen.com/subject/bumrwltx.html