Giter Site home page Giter Site logo

ndk_json_androidstudio's Introduction

Use JsonCPP with NDK in Android Studio

Create class & setup NDK build to get String from native

  • Create class NativeClass with function getStringFromNative:
public class NativeClass {
    public native static String getStringFromNative();
}
  • Build project to generate classes

  • Open terminal. Change to app/src/main. Then run

javah -d jni -classpath ../../build/intermediates/classes/debug/ com.example.ndk_json_androidstudio.NativeClass
  • Create "com_example_ndk_json_androidstudio_NativeClass.cpp" in c folder (jni folder).
#include 

JNIEXPORT jstring JNICALL Java_com_example_ndk_1json_1androidstudio_NativeClass_getStringFromNative
  (JNIEnv * env, jobject obj){
    return env->NewStringUTF("Hello from JNI");
}
  • Add id to TextView
android:id="@+id/testTextView"
  • Use getStringFromNative() to change testTextView text
    TextView tv = (TextView) findViewById(R.id.testTextView);
    tv.setText(NativeClass.getStringFromNative());
  • Open build.gradle. Add this before buildTypes
    // begin
    sourceSets.main {
        jni.srcDirs = [] //disable automatic ndk-build call
    }
    // replace path to NDK_BUILD
    task ndkBuild(type: Exec, description: 'Compile JNI source via NDK') {
        commandLine "/home/robotbase/Android/NDK64/android-ndk-r10d/ndk-build",
                'NDK_PROJECT_PATH=build/intermediates/ndk',
                'NDK_LIBS_OUT=src/main/jniLibs',
                'APP_BUILD_SCRIPT=src/main/jni/Android.mk',
                'NDK_APPLICATION_MK=src/main/jni/Application.mk'
    }
    tasks.withType(JavaCompile) {
        compileTask -> compileTask.dependsOn ndkBuild
    }
    //end
  • Android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := com_example_ndk_json_androidstudio_NativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLib

include $(BUILD_SHARED_LIBRARY)
  • Application.mk. Note: List of available toolchain is in android-ndk-r10d/build/core/setup-toolchain.mk. You can change 4.9 to another if it doesn't work.
APP_STL := gnustl_shared

APP_CPPFLAGS += -frtti
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -std=c++11

APP_ABI := armeabi-v7a
NDK_TOOLCHAIN_VERSION := 4.9
  • MainActivity.class
static {
    System.loadLibrary("MyLib");
}

Setup Jsoncpp

  • Clone jsoncpp project
	git clone https://github.com/open-source-parsers/jsoncpp
  • Create json.cpp file. Run in jsoncpp folder
	python amalgamate.py
  • Copy content of folder "dist" to "jni" folder
json/json.h
json/json-forwards.h
jsoncpp.cpp
  • Open Android.mk and edit as follow
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpp
LOCAL_CPPFLAGS := -fexceptions
LOCAL_SRC_FILES := jsoncpp.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/json
LOCAL_LDLIBS := -L$(call host-path, $(LOCAL_PATH)/../../libs/armeabi)
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_example_ndk_json_androidstudio_NativeClass.cpp
LOCAL_LDLIBS += -llog
LOCAL_MODULE := MyLib

# include jsoncpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/json
LOCAL_SHARED_LIBRARIES := jsoncpp

include $(BUILD_SHARED_LIBRARY)
  • Edit com_example_ndk_json_androidstudio_NativeClass.cpp
#include "com_example_ndk_json_androidstudio_NativeClass.h"
#include "json/json.h"

JNIEXPORT jstring JNICALL Java_com_example_ndk_1json_1androidstudio_NativeClass_getStringFromNative
  (JNIEnv * env, jobject obj){
//    return env->NewStringUTF("Hello from JNI");

    Json::Value jsonObject;
    jsonObject["data1"] = "this is a json object";
    jsonObject["data2"] = 8888;
    jsonObject["data3"] = 9999;


    const char* json_str = jsonObject.toStyledString().c_str();
    jstring result = env->NewStringUTF(json_str);

    return result;
}

Troubleshooting

  • Crash on "Json:Reader jsonReader". Make sure Application.mk has :
APP_STL := gnustl_shared
NDK_TOOLCHAIN_VERSION := 4.9

Reference

ndk_json_androidstudio's People

Contributors

quanhua92 avatar

Watchers

James Cloos avatar C++ avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.