Created
July 10, 2018 09:12
-
-
Save jan4984/477d8b5ab91169e2dd85154af3e63a52 to your computer and use it in GitHub Desktop.
my jni helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <jni.h> | |
#include <string> | |
#define _MACRO_CONCAT(a,b) a ## b | |
#define _MACRO_2_STR(a) #a | |
#define _TOSTR(a) _MACRO_2_STR(a) | |
#define JMETHOD_VAR(n) _MACRO_CONCAT(jmethodId, n) | |
#define JMETHOD_DECLARE(cn,n,s) static jmethodID JMETHOD_VAR(n); | |
#define JMETHOD_GET(cn, n,s) JMETHOD_VAR(n) = jEnv->GetMethodID(_MACRO_CONCAT(jclass, cn), #n, s); | |
#define JCLASS_VAR_STATMENT(n) _MACRO_CONCAT(jclass, n) | |
#define JCLASS_VAR JCLASS_VAR_STATMENT(JCLASS_SIMPLENAME) | |
#define JCLASS_GET JCLASS_VAR = jEnv->FindClass(_TOSTR(JCLASS_FULLNAME)); | |
#define JCLASS_DECLARE static jclass JCLASS_VAR; | |
/* example */ | |
#define JCLASS_FULLNAME cn.iflyos.sdk.MediaPlayer | |
#define JCLASS_SIMPLENAME MediaPlayer | |
#define JMETHODS \ | |
JMETHOD(JCLASS_SIMPLENAME, setStringSource, "(J)Ljava.lang.String;") \ | |
JMETHOD(JCLASS_SIMPLENAME, setStreamSource, "(J)Lcn.iflyos.sdk.NativeStream;") | |
JCLASS_DECLARE | |
#define JMETHOD JMETHOD_DECLARE | |
JMETHODS | |
#undef JMETHOD | |
extern "C" JNIEXPORT jstring JNICALL | |
Java_cn_iflyos_open_myapplication_MainActivity_stringFromJNI( | |
JNIEnv *jEnv, | |
jobject /* this */) { | |
std::string hello = "Hello from C++"; | |
JCLASS_GET | |
#define JMETHOD JMETHOD_GET | |
JMETHODS | |
#undef JMETHOD | |
jEnv->CallLongMethod(JCLASS_VAR, JMETHOD_VAR(setStringSource), nullptr); | |
return jEnv->NewStringUTF(hello.c_str()); | |
} | |
#undef JMETHODS | |
#undef JCLASS_FULLNAME | |
#undef JCLASS_SIMPLENAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment