android JNI 实现测试

来源:互联网 发布:小学知乎 编辑:程序博客网 时间:2024/06/05 08:29
1.定义JAVA类,内部具有native关键字函数
如:
    package com.jni.jnitest;

    public class Algorithm {
        static {
            System.loadLibrary("Algorithm");
        }
        public native int add(int a,int b);
        public native int subtract(int a,int b);
    }

2.用javah命令生成.h文件。
    cd 到工程目录下,执行javah -classpath bin -d jni com.jni.jnitest.Algorithm 生成
    bin目录同时生成com.jni.jnitest.Algorithm.h文件
    如:
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_jni_jnitest_Algorithm */

    #ifndef _Included_com_jni_jnitest_Algorithm
    #define _Included_com_jni_jnitest_Algorithm
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_jni_jnitest_Algorithm
     * Method:    add
     * Signature: (II)I
     */
    JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add
      (JNIEnv *, jobject, jint, jint);

    /*
     * Class:     com_jni_jnitest_Algorithm
     * Method:    subtract
     * Signature: (II)I
     */
    JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract
      (JNIEnv *, jobject, jint, jint);

    #ifdef __cplusplus
    }
    #endif
    #endif
3.实现com_jni_jnitest_Algorithm.h文件,创建com_jni_jnitest_Algorithm.c
    如:
    #include "com_jni_jnitest_Algorithm.h"

    JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_add(JNIEnv *env,jobject c,jint a,jint b)
    {
        return (a+b);
    }

    JNIEXPORT jint JNICALL Java_com_jni_jnitest_Algorithm_subtract(JNIEnv*,jobject c,jint a,jint b)
    {
        return (a-b);
    }
4.定义mk文件
    如:
    #
    # Copyright (C) 2008 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.
    #

    # This makefile supplies the rules for building a library of JNI code for
    # use by our example of how to bundle a shared library with an APK.

    LOCAL_PATH:= $(call my-dir)
    include $(CLEAR_VARS)

    LOCAL_MODULE_TAGS := optional

    # This is the target being built.
    LOCAL_MODULE:= libAlgorithm


    # All of the source files that we will compile.
    LOCAL_SRC_FILES:= \
      com_jni_jnitest_Algorithm.c

    # All of the shared libraries we link against.
    LOCAL_SHARED_LIBRARIES := \
        libutils

    # No static libraries.
    LOCAL_STATIC_LIBRARIES :=

    # Also need the JNI headers.
    LOCAL_C_INCLUDES += \
        $(JNI_H_INCLUDE)

    # No special compiler flags.
    LOCAL_CFLAGS +=

    # Don't prelink this library.  For more efficient code, you may want
    # to add this library to the prelink map and set this to true. However,
    # it's difficult to do this for applications that are not supplied as
    # part of a system image.

    LOCAL_PRELINK_MODULE := false

    include $(BUILD_SHARED_LIBRARY)
5.放到android源代码录目下,自己任意创建一个目录。进入目录,执行mm就可以生成.so了
    然后将so push到手机中。 如:adb push libAlgorithm.so /system/lib/
6.创建应用程序就可以调用了。
原创粉丝点击