Android配置NDK环境

准备工作
1、下载NDK
2、直接运行,会解压到当前文件夹
3、剪切到你喜欢的文件夹

第一个项目
1、写一个调用JNI的Java类

package com.neohope.android.jni;

public class JniFunc {
    private native int  addNative(int a, int b);

    static {
        System.loadLibrary("jnifunc");
    }

    public int add(int a, int b)
    {
        return addNative(a,b);
    }
}

2、用你喜欢的方式,编译为class文件

3、用jdk的javah工具生成头文件

#在class文件的顶层路径,比如这个例子,就在com这个文件夹相同目录下
javah com.neohope.android.jni.JniFunc

会输出文件“com_neohope_android_jni_JniFunc.h”:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_neohope_android_jni_JniFunc */

#ifndef _Included_com_neohope_android_jni_JniFunc
#define _Included_com_neohope_android_jni_JniFunc
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_neohope_android_jni_JniFunc
 * Method:    addNative
 * Signature: (II)I
 */
JNIEXPORT jint JNICALL Java_com_neohope_android_jni_JniFunc_addNative
  (JNIEnv *, jobject, jint, jint);

#ifdef __cplusplus
}
#endif
#endif

4、编写“com_neohope_android_jni_JniFunc.c”

#include <jni.h>
#include "com_neohope_android_jni_JniFunc.h"

JNIEXPORT jint JNICALL Java_com_neohope_android_jni_JniFunc_addNative
  (JNIEnv *evn, jobject obj, jint a, jint b)
{
    return a+b;
}

5、编写Android.mk及Application.mk

APP_ABI := all
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := jnifunc
LOCAL_SRC_FILES := com_neohope_android_jni_JniFunc.c \

LOCAL_C_INCLUDES += com_neohope_android_jni_JniFunc.h

include $(BUILD_SHARED_LIBRARY)

6、编译

SET NDK_HOME="C:\Languages\Android\android-ndk-r10d"

SET PATH=%NDK_HOME%;%PATH%

CMD

REM ndk-build

Leave a Reply

Your email address will not be published. Required fields are marked *

*