JNI笔记

来源:互联网 发布:张成泽犬决 知乎 编辑:程序博客网 时间:2024/04/30 16:30

理解JNI
JNI编写

1、编写Java类接口并编译
示例:

public class ReedSolomon {public static native int rsInit();    public static native String rsEncode(String data);    public static native String rsDecode(String rsdata);        static     {        System.loadLibrary("rs");     }}

编译:javac src\com\libra\sinvoice\ReedSolomon.java -d bin\classes\
备注:javac的用法
2、javah 生成头文件
javah -classpath bin\classes -jni com.libra.sinvoice.ReedSolomon 

可以在jni目录生成头文件com_libra_sinvoice_ReedSolomon.h

/* DO NOT EDIT THIS FILE - it is machine generated */#include <jni.h>/* Header for class com_libra_sinvoice_ReedSolomon */#ifndef _Included_com_libra_sinvoice_ReedSolomon#define _Included_com_libra_sinvoice_ReedSolomon#ifdef __cplusplusextern "C" {#endif/* * Class:     com_libra_sinvoice_ReedSolomon * Method:    rsInit * Signature: ()I */JNIEXPORT jint JNICALL Java_com_libra_sinvoice_ReedSolomon_rsInit(JNIEnv *, jclass);/* * Class:     com_libra_sinvoice_ReedSolomon * Method:    rsEncode * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_libra_sinvoice_ReedSolomon_rsEncode(JNIEnv *, jclass, jstring);/* * Class:     com_libra_sinvoice_ReedSolomon * Method:    rsDecode * Signature: (Ljava/lang/String;)Ljava/lang/String; */JNIEXPORT jstring JNICALL Java_com_libra_sinvoice_ReedSolomon_rsDecode(JNIEnv *, jclass, jstring);#ifdef __cplusplus}#endif#endif



3、编写JNI C代码

4、编写Android.mk
示例:
# Copyright (C) 2009 The Android Open Source Project## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at##      http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.#LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE    := rsLOCAL_SRC_FILES := LOCAL_SRC_FILES += reed_solomon.cLOCAL_SRC_FILES += rscode.cLOCAL_C_INCLUDES := #LOCAL_C_INCLUDES += $(LOCAL_PATH)LOCAL_LDLIBS :=LOCAL_LDLIBS += -L$(SYSROOT)/usr/lib -llogLOCAL_CFLAGS :=#LOCAL_STATIC_LIBRARIES := $(ANDROID_MAVEN_PLUGIN_LOCAL_STATIC_LIBRARIES)                      # Include the Android Maven plugin generated makefile# Important: Must be the last import in order for Android Maven Plugins paths to work#include $(ANDROID_MAVEN_PLUGIN_MAKEFILE)include $(BUILD_SHARED_LIBRARY)

5、NDK配置

工程名-Property-Builder-New






JNI常用函数:

返回jtsring类型数据

return (*env)->NewStringUTF(env, "abc"); // c 

return env->NewStringUTF(env, "abc"); // c ++









0 0
原创粉丝点击