NDK compilation Jsoncpp

Source: Internet
Author: User

This example uses Jsoncpp-src-0.6.0-rc2-amalgamation.tar

Java Call statement

int id = 1001;
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));

Write a class to manage the native function as follows

Package Com.mjson;

public class System {

static {

System.loadlibrary ("Jsoncpp");

System.loadlibrary ("system");

}

public static native String Tobuildjson (int id,string name);

}

Configure the NDK and Cywin (see http://blog.csdn.net/pengchua/article/details/7582949.
http://my.oschina.net/lifj/blog/177087)

In the Cywin Control window CD to your own project, such as/cygdrive/d/jdk/work/mjson/

Javah-classpath bin/classes-d JNI Com.mJson.system

Generate the. h interface File

/* Don't EDIT this file-it are machine generated */

#include <jni.h>

#include <android/log.h>

#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

Originally for Jniexport jstring jnicall java_com_ ... This function requires the Entends "C" {} to be included, which is used to identify the C implementation, which is easy to compile into a dynamic library, but

Since I moved the implementation of the function into the system.cpp, I removed it and added it to the system.cpp, otherwise it would have been reported as a conflict error at compile time.

Systm.cpp as follows

#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;

}

}

Create a new application under JNI

# It's needed for NDK-R5
#APP_STL: = stlport_static
App_stl: = gnustl_static

Create a new android.mk file under JNI

Note the path when makefile

1, Jsoncpp used to the STL exception, so if you specify the STL library path in the application file of Android's compiling system, if you use: app_stl: = stlport_static, you cannot compile. Need to change to: app_stl: = gnustl_static

2, Jsoncpp. Note local_cppflags: =-djson_is_amalgamation-fexceptions this line. The macro definition Json_is_amalgamation tells Jsoncpp is the amalgamation version, which is the version we just downloaded. -fexceptions opens the exception application.

3. Make file for our test program. Note local_c_includes: = $ (local_path)/soncpp This line, we specify that the header file address is, the current path (that is, $project/jni/test/) on the previous level of the Jsoncpp folder, that is, $project/ jni/jsoncpp/, so in use we need to Inlcude is "Json/json.h".

  1. LOCAL_ ldlibs := -l$ (call host-path, $ (Local_path)/. /.. /libs/armeabi)  \  
  2.                  -ljsoncpp -llog   

This specifies the libraries Libjsoncpp and Liblog that are used

4, Java interface, pay attention to loading the library sequence:

[Java] View Plaincopy < Param name= "loop" value= "loop" >
  1. System.loadLibra Ry ( " Jsoncpp "
  2. System.loadlibrary ("main");

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's 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's 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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.