导入现有的C ++库(.a或.so文件)NDK Android [英] Import existing c++ library (.a or .so file) ndk android

查看:93
本文介绍了导入现有的C ++库(.a或.so文件)NDK Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚经历了android的本机开发。我成功地将 AndroidStudio 2.2.2 准备好进行本机部署

I just gone through native development in android. I am successful in getting my AndroidStudio 2.2.2 ready for native debelopment

我还构建了示例 hello-jni 项目

我要实现的目标

我正在尝试使用现有的用c ++设计的库(将为我提供静态库 .a 扩展名或 .so 文件)

I am trying to use an existing library designed in c++ (I will be provided with static library .a extension or .so file)

关于原生开发的一些困惑

1)我应该使用.cpp&现有c ++库的.h文件而不是 .a .so 文件?

1) Shall I use the .cpp & .h files of the existing c++ library instead of .a or .so file ?

2)我可以吗?需要制作 CMakeLists.text :就我用google搜索我的 .a文件而言,它不是使用 ndk-build 生成的,所以我需要制作

2) Do I need to make CMakeLists.text : So far as I googled my .a files is not generated using ndk-build , so I need to make it.

如果我使用 .cpp& .h文件,我应该制作为 Android.mk & Application.mk

If I use .cpp & .h files , should I make Android.mk & Application.mk

CMakeLists.text 是否将我新开发的android项目编译为库或现有的 .a 文件?

Does CMakeLists.text compile my newly developed android project as library or my existing .a file ?

3)我将 .a 文件放在哪里?它在 jni文件夹下吗?

3) Where do I put the .a file in my project . Is it under jni folder?

4)我的Java类文件是否应该使用关键字native 定义方法,例如如在c ++文件中实现的那样(示例:在c ++文件中,方法名称为getData(),java类应包含公共本机getData())

4) Should my java class files should define methods with keyword native same like as implemented in c++ file (Example : In c++ file method name getData() , should java class contain public native getData() )

推荐答案

<好的,所以你有很多问题。其中一些问题属于个人喜好类型,但我将根据个人选择提供。

Ok so you have bunch of questions. Some of these questions are personal preference type but I will provide them as my personal choice.

这是您的选择。我个人将使用已编译的 .so 文件。这样,我永远不必担心NDK和CMake和.mk文件。如果有该文件,则只需将文件添加到 libs 文件夹(而不是 lib 文件夹)中即可。然后对您的 build.gradle 文件进行较小的更改。就是这样。

This is your choice. I, personally, would use the compiled .so file. This way I never have to worry about NDK and CMake and .mk files. If you have the file, all you have to do is add the file to the libs folder (not lib folder) and make a minor change to your build.gradle file. That's it.

更改为build.gradle:

Change to build.gradle:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
        jniLibs.srcDirs = ['libs']
    }
}



2& 3



这些与该选项无关。

2 & 3

These would be irrelevant with this option.

无论使用文件还是编译的库,您都必须这样做:

You would have to do something like this no matter if you use the files or the compiled libraries:

@SuppressWarnings("JniMissingFunction")
public class MyNativeMethods {
    static {
        System.loadLibrary("my_native_lib");
    }

    public native int native_method_1(int fd);
    public native int native_method_2(int fd);
    public native void native_method_3(int fd, int arr[]);
    public native int[] native_method_4(int fd);
}

然后,您可以从调用这些方法活动 / 片段

And then, you can call those methods from your Activity/Fragment.

希望这很清楚。

1) .so .a 文件是您的本机库。

1) .so or .a files are your native libraries.

2) .cpp .c ,等文件只是您的本机源代码文件。如果要在项目中使用这些文件,则必须使用构建系统(例如CMake)来使用它们。 CMake将获取您的源代码文件,并创建一个 .so 库,该库又是本机库。这就是为什么我建议使用 .so 文件的原因,因为为什么在不需要时在项目中实现CMake的工作呢?

2) the .cpp, .c, etc. files are just your native source code files. If you were to use those files in the project, you would have to use a build system (for example, CMake) to use them. CMake would take your source code files and make a .so library which is again the native library. This is why I suggested to use the .so files because why do the work of implementing CMake in your project when you don't need to?

如果您想将来尝试CMake或学习它,请查看以下答案:带有Android Studio版本2.2的C / C ++

If you want to try CMake or learn it in the future, check this answer: C/C++ with Android Studio version 2.2

3) System.loadLibrary( my_native_lib); :在这里,您告诉Java运行时添加此给定的库。这样,您就可以在Java和库中的C ++代码之间创建链接。该行下方的方法应与C ++ / C代码中的名称相同。这样,Java运行时将找到并打开该库,并在您加载的库中查找那些方法。在此处

3) System.loadLibrary("my_native_lib");: Here you are telling the Java runtime to add this given library. This way you are creating a link between Java and the C++ code that is within the library. The methods below that line should have the same name as they do in the C++/C code. This way Java runtime will find and open the library and look for those method in the library you load. See more here

这篇关于导入现有的C ++库(.a或.so文件)NDK Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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