반응형
Android에서 c++에서 Java 메서드 호출
자바가 네이티브 메서드를 호출하는 동안 C++에서 자바 메서드를 호출하려고 합니다.Java 코드는 다음과 같습니다.
public class MainActivity extends Activity {
private static String LIB_NAME = "name";
static {
System.loadLibrary(LIB_NAME);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textview);
tv.setText(this.getJniString());
}
public void messageMe(String text) {
System.out.println(text);
}
public native String getJniString();
}
전화하려고 합니다messageMe
처리 중인 네이티브 코드로부터의 메서드getJniString*
메서드 호출을 Java에서 네이티브로 합니다.
native.cpp:
#include <string.h>
#include <stdio.h>
#include <jni.h>
jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj, jint depth ){
// JavaVM *vm;
// JNIEnv *env;
// JavaVMInitArgs vm_args;
// vm_args.version = JNI_VERSION_1_2;
// vm_args.nOptions = 0;
// vm_args.ignoreUnrecognized = 1;
//
// // Construct a VM
// jint res = JNI_CreateJavaVM(&vm, (void **)&env, &vm_args);
// Construct a String
jstring jstr = env->NewStringUTF("This string comes from JNI");
// First get the class that contains the method you need to call
jclass clazz = env->FindClass("the/package/MainActivity");
// Get the method that you want to call
jmethodID messageMe = env->GetMethodID(clazz, "messageMe", "(Ljava/lang/String;)V");
// Call the method on the object
jobject result = env->CallObjectMethod(jstr, messageMe);
// Get a C-style string
const char* str = env->GetStringUTFChars((jstring) result, NULL);
printf("%s\n", str);
// Clean up
env->ReleaseStringUTFChars(jstr, str);
// // Shutdown the VM.
// vm->DestroyJavaVM();
return env->NewStringUTF("Hello from JNI!");
}
클린 컴파일 앱이 정지하면 다음 메시지가 나타납니다.
ERROR/AndroidRuntime(742): FATAL EXCEPTION: main
java.lang.NoSuchMethodError: messageMe
at *.android.t3d.MainActivity.getJniString(Native Method)
at *.android.t3d.MainActivity.onCreate(MainActivity.java:22)
그 메서드명이 틀렸다는 뜻인 것 같습니다만, 저는 괜찮은 것 같습니다.
오브젝트 메서드일 경우 오브젝트를 다음에 전달해야 합니다.CallObjectMethod
:
jobject result = env->CallObjectMethod(obj, messageMe, jstr);
당신이 하고 있는 일은 그 사람의 일과 맞먹는다.jstr.messageMe()
.
는 무효 메서드이므로 다음 연락처로 문의해 주십시오.
env->CallVoidMethod(obj, messageMe, jstr);
결과를 반환하려면 JNI 시그니처를 변경해야 합니다.()V
의 방법을 의미한다void
return type) 및 Java 코드의 return type을 입력합니다.
c-to-c++ 변환으로 완전히 실패하였습니다(기본적으로).env
(변수) 단, 다음 C++용 코드로 동작하도록 했습니다.
#include <string.h>
#include <stdio.h>
#include <jni.h>
jstring Java_the_package_MainActivity_getJniString( JNIEnv* env, jobject obj){
jstring jstr = (*env)->NewStringUTF(env, "This comes from jni.");
jclass clazz = (*env)->FindClass(env, "com/inceptix/android/t3d/MainActivity");
jmethodID messageMe = (*env)->GetMethodID(env, clazz, "messageMe", "(Ljava/lang/String;)Ljava/lang/String;");
jobject result = (*env)->CallObjectMethod(env, obj, messageMe, jstr);
const char* str = (*env)->GetStringUTFChars(env,(jstring) result, NULL); // should be released but what a heck, it's a tutorial :)
printf("%s\n", str);
return (*env)->NewStringUTF(env, str);
}
Java 메서드의 다음 코드:
public class MainActivity extends Activity {
private static String LIB_NAME = "thelib";
static {
System.loadLibrary(LIB_NAME);
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.textview);
tv.setText(this.getJniString());
}
// please, let me live even though I used this dark programming technique
public String messageMe(String text) {
System.out.println(text);
return text;
}
public native String getJniString();
}
언급URL : https://stackoverflow.com/questions/5198105/calling-a-java-method-from-c-in-android
반응형
'source' 카테고리의 다른 글
python/numpy를 사용하여 백분위수를 계산하는 방법은 무엇입니까? (0) | 2022.09.04 |
---|---|
EC2-classic의 소스 끝점과 관련된 AWS DMS 문제 (0) | 2022.09.04 |
java.sql에서 열 이름을 검색합니다.결과 세트 (0) | 2022.09.04 |
Java 객체(빈)를 키와 값의 쌍으로 변환하는 방법(또는 그 반대) (0) | 2022.09.04 |
Gson을 사용하여 JSON을 HashMap으로 변환하려면 어떻게 해야 합니까? (0) | 2022.09.04 |