1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
#include <jni.h> #include <android/log.h> #include <string>
extern "C" { JNIEXPORT jstring JNICALL Java_com_xiasuhuei321_forjni_entry_JNIEntry_stringFromJNI(JNIEnv *env, jobject instance) { __android_log_print(ANDROID_LOG_DEBUG, "jni", "Hello World"); return env->NewStringUTF("Hello World"); }
JNIEXPORT jstring JNICALL Java_com_xiasuhuei321_forjni_entry_JNIEntry_toLowCase(JNIEnv *env, jclass type, jstring str_) { const char *str = env->GetStringUTFChars(str_, JNI_FALSE); jint length = env->GetStringLength(str_); char strs[length];
strcpy(strs, str); for (int i = 0; i < length; i++) { char c = str[i]; if (c >= 'A' && c <= 'Z') { strs[i] = (char) (c + 32); } else { strs[i] = c; } }
__android_log_print(ANDROID_LOG_DEBUG, "jni", "%s", strs); env->ReleaseStringUTFChars(str_, str);
return env->NewStringUTF(strs); }
JNIEXPORT void JNICALL Java_com_xiasuhuei321_forjni_entry_JNIEntry_changeArray(JNIEnv *env, jclass type, jintArray array_) {
jint *value = env->GetIntArrayElements(array_, NULL); jint length = env->GetArrayLength(array_); for (int i = 0; i < length; i++) { value[i] += 1; } env->ReleaseIntArrayElements(array_, value, 0); }
JNIEXPORT void JNICALL Java_com_xiasuhuei321_forjni_entry_JNIEntry_accessField(JNIEnv *env, jclass type, jobject instance) { jclass clazz = env->GetObjectClass(instance); jfieldID instanceFieldId = env->GetFieldID(clazz, "TAG1", "Ljava/lang/String;"); jstring jstr_value = (jstring) env->GetObjectField(instance, instanceFieldId); const char *ch_value = env->GetStringUTFChars(jstr_value, NULL);
__android_log_print(ANDROID_LOG_DEBUG, "jni", "%s", ch_value);
jfieldID staticFieldId = env->GetStaticFieldID(clazz, "TAG2", "Ljava/lang/String;"); jstring static_value = (jstring) env->GetStaticObjectField(clazz, staticFieldId); const char *st_value = env->GetStringUTFChars(static_value, NULL); __android_log_print(ANDROID_LOG_DEBUG, "jni", "%s", st_value);
}
JNIEXPORT void JNICALL Java_com_xiasuhuei321_forjni_entry_JNIEntry_accessMethod(JNIEnv *env, jclass type, jobject obj) { jclass clazz = env->GetObjectClass(obj); jmethodID i_id = env->GetMethodID(clazz, "testInstanceMethod", "()V"); jmethodID s_id = env->GetStaticMethodID(clazz, "testStaticMethod", "()V");
env->CallVoidMethod(obj, i_id); env->CallStaticVoidMethod(clazz,s_id); } }
|