如何使用本机库 [英] How to use native library

查看:141
本文介绍了如何使用本机库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我有一个名为HelperLib的C ++库.我有一些假设:

For example, I have a C++ library which was called HelperLib. I have some assumptions:

  1. 我可以将此项目编译为* so文件. (通过一些在线教程可以帮助我在Android上构建该库).

  1. I can compile this project into *so file. (by some tutorial online that help me build this library on Android).

我可以在C/C ++中使用此库.通过图书馆网站上的官方教程.

I can use this library in C/C++. By official tutorial on library's website.

我对本机库有基本的了解.例如,使用System.loadlibrary()加载本机库,例如调用简单的本机C函数.

I have a basic knowledge about native library. For example, loading a native library using System.loadlibrary(), calling simple native C function for example.

当我决定在线使用某些C ++库时,经常会遇到这些假设.他们提供了使用方法的教程,而另一些则提供了如何在Android上构建此库的信息.

Those assumptions I often meet when decide to use some C++ libraries online. They provide tutorials how to use, and some other provide how to build this library on Android.

但是,尽管如此,我仍然无法弄清楚如何在Android上使用该库.从哪儿开始?在可以使用该库之前,我应该做些什么?例如.

But with all of these, I still cannot figure out how to use this library on Android. Where to start? Which should I do more, before I can use this library? For example.

///// this is a C++ source file to use library
int main() {
   HelperLib lib = new HelperLib(); // just for example
   lib.initialize();
   /// demo how this library was used
   lib.makeConnection();
   lib.closeConnection();
   return 0;
}

基于以上代码,我可以将其移植到Android层吗?如何移植?通过调用line-by-line函数作为C ++示例是否简单?还是我们必须做别的事情"?当很多人说"Android的教程端口库",却没有提供调用库函数的有用方法时,这使我感到困惑.

Based on the above code, can I port it to Android layer and how? Does it simple by calling line-by-line function as C++ example? Or we must do "something else" ? This makes me confuse many times, when many people say "tutorial port library to Android", but not provide a useful way to invoke library function.

在以上假设的基础上,请帮助我如何继续使用该库.在线上有任何教程可以帮助我解决这个问题吗?

Please help me, with above assumptions, how can I continue to use this library. Are there any tutorials online to help me on this problems?

谢谢:)

推荐答案

就像直接提到 @Gabe Sechan 一样简单在评论中.

It's as straightforward as @Gabe Sechan has already mentioned in the comments.

1.我可以将该项目编译为* so文件. (通过一些在线教程可以帮助我在Android上构建该库).

1.I can compile this project into *so file. (by some tutorial online that help me build this library on Android).

您将需要将JNI包装器添加到本机库.例如wrapper.cpp:

You will need to add the JNI wrapper to the native library. For e.g., wrapper.cpp:

HelperLib lib;

void Java_com_example_hqt_MainActivity_initialize() {
    lib = new HelperLib();
    lib.initialize();
}

void Java_com_example_hqt_MainActivity_makeConnection() {
    lib.makeConnection();
}

然后

3.我对本机库有基本的了解.例如,使用System.loadlibrary()加载本机库,例如调用简单的本机C函数.

3.I have a basic knowledge about native library. For example, loading a native library using System.loadlibrary(), calling simple native C function for example.

也就是说,在MainActivity.java中:

That's, in MainActivity.java:

static {
    Log.d(TAG, "loadLibrary");
    System.loadLibrary("mynativelibrary");
}

public static native void initialize();
public static native void makeConnection();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Init native library
    initialize();
}

public void makeNativeConnection() {
    makeConnection();
}

希望这会有所帮助:)

这篇关于如何使用本机库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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