Last active
July 5, 2017 09:30
-
-
Save sunwayforever/71418518dc65b509e23f32a1dfbb9cac to your computer and use it in GitHub Desktop.
android jni example
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
Android.mk | |
========== | |
LOCAL_PATH := $(call my-dir) | |
include $(CLEAR_VARS) | |
LOCAL_MODULE_TAGS := tests | |
LOCAL_SRC_FILES := $(call all-java-files-under, src) | |
LOCAL_SDK_VERSION := current | |
LOCAL_PACKAGE_NAME := Test | |
LOCAL_JNI_SHARED_LIBRARIES := libtest | |
include $(BUILD_PACKAGE) | |
include $(call all-makefiles-under,$(LOCAL_PATH)) | |
JNI Android.mk | |
========== | |
LOCAL_PATH := $(call my-dir) | |
include $(CLEAR_VARS) | |
LOCAL_SRC_FILES = <...> | |
LOCAL_C_INCLUDES += \ | |
$(JNI_H_INCLUDE) | |
LOCAL_STATIC_LIBRARIES += <...> | |
LOCAL_SHARED_LIBRARIES := | |
libutils \ | |
liblog \ | |
LOCAL_MODULE := libtest | |
include $(BUILD_SHARED_LIBRARY) | |
libtest | |
========== | |
#include <jni.h> | |
extern "C" { | |
void test(JNIEnv* env, jobject thiz) { | |
ALOGE("test"); | |
} | |
} | |
static JNINativeMethod gMethods[] = { | |
{ "test","()V", (void*) test} | |
}; | |
jint JNI_OnLoad(JavaVM* vm, void* reserved) | |
{ | |
JNIEnv *env; | |
if (vm->GetEnv((void **)&env, JNI_VERSION_1_6)) { | |
return JNI_ERR; | |
} | |
jclass clazz; | |
clazz = env->FindClass("com.sunway.test.Test"); | |
if (clazz == NULL) { | |
ALOGE("Native registration unable to find class '%s'", "com.sunway.test.Test"); | |
return JNI_FALSE; | |
} | |
if (env->RegisterNatives(clazz, gMethods, 1) < 0) { | |
ALOGE("RegisterNatives failed for '%s'", "com.sunway.test.Test"); | |
return JNI_FALSE; | |
} | |
return JNI_VERSION_1_6; | |
} | |
Test.java | |
========== | |
public class Test extends Activity | |
{ | |
private native void test(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
System.loadLibrary("test"); | |
test(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment