Skip to content

Instantly share code, notes, and snippets.

@Ssioo
Last active November 28, 2022 08:11
Show Gist options
  • Save Ssioo/060f3f849f3e5afccda486c17d013a94 to your computer and use it in GitHub Desktop.
Save Ssioo/060f3f849f3e5afccda486c17d013a94 to your computer and use it in GitHub Desktop.
public class MyNativeActivity extends AppCompatActivity {
static {
System.loadLibrary("ndk_example");
init();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
int res = test(1, 3);
Log.d("JNI Result", String.format("Res: %d"), res));
}
@Override
protected void onDestroy() {
terminate();
super.onDestroy();
}
public static native void init();
public static native void terminate();
public native int test(int a, int b);
}
#include <jni.h>
#include <sstream>
#include "assert.h"
#include "android/log.h"
static int c;
extern "C" {
JNIEXPORT void
Java_com_my_package_name_MyNativeActivity_init(JNIEnv* env, jclass thiz) {
// Static Method는 두번째 인자 type이 jclass.
// jclass는 MyNativeActivity.java를 지칭
c = 10;
}
JNIEXPORT void
Java_com_my_package_name_MyNativeActivity_terminate(JNIEnv* env, jclass thiz) {
c = 0;
}
JNIEXPORT jint
Java_com_my_package_name_MyNativeActivity_init(JNIEnv* env, jobject thiz, jint a, jint b) {
// 일반 Method는 두번째 인자 type이 jobject.
// jobject는 MyNativeActivity를 지칭
// 함수 인자는 뒤이어 등장. int -> jint, boolean -> jboolean 등의 형태.
return a + b + c;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment