使用JNI只有一个C共享库和头文件从Android的调用C [英] Call C from Android using JNI with only a C shared library and header file

查看:219
本文介绍了使用JNI只有一个C共享库和头文件从Android的调用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Android Studio 0.3.1

您好,

我已经写在C以下的库,但没有源$ C ​​$ C它只有libapp_module.so

I have the following library written in C but don't have the source code for it only the libapp_module.so

libapp_module.so: ELF 32-bit LSB shared object, ARM, version 1 (SYSV), dynamically linked (uses shared libs), not stripped

我对这个库中的头文件:

I have the header file for this library:

#ifdef __cplusplus
extern "C" {
#endif

#ifdef _WIN32
#define LIB_API(type) __declspec(dllexport) type
#else 
#define LIB_API(type) type
#endif

LIB_API(int) module_init();

#ifdef __cplusplus
}
#endif

问题是例外,当我尝试从我的Andr​​oid应用程序调用该函数:

The problem is the exception when I try and call the function from my Android App:

D/dalvikvm﹕ Added shared lib /data/app-lib/com.sunsystems.snaptest-1/libapp_module.so 0x416f3d88
D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.sunsystems.snaptest-1/libapp_module.so 0x416f3d88, skipping init
W/dalvikvm﹕ No implementation found for native Lcom/sunsystems/snaptest/App_Module;.module_init:()I
UnsatisfiedLinkError: Native method not found: com.sunsystems.snaptest.App_Module.module_init:()I

这是我在做什么。

我创建了一个Android应用程序,我想,所以我创建了一个名为App_Module.java类从我的应用程序调用宏module_init()

I have created a Android App and I want to call that module_init() from my App so I have created a class called App_Module.java

public class App_Module {
    /* Load native C libraries */
    static {
        System.loadLibrary("app_module");
    }

    public native static int module_init();
}

和我使用JNI这样在我的项目的根目录:

And I used JNI like this in my root of the project:

javah -jni -classpath build/classes/debug -d jni/ com.sunsystems.snaptest.App_Module

即生成下面的头接口:

Which generated the following header interface:

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_sunsystems_snaptest_App_Module */

#ifndef _Included_com_sunsystems_snaptest_App_Module
#define _Included_com_sunsystems_snaptest_App_Module
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_sunsystems_snaptest_App_Module
 * Method:    module_init
 * Signature: ()I
 */
JNIEXPORT jint JNICALL Java_com_sunsystems_snaptest_App_Module_module_1init
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

然后在我的Andr​​oid应用我加载库在上面App_Module类并调用它像这样:

Then in my Android App I load the library in the above App_Module class and call it like this:

App_Module.module_init()

所以我想它找不到libapp_module.so库中的符号。

So I guess it cannot find symbol inside the libapp_module.so library.

非常感谢您的任何建议,

Many thanks for any suggestions,

推荐答案

您必须实现本地方法,使其调用函数库中的。例如:

You have to implement the native method and make it call the function in your library. For example:

#include "header_file_for_your_library.h"

#include "com_sunsystems_snaptest_App_Module.h"

JNIEXPORT jint JNICALL Java_com_sunsystems_snaptest_App_Module_module_1init(JNIEnv *env, jclass klass) {
    return module_init();
}

就有关JNI或Android NDK任何教程将有进一步的细节。

Just about any tutorial on JNI or Android NDK will have further details.

这篇关于使用JNI只有一个C共享库和头文件从Android的调用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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