android JNI 实现测试

本文详细介绍了如何在Android中使用JNI技术,从定义JAVA类、生成头文件、实现C/C++代码、创建mk文件到编译生成.so库,并最终将库推送到设备上进行调用的全过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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.创建应用程序就可以调用了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值