在Android上使用C/C ++库 [英] Using C/C++ Library on Android

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

问题描述

我已经编码了一个库,该库提供了一些使用c/c ++编程语言进行加,减,除,乘的功能.它是使用Android NDK构建到library.so的.因此,现在,我想使用Android来调用库的这些功能.我想按自己的方式去做?

I have coded a library that provides some function add, sub, divide, multi by c/c++ programming language. It's built to library.so by using Android NDK. So now, I want to call these function of library by using Android. I want to as How I can do ?

谢谢.

推荐答案

NDK软件包

Android Studio仅包含默认工具和SDK,因此请分别下载并安装NDK软件包.有两种方法可以做到这一点.第一个也是最简单的是"SDK工具"选项卡中的自动安装"选项.这是一个很大的下载文件(〜1GB),因此请确保拥有良好的互联网连接和/或耐心.

Android Studio only includes the default tools and SDK, so download and install the NDK package separately. There are two ways to do this. The first and easiest is the automatic installation option in the SDK Tools tab. This is a large download (~1GB) so be sure to have a good internet connection and/or patience.

第二种方法是从 NDK下载页面.由于下载大小较小(<.5 GB),因此可以节省一些时间,但需要进行一些设置.下载适合您平台的NDK软件包,然后按照安装说明进行操作.您可以将提取的软件包放在所需的任何位置,但是请记住此位置,以备日后使用.

The second way is to download the NDK manually from the NDK Downloads page. This saves some time because of the smaller download size (<.5 GB) but will need some setting up. Download the appropriate NDK package for your platform and follow the installation instructions. You can put the extracted package anywhere you want, but remember this location as you will need it later.

Hello World

让我们运行一个测试项目,看看NDK安装是否可行. NDK软件包文件夹中包含示例,但如果没有额外的配置,它们似乎无法在Android Studio中立即使用.取而代之的是,我们将从欢迎屏幕中导入可以使用的示例.这些来自GitHub的 NDK示例也可以直接下载或克隆.该示例集合与最新版本的Android Studio具有更好的兼容性.

Let’s run a test project to see if the NDK installation works. The NDK package folder contains samples but they don’t seem to work out of the box in Android Studio without extra configuration. Instead we’ll import samples that work from the welcome screen. These NDK samples from GitHub can also be downloaded or cloned directly. This collection of samples has better compatibility with the latest version of Android Studio.

选择导入Android代码示例",然后在搜索框中键入hello以过滤列表.从筛选"列表中的"Ndk"类别下选择"Hello JNI".单击下一步"编辑应用程序名称和项目位置,然后单击完成".

Select Import an Android code sample and type hello in the search box to filter the list. Choose Hello JNI from the filtered list under the Ndk category. Click Next to edit the app name and project location, then click Finish.

JNI

在加载代码并同步Gradle之后,让我们看一下生成的项目结构.您可能已经注意到,项目名称似乎有差异.为什么叫HelloJNI而不叫HelloNDK?什么是"JNI",它与"NDK"有什么区别? "Java本机接口"是使Java应用程序与本机代码进行交互的框架. JNI和NDK共同努力,为Android应用程序带来本机支持. NDK是Android框架的一部分,而JNI可用于任何Java应用程序,而不仅仅是Android应用程序.

After the code has loaded and Gradle has synced, let’s take a look at the resulting project structure. You might have noticed what appears to be a discrepancy in the project’s name. Why is it called HelloJNI and not HelloNDK? What is ‘JNI’ and how does it differ from the ‘NDK’? The ‘Java Native Interface’ is a framework that enables Java applications to interact with native code. The JNI and the NDK work together to bring native support to Android apps. The NDK is part of the Android framework while the JNI is available to any Java application, not just Android apps.

项目内部是一个名为jni的文件夹,它将保存该应用程序的所有本机C或C ++源代码. JNI提供了一个双向接口. C/C ++代码能够调用Java代码,包括标准的Android库,并且Java代码能够调用在C/C ++代码中定义的本机函数.本机代码受与Java代码相同的沙箱和安全规则的约束,因此该应用不会完全不受限制地访问硬件.有关JNI的更详细讨论,请阅读 Android开发者网站上的JNI文章. .

Inside the project is a folder named jni which will hold all the native C or C++ source code of the app. The JNI provides a two-way interface. The C/C++ code is able to call Java code, including the standard Android libraries, and the Java code is able to call native functions defined in C/C++ code. Native code is governed by the same sandbox and security rules as Java code so the app will not have full unrestricted access to hardware. For a more detailed discussion on the JNI, read the JNI articles from the Android Developer website.

这篇关于在Android上使用C/C ++库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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