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

查看:34
本文介绍了导入现有的 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 项目

I also build sample hello-jni project

我正在努力实现的目标

我正在尝试使用用 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 :就我谷歌搜索而言,我的 .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 类是否应该包含 public native 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() )

推荐答案

注意:此答案并未说明在编写原生 (C++) 代码后如何使用 CMake.对于该设置,请阅读下面的答案以全面了解设置,然后查看此答案.

Note: This answer does not show how you can use CMake after writing native (C++) code. For that setup, read the answer below to get full understanding of the setup and then look at this answer.

好的,所以你有很多问题.其中一些问题是个人偏好类型,但我将提供它们作为我个人的选择.

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

#1这是你的选择.我个人会使用编译后的 .so 文件.这样我就不必担心 NDK 和 CMake 以及 .mk 文件.如果您有该文件,您只需将文件添加到 libs 文件夹(不是 lib 文件夹)并对您的 build.gradle 文件.就是这样.

#1 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.

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

#4 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);
}

然后,您可以从 Activity/Fragment 调用这些方法.

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

希望这已经足够清楚了.

Hope this is clear enough.

  1. .so.a 文件是您的原生库.

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

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

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 或将来学习它,请查看以下答案:C/C++ 与 Android Studio 版本 2.2

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

  1. System.loadLibrary(my_native_lib");:在这里您告诉 Java 运行时添加这个给定的库.这样,您就可以在 Java 和库中的 C++ 代码之间创建链接.该行下方的方法应该与它们在 C++/C 代码中的名称相同.这样,Java 运行时将找到并打开库并在您加载的库中查找那些方法.查看更多这里
  1. 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天全站免登陆