本文主要是介绍[原创]cmake和Android.mk两种编译c/c++双编译的样板代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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)/IncludeLOCAL_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 := fbinclude $(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(hookzzstaticPROPERTIES IMPORTED_LOCATION${PROJECT_SOURCE_DIR}/libhookzz.a)]]
message("------------------------------------------------")add_library( # Sets the name of the library.qssqredirect# Sets the library as a shared library.SHAREDsrc/main/cpp/android/Foundation/JniHelper.cppsrc/main/cpp/QSSQ_hkkq.cppsrc/main/cpp/base64.csrc/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.qssqredirecthookzz# 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定义判断即可
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++双编译的样板代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!