- 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"); }
- 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; }
- Crash on "Json:Reader jsonReader". Make sure Application.mk has :
APP_STL := gnustl_shared NDK_TOOLCHAIN_VERSION := 4.9