Android NDK-如何功能调用另一个预建库 [英] Android NDK - How to function call another prebuilt library

查看:147
本文介绍了Android NDK-如何功能调用另一个预建库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个预构建的.so文件,我的Android.mk是:

I had a pre-built .so file , my Android.mk is:

LOCAL_MODULE    := hello
LOCAL_SRC_FILES := libfoo.so hello.c
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
include $(BUILD_SHARED_LIBRARY)

现在,我想要的是如何构建hello.c,它调用一个函数(getBoo-位于libfoo.so中的文件)

Now, what i want is how to build hello.c , which calls a function ( getBoo - Located in libfoo.so ) file

我到目前为止所拥有的是:

What i have so far is:

#include <jni.h>

jstring Java_com_example_getBoo( JNIEnv*  env,jobject  this,jstring x )
{
    return foo.getBoo(x);
}

很明显,没有引用libfoo.so文件,我该如何解决?

Which, obviously, isn't referenced to libfoo.so file , how do i fix this?

推荐答案

Android.mk :

LOCAL_PATH      := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE    := hello
LOCAL_SRC_FILES := hello.c
LOCAL_SHARED_LIBRARIES := foo_prebuilt
include $(BUILD_SHARED_LIBRARY)

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

别忘了您的Java代码应该以正确的顺序加载两个库:

Don't forget that your Java code should load the two libraries in correct order:

System.loadLibrary("foo");
System.loadLibrary("hello");

您还需要函数声明来满足C编译器的要求.

You also need function declaration to satisfy the C compiler.

#include <jni.h>
extern int getBoo(int);

jint Java_com_example_getBoo(JNIEnv* env, jobject this, jint x)
{
    return getBoo(x);
}

我希望您知道从libfoo.so导出的函数的名称.无论如何,NDK(ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-nm.exe)中有 nm 命令.使用-D运行它,您将获得此列表.通常,我们使用 附带的头文件,这些头文件用于提供前向声明.

I expected that you know the names of exported functions from libfoo.so. At any rate, there is nm command in NDK (ndk\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-nm.exe). Run it with -D and you have this list. Usually, we use header files that come with the libraries we use to provide forward declarations.

我看到您的原始示例打算采用字符串参数并返回字符串.这就增加了另一个层次的复杂性,请阅读有关JNI如何处理Java字符串的JNI书籍.

I see that your original example intend to take string argument and to return string. This adds another level of complexity, please read a JNI book on how JNI handles Java strings.

这篇关于Android NDK-如何功能调用另一个预建库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆