首页 > 代码库 > NDK 的helloworld步奏

NDK 的helloworld步奏

1. helloworld.c

#include <string.h>#include <jni.h>/* * Class:     com_example_ndk_NativeHelloworld * Method:    hello * Signature: ()Ljava/lang/String; */jstring JNICALL Java_com_example_ndk_NativeHelloworld_hello(JNIEnv * env, jclass thiz) {    return (*env)->NewStringUTF(env, "HelloWorld! I am from JNI !");}

 

2.Android.mk

LOCAL_PATH := $(call my-dir)    include $(CLEAR_VARS)  #LOCAL_MODULELOCAL_MODULE    := hello  LOCAL_SRC_FILES := helloworld.c    include $(BUILD_SHARED_LIBRARY) 

3.NativeHelloworld.java

package com.example.ndk;import android.util.Log;public class NativeHelloworld {    public static native String hello();        static {          Log.i("NativeClass","before load library");          System.loadLibrary("hello");//注意这里为自己指定的.so文件,无lib前缀,亦无后缀          Log.i("NativeClass","after load library");        }  }

4.用ndk在项目目录里面编译出hello.so

本人是在linux系统下编译,放到eclipse的libs/armeabi下

5.调用方法,运行

NDK 的helloworld步奏