从另一个库调用一个库函数

Calling one library function from another library

本文关键字:一个 库函数 另一个 调用      更新时间:2023-10-16

我有一个库,比如libraryOne.so(File1.c(,它包含纯c代码。现在我想从我的Java文件中访问这些代码。为此,我将使用标准的JNI程序。但对于使用JNI,我还应该修改File1.C中的C代码(比如包括JNI的头文件和一些标准的JNIEXPORT(。因此,我正在创建一个包装器库(libraryWrapper.So由say File2.c制成(。这个库将包含所需的JNI声明,并将与我的Java文件交互现在我想知道如何从File2.C 调用File1.C的纯C函数

首先,您应该阅读ndk教程。因为你需要设置ndk等等。之后你可以寻找ndk样本来学习如何做事。以及如何构建

实际上上面是你的答案来回答你的问题。

你应该有libraryOne.so的源代码,正如你所说的那样。导致android针对许多平台,而你的so无法在这些平台上运行。因此需要使用ndk 进行编译

好的,让我们的文件File1.c

int myfunction(const char* st){
int answer=0;
  //stufs here
return answer;
}

首先,添加我们想要访问的Java类

package example.com.myapplication;
public class JniClass {
    static {
        System.loadLibrary("libraryWrapper");
    }
    public   native int myFunction(String a );
}

现在我们需要包装。为此,我们应该了解jni接口。幸运的是,有一个名为javah的工具,它可以为我们生成它,并使我们免于为所有调用手动执行它。我们应该构建我们的android项目,以便生成类文件。我用debug构建它,所以我生成的文件将位于/app/build/mediater/classes/debug文件夹中。

好的,然后我们运行javahjavah -jni example.com.myapplication.JniClas s

如果你有错误,在JNI 中使用Javah时会看到错误

它生成了名为example_com_myapplication_JniClass.h的头文件,其中包含

#include <jni.h>
/* Header for class example_com_myapplication_JniClass */
#ifndef _Included_example_com_myapplication_JniClass
#define _Included_example_com_myapplication_JniClass
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     example_com_myapplication_JniClass
 * Method:    myFunction
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
  (JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif

好的,现在你的答案。简单的方法是复制生成的代码并将其粘贴在File1.c代码的底部。并实施它:注意,我在粘贴后将参数命名为env、obj和str,并清除了注释

//..here your File1.c source code content
//
#include <jni.h> 
#ifdef __cplusplus
extern "C" {
#endif 
JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
  (JNIEnv * env, jobject obj, jstring str){
       const char * x=(*env)->GetStringUTFChars(env,str,0);
      //call our function
       int ret=  myfunction(x);
       (*env)->ReleaseStringUTFChars(env,str, x);
       return ret;
     }
#ifdef __cplusplus
}
#endif

但实际上,您可能会在c代码中使用头文件例如,您可以添加生成的头文件并在.c文件上实现它。在.c文件中,您可以包含File1.c头。这样更干净

假设您已经阅读了ndk教程。您设置了ndk工具并知道如何构建

最后,我们将File1.c添加到jni文件夹中,并更改Android.mk文件

LOCAL_MODULE:=库包装LOCAL_SRC_FILES:=文件1.c和

APP_MODULES:=库包装

然后我们用ndk构建它,它将把我们的so文件添加到libs 中

Op只需要File1.c和File2.cFile2.c 内部

//declaration of funtions inside File1.c
extern int myfunction(const char* st);
#include <jni.h> 
    #ifdef __cplusplus
    extern "C" {
    #endif 
    JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
      (JNIEnv * env, jobject obj, jstring str){
           const char * x=(*env)->GetStringUTFChars(env,str,0);
          //call our function
           int ret=  myfunction(x);
           (*env)->ReleaseStringUTFChars(env,str, x);
           return ret;
         }
    #ifdef __cplusplus
    }
    #endif

并更改Android.mk

LOCAL_SRC_FILES := File1.c 
LOCAL_SRC_FILES += File2.c