如何使用/合并CPP文件到Android项目?

时间:2023-01-17 13:10:48

We have developed an iPad application where the core logic is written in CPP code, so that we can use the same code files/libraries to other platforms.

我们开发了一个iPad应用程序,其中核心逻辑是用CPP代码编写的,因此我们可以将相同的代码文件/库用于其他平台。

Now I want to use those files and develop similar Android application, but unable to create .so files and integrate paths in Android.mk files and all. I am basically an iOS developer, this is first time I am looking into Android NDK.

现在我想使用这些文件并开发类似的Android应用程序,但无法创建.so文件并在Android.mk文件和所有文件中集成路径。我基本上是iOS开发人员,这是我第一次看到Android NDK。

Can anyone help and guide if there is any straight forward steps to it.

如果有任何直接的步骤,任何人都可以帮助和指导。

I have already gone through android developers site and few other tutorial sites. But none of those worked for me.

我已经通过Android开发人员网站和其他一些教程网站。但这些都不适合我。

Require easy-clear steps to call cpp method in java, if I do have few cpp files and .a libraries with me already.

需要简单明了的步骤来调用java中的cpp方法,如果我已经有很少的cpp文件和.a库了。

1 个解决方案

#1


1  

You aren't very specific at the step you are stuck at.

你在坚持的步骤中并不是很具体。

Here's a very quick explanation on how to call native code from java (android) :

这里有一个关于如何从java(android)调用本机代码的快速解释:

  • first create a method to be exported by the native and called by java (this uses JNI, so google JNI , JNIEXPORT)

    首先创建一个由本机导出并由java调用的方法(这使用JNI,所以google JNI,JNIEXPORT)

  • once you have this method defined in your native code, it's time to create a shared library (.so) file , using the compiler that comes in the NDK (because you are compiling for android ). You will need to compile for the correct architecture of the device (armeabiv7s is the most common now days).

    一旦你在你的本机代码中定义了这个方法,就可以使用NDK中的编译器创建一个共享库(.so)文件了(因为你正在为android编译)。您需要编译设备的正确架构(armeabiv7s是现在最常见的日子)。

  • you need to add the library file in your app.apk inside the armeabi folder (more details in NDK tutorials).

    你需要在app.apk中的armeabi文件夹中添加库文件(NDK教程中的更多细节)。

  • inside your java code you will need to load the shared library via the System.loadLibrary(LIBRARY_NAME);

    在您的java代码中,您需要通过System.loadLibrary(LIBRARY_NAME)加载共享库;

  • inside your java code you will need to have defined static native methods that are in pair with the methods you exported from your CPP code

    在您的Java代码中,您需要定义与您从CPP代码导出的方法配对的静态本机方法

Quick tips :

快速提示:

  • use C functions,not CPP , since CPP will be mangled in the resulting shared library. In other words, you will need to create a C wrapper that will call your cpp code.

    使用C函数而不是CPP,因为CPP将在生成的共享库中被破坏。换句话说,您将需要创建一个将调用cpp代码的C包装器。

  • look over a hello world tutorial for NDK , and work yourself from there . Here's a link to such tutorial http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/

    查看NDK的hello world教程,并从那里开始工作。这是教程http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/的链接

You will bump later on into compilation issues with the makefiles, but by then you will probably be able to be more specific with your question.

稍后您将遇到使用makefile的编译问题,但到那时您可能会对您的问题更加具体。

#1


1  

You aren't very specific at the step you are stuck at.

你在坚持的步骤中并不是很具体。

Here's a very quick explanation on how to call native code from java (android) :

这里有一个关于如何从java(android)调用本机代码的快速解释:

  • first create a method to be exported by the native and called by java (this uses JNI, so google JNI , JNIEXPORT)

    首先创建一个由本机导出并由java调用的方法(这使用JNI,所以google JNI,JNIEXPORT)

  • once you have this method defined in your native code, it's time to create a shared library (.so) file , using the compiler that comes in the NDK (because you are compiling for android ). You will need to compile for the correct architecture of the device (armeabiv7s is the most common now days).

    一旦你在你的本机代码中定义了这个方法,就可以使用NDK中的编译器创建一个共享库(.so)文件了(因为你正在为android编译)。您需要编译设备的正确架构(armeabiv7s是现在最常见的日子)。

  • you need to add the library file in your app.apk inside the armeabi folder (more details in NDK tutorials).

    你需要在app.apk中的armeabi文件夹中添加库文件(NDK教程中的更多细节)。

  • inside your java code you will need to load the shared library via the System.loadLibrary(LIBRARY_NAME);

    在您的java代码中,您需要通过System.loadLibrary(LIBRARY_NAME)加载共享库;

  • inside your java code you will need to have defined static native methods that are in pair with the methods you exported from your CPP code

    在您的Java代码中,您需要定义与您从CPP代码导出的方法配对的静态本机方法

Quick tips :

快速提示:

  • use C functions,not CPP , since CPP will be mangled in the resulting shared library. In other words, you will need to create a C wrapper that will call your cpp code.

    使用C函数而不是CPP,因为CPP将在生成的共享库中被破坏。换句话说,您将需要创建一个将调用cpp代码的C包装器。

  • look over a hello world tutorial for NDK , and work yourself from there . Here's a link to such tutorial http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/

    查看NDK的hello world教程,并从那里开始工作。这是教程http://trivedihardik.wordpress.com/2011/06/16/hello-world-example-using-ndk-in-android/的链接

You will bump later on into compilation issues with the makefiles, but by then you will probably be able to be more specific with your question.

稍后您将遇到使用makefile的编译问题,但到那时您可能会对您的问题更加具体。