NDK开发模块的时候,如果涉及到网络请求,类似json数据传递的时候,有现成的第三方json库可以移植,后台C++开发中使用的比较多的是jsoncpp,今天记录一下jsoncpp移植到Android平台的过程
cmake编译
此方法并非标准的NDK项目
采用的是cmake交叉编译生成
jsoncpp地址
https://github.com/open-source-parsers/jsoncpp
先将源码克隆下来
新建文件夹libjson
将jsoncpp源码中的include文件夹复制到该目录,然后进入src/lib_json目录复制
部分文件到libjson目录下的src目录中,如下
作者:Andy周
链接:https://www.jianshu.com/p/02ccd73c927e
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
json_reader.cpp json_tool.h json_value.cpp json_valueiterator.inl json_writer.cpp version.h.in
在libjson目录新建文件CMakeLists.txt
内容如下
1 CMAKE_MINIMUM_REQUIRED(VERSION 3.5) 2 PROJECT(Jpp) 3 4 SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 5 6 7 #ARCHS集合 8 SET(ARCHS "arm" "arm64" "x86" "x86_64" "mips" "mips64") 9 10 #PLATFORM集合 11 SET(PLATFORM "arm-linux-androideabi" "aarch64-linux-android" "i686-linux-android" "x86_64-linux-android" "mipsel-linux-android" "mips64el-linux-android") 12 13 14 SET(JSONCPP_INCLUDE_DIR include) 15 16 INCLUDE_DIRECTORIES(${JSONCPP_INCLUDE_DIR}) 17 18 SET(PUBLIC_HEADERS 19 ${JSONCPP_INCLUDE_DIR}/json/config.h 20 ${JSONCPP_INCLUDE_DIR}/json/forwards.h 21 ${JSONCPP_INCLUDE_DIR}/json/features.h 22 ${JSONCPP_INCLUDE_DIR}/json/value.h 23 ${JSONCPP_INCLUDE_DIR}/json/reader.h 24 ${JSONCPP_INCLUDE_DIR}/json/writer.h 25 ${JSONCPP_INCLUDE_DIR}/json/assertions.h 26 ${JSONCPP_INCLUDE_DIR}/json/version.h 27 ) 28 29 SOURCE_GROUP("Public API" FILES ${PUBLIC_HEADERS}) 30 31 SET(jsoncpp_sources 32 src/json_tool.h 33 src/json_reader.cpp 34 src/json_valueiterator.inl 35 src/json_value.cpp 36 src/json_writer.cpp 37 src/version.h.in) 38 39 40 #INDEX=0 "arm" 41 #INDEX=1 "arm64" 42 #INDEX=2 "x86" 43 #INDEX=3 "x86_64" 44 #INDEX=4 "mips" 45 #INDEX=5 "mips64" 46 47 SET(INDEX 0) 48 49 list(GET ARCHS ${INDEX} TARGET_ARCH) 50 list(GET PLATFORM ${INDEX} TARGET_PLATFORM) 51 52 MESSAGE("ARCH= " ${TARGET_ARCH}) 53 MESSAGE("PLATFORM= " ${TARGET_PLATFORM}) 54 55 SET(CMAKE_C_COMPILER "$ENV{HOME}/android-toolchain/${TARGET_ARCH}/bin/${TARGET_PLATFORM}-gcc") 56 SET(CMAKE_CXX_COMPILER "$ENV{HOME}/android-toolchain/${TARGET_ARCH}/bin/${TARGET_PLATFORM}-g++") 57 58 59 SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin) 60 SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib) 61 62 ADD_LIBRARY(${TARGET_ARCH}_share SHARED ${PUBLIC_HEADERS} ${jsoncpp_sources}) 63 SET_TARGET_PROPERTIES(${TARGET_ARCH}_share PROPERTIES OUTPUT_NAME ${TARGET_ARCH}) 64 ADD_LIBRARY(${TARGET_ARCH}_static STATIC ${PUBLIC_HEADERS} ${jsoncpp_sources}) 65 SET_TARGET_PROPERTIES(${TARGET_ARCH}_static PROPERTIES OUTPUT_NAME ${TARGET_ARCH})
编译之前确认已安装cmake
如果没有,请先安装
sudo apt-get install cmake
目录结构如下
交叉编译请,参考笔者之前记录的《NDK交叉编译之自定义工具链》
准备工作做好之后可以进行编译了
cmake .
make
笔者目前只编译了arm平台的,如果有需要其他平台的,只需要改动上述CMakeLists.txt中的编号即可
SET(INDEX 0)
NDK原生编译
测试
下面简单测试一下,就不建Android项目了
新建文件夹JNI
将刚才生成的libarm.a和include文件夹放入进去,新建文件Android.mk
1 LOCAL_PATH := $(call my-dir) 2 3 include $(CLEAR_VARS) 4 5 LOCAL_MODULE := jsoncpp 6 LOCAL_SRC_FILES := libarm.a 7 include $(PREBUILT_STATIC_LIBRARY) 8 9 include $(CLEAR_VARS) 10 LOCAL_MODULE := main 11 LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/json 12 LOCAL_SRC_FILES := main.cpp 13 LOCAL_CPPFLAGS := -fexceptions 14 15 LOCAL_STATIC_LIBRARIES := jsoncpp 16 17 LOCAL_CFLAGS += -pie -fPIE 18 LOCAL_LDFLAGS += -pie -fPIE 19 20 LOCAL_LDLIBS := -llog 21 22 include $(BUILD_EXECUTABLE)
新建Application.mk文件
1 APP_ABI := armeabi 2 APP_PLATFORM := 12 3 APP_STL := gnustl_static 4 APP_CPPFLAGS := -fexceptions -frtti 5 APP_CPPFLAGS += -std=gnu++11 6 APP_CPPFLAGS += -fpermissive
新建main.cpp
1 #include <jni.h> 2 #include <iostream> 3 #include "include/json/json.h" 4 5 using namespace std; 6 7 void JsonTest(); 8 9 int main(){ 10 JsonTest(); 11 return 0; 12 } 13 14 void JsonTest(){ 15 Json::Value root; 16 Json::Value array; 17 Json::Value item; 18 19 for (int i = 0; i < 2; i ++) 20 { 21 item["id"] = i; 22 item["name"] = "name"; 23 array.append(item); 24 } 25 26 root["username"] = "andy"; 27 root["age"] = 18; 28 root["array"] = array; 29 30 string out = root.toStyledString(); 31 cout << out << endl; 32 }
然后编译
ndk-build
然后将生成的main执行文件上传到手机中测试,手机需要root权限
笔者一般丢在/data/local目录下测试
./main