ndk编译jsoncpp
2020-12-13 05:36
标签:des android style blog class c 本例采用jsoncpp-src-0.6.0-rc2-amalgamation.tar java调用语句 int id = 1001;
单写一个类来管理native函数如下 package com.mjson; public class system { static { System.loadLibrary("jsoncpp");
System.loadLibrary("system"); } public static native String toBuildJson(int id,String name); } 配置好ndk和cywin(详见http://blog.csdn.net/pengchua/article/details/7582949 在cywin控制窗口cd到自己的项目下,如 /cygdrive/d/jdk/work/mJson/ javah -classpath bin/classes -d jni com.mJson.system 生成.h接口文件 /* DO NOT EDIT THIS FILE - it is machine generated */ #include #include #include "json/json.h" #define LOG_TAG "system" #define LOGD(...)
__android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__) /* Header for class com_mjson_system */ #ifndef _Included_com_mjson_system #define _Included_com_mjson_system #ifdef __cplusplus #endif /* * Class: com_mjson_system *
Method: toBuildJson * Signature:
(ILjava/lang/String;)Ljava/lang/String; */ JNIEXPORT jstring JNICALL Java_com_mjson_system_toBuildJson (JNIEnv *,
jclass, jint, jstring); #ifdef __cplusplus #endif #endif 本来对于JNIEXPORT jstring JNICALL Java_com_...这个函数需要entends
“c”{}包含,用于标识是c实现,便于编译成动态库,但是 由于我将函数的实现挪到了system.cpp中实现,所以此处去掉,将此加到system.cpp中,不然编译时会报冲突错误 systm.cpp如下 #include "com_mjson_system.h" extern "C" { JNIEXPORT jstring JNICALL Java_com_mjson_system_toBuildJson (JNIEnv* env, jobject thiz,jint id,jstring name){ jboolean isCopy = 0; const char* c_name = env->GetStringUTFChars(name, &isCopy); LOGD("on calling,id:%d,name:%s",id,c_name); // to build a json object with id and name Json::Value user; user["id"] = id; user["name"] = c_name; const char* json_str = user.toStyledString().c_str(); jstring result = env->NewStringUTF(json_str); env->ReleaseStringUTFChars(name,c_name); return result; } } 在jni下新建Application # it is needed for ndk-r5 在jni下新建Android.mk文件 在Makefile时注意路径 1、JsonCpp用到了stl的exception,所以如果你在android的编译系统的Application文件中指定STL库路径时,如果使用: APP_STL
:= stlport_static,那么就无法通过编译。需要改成:APP_STL := gnustl_static 2、jsoncpp的make
file。注意LOCAL_CPPFLAGS := -DJSON_IS_AMALGAMATION -fexceptions
这一行。宏定义JSON_IS_AMALGAMATION告诉jsoncpp是amalgamation版本,即是我们刚才下载的版本。-fexceptions则开启exception应用。 3、我们的测试程序的make
file。注意LOCAL_C_INCLUDES :=
$(LOCAL_PATH)/soncpp这一行,我们指定了头文件的地址为,当前路径(即$project/jni/test/)的上一级的jsoncpp文件夹,即$project/jni/jsoncpp/,这样在使用中我们需要inlcude的就是
"json/json.h"。 这里则指定使用的库libjsoncpp和liblog 4、java端的接口,注意加载库的先后顺序: LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := jsoncpp LOCAL_CPPFLAGS := -DJSON_IS_AMALGAMATION -fexceptions LOCAL_SRC_FILES := $(LOCAL_PATH)/jsoncpp/jsoncpp.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsoncpp/json # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn‘t support Cygwin‘s drive # mapping (i.e /cygdrive/c/ instead of C:/) #LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../libs/armeabi) include $(BUILD_SHARED_LIBRARY) include $(CLEAR_VARS) LOCAL_MODULE := system # for jsoncpp LOCAL_CPPFLAGS := -DJSON_IS_AMALGAMATION LOCAL_SRC_FILES := system.cpp LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsoncpp # it is used for ndk-r5 # if you build with ndk-r4, comment it # because the new Windows toolchain doesn‘t support Cygwin‘s drive # mapping (i.e /cygdrive/c/ instead of C:/) LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../libs/armeabi)
\
-ljsoncpp -llog include $(BUILD_SHARED_LIBRARY) ndk编译jsoncpp,搜素材,soscw.com ndk编译jsoncpp 标签:des android style blog class c 原文地址:http://www.cnblogs.com/quanhy/p/3737597.html
String name = "Kevin";
String result =
system.toBuildJson(id, name);
Log.i(TAG,String.format("Id:%1$d,Name:%2$s,The json formated string:%3$s",
id,name,result));
http://my.oschina.net/lifj/blog/177087)
#APP_STL := stlport_static
APP_STL :=
gnustl_static