Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -lm -llog
LOCAL_SHARED_LIBRARIES := liblog libcutils
LOCAL_DEFAULT_CPP_EXTENSION := cpp
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := \
main.cpp \
http_get.cpp \
include $(BUILD_SHARED_LIBRARY)
Main.cpp
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <jni.h>
#include <assert.h>
#include "http_get.h"
#include <android/log.h>
#define LOG_TAG "MY"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,LOG_TAG,__VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,LOG_TAG,__VA_ARGS__)
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE,LOG_TAG,__VA_ARGS__)
extern "C" {
JNIEXPORT jstring JNICALL native_hello(JNIEnv *env, jclass clazz)
{
printf("hello in native code.");
mmain();
return env->NewStringUTF("-------------hello world mmain().-------------");
}
#define JNIREG_CLASS "ub/test/hellojni/HelloJniActivity"
static JNINativeMethod gMethods[] = {
{ "native_hello", "()Ljava/lang/String;", (void*)native_hello },
}; /* * Register several native methods for one class. */
static int registerNativeMethods(JNIEnv* env, const char* className, JNINativeMethod* gMethods, int numMethods)
{
jclass clazz;
clazz = env->FindClass(className);
if (clazz == NULL)
{
return JNI_FALSE;
}
if (env->RegisterNatives(clazz, gMethods, numMethods) < 0)
{
return JNI_FALSE;
}
return JNI_TRUE; }
/* * Register native methods for all classes we know about. */
static int registerNatives(JNIEnv* env)
{
if (!registerNativeMethods(env, JNIREG_CLASS, gMethods, sizeof(gMethods) / sizeof(gMethods[0])))
{
return JNI_FALSE;
}
return JNI_TRUE;
}
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void* reserved)
{
JNIEnv* env = NULL;
jint result = -1;
if (vm->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK)
{
return -1;
}
assert(env != NULL);
if (!registerNatives(env))
{
return -1;
}
/* success -- return valid version number */
result = JNI_VERSION_1_4;
return result;
}
}
HelloJniActivity.java
package ub.test.hellojni;
public class HelloJniActivity extends Activity {
/** Called when the activity is first created. */
public native String native_hello();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v("MY", "onCreate! " + native_hello());
}
static {
System.loadLibrary("mylib");
}
}
댓글 없음:
댓글 쓰기