简体   繁体   English

使用android ndk从.so文件中调用本地函数

[英]using android ndk to call native function from .so file

I found instructions for how to link and use c/c++ code in Android by utilizing the NDK. 我发现了有关如何利用NDK在Android中链接和使用c / c ++代码的说明。 But I'm searching how call function from third party .so . 但是我正在寻找如何从第三方.so调用函数。

For example your prebuilt library is called "libmy.so" 例如,您的预建库称为“ libmy.so”

In the project folder of the project you want to use it: 在要使用的项目的项目文件夹中:

1) create libmy folder in jni folder ( jni/libmy ) 1)在jni文件夹( jni/libmy )中创建libmy文件夹

2) copy your libmy.so here 2)在这里复制您的libmy.so

Then, just create a jni/libmy/Android.mk file: 然后,只需创建一个jni/libmy/Android.mk文件:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := libmy
LOCAL_SRC_FILES := libmy.so
include $(PREBUILT_SHARED_LIBRARY)

Now in your jni/Android.mk you can write: 现在在您的jni / Android.mk中,您可以编写:

LOCAL_SHARED_LIBRARIES := libmy

Then when you do ndk-build, it will copy this library in to libs/armeabi/ 然后,当您执行ndk-build时,它将将此库复制到libs / armeabi /中。

After that you can use this library in your C++ code. 之后,您可以在C ++代码中使用此库。

You just have to put the .so in your libs/armeabi-v7a folder (or whatever other architecture you have compiled for, like armeabi, x86 etc.) and Eclipse will automatically see it and integrate it into the APK. 您只需将.so放入您的libs / armeabi-v7a文件夹(或您为之编译的任何其他体系结构,例如armeabi,x86等)中,Eclipse就会自动看到它并将其集成到APK中。

Then to access any native functions from the .so in your Java code, you just have to declare it as a native function at the top of your class. 然后,要从Java代码中的.so访问任何本机函数,只需在类顶部将其声明为本机函数即可。 For example 例如

protected static native void AKUAppInitialize (); 受保护的静态本机无效AKUAppInitialize();

which can then be called anywhere later in the code like 然后可以在以后的代码中的任何地方调用

AKUAppInitialize(); AKUAppInitialize();

如果您有源代码,则必须配置NDK部分:请看本教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM