android开发使用c+

by Onur Tuna

通过Onur Tuna

如何在Android项目中开始使用C ++代码 (How to start using C++ code in your Android project)

Last year I gave a talk at the GDG DevFest in Ankara, Turkey. I have been planning to share that talk here ever since. Now that I’m a PhD candidate and have a bit more time, I’m putting the post down here.

去年,我在土耳其安卡拉的GDG DevFest上发表了演讲。 从那时起,我一直计划在这里分享这一话题。 现在,我是一名博士候选人,并且还有更多时间,现在我将其放在这里。

If you would like to get the presentation, it’s available on my drive.

如果您想获得演示文稿,可以在我的驱动器上找到 。

暖身 (Warm Up)

I would like to start by explaining the build process of an app in Android. Because you need to know some basic internal stuff, this topic is somewhat technical.

我首先要解释一下Android应用程序的构建过程。 因为您需要了解一些基本的内部知识,所以本主题有些技术性。

You don’t need to know everything shown in the image above — but it’s a good reference.

您不需要知道上图中显示的所有内容,但这是一个很好的参考。

Now say that you write an application for Android using Java. You’re going to have:

现在说您使用Java编写了一个Android应用程序。 您将拥有:

  • the source code for that application该应用程序的源代码
  • some sort of resource files (like images or xml files for the arrangement of the GUI)某种资源文件(例如用于安排GUI的图像或xml文件)
  • and perhaps some AIDL files, which are Java interfaces that make processes talk to each other.也许还有一些AIDL文件,它们是使进程相互通信的Java接口。

You’re probably also going to use additional libraries and their related files in your project.

您可能还会在项目中使用其他库及其相关文件。

When building a working app, you first compile those source codes together. A compiler will yield a DEX file which then can be read by a virtual machine. This machine-readable file and some additional information about the app will be packaged together by a package manager. The final package — called an APK package — is the final app.

在构建可运行的应用程序时,首先需要将这些源代码一起编译。 编译器将生成一个DEX文件,然后该文件可以由虚拟机读取。 该机器可读文件以及有关该应用的一些其他信息将由程序包管理器打包在一起。 最终程序包(称为APK程序包)是最终应用程序。

This is the build process of an Android package in the simplest terms.

这是最简单的Android程序包的构建过程。

Android执行时间 (Android Run Time)

Now let’s talk about the run time stuff. You have an app, and when it starts running it’s read by a machine. Android has two kinds of virtual machines to run an app. I won’t introduce the old one, called Dalvik, as today most Android devices run a virtual machine called Android Run Time, ART — so that’s what we’ll talk about here.

现在让我们谈谈运行时的东西。 您有一个应用程序,当它开始运行时,它会被机器读取。 Android有两种运行应用程序的虚拟机。 我不会介绍旧的Dalvik,因为今天大多数Android设备都运行一个名为Android Run Time,ART的虚拟机-因此我们将在这里讨论。

ART is an ahead-of-time (AOT) virtual machine. So, what does that mean? Let me explain. When your app starts running for the first time, its code is compiled to machine code which can then be read by the real machine. This means that the code isn’t compiled part by part at run time. This enhances the install time of the app while reducing the battery usage.

ART是一种提前(AOT)虚拟机。 那是什么意思呢? 让我解释。 当您的应用程序首次开始运行时,其代码将编译为机器代码,然后由真实机器读取。 这意味着在运行时不会部分地编译代码。 这可以延长应用程序的安装时间,同时减少电池消耗。

In sum, you write an app then compile it to binary code which is read by the ART. Then the ART converts that code to native code which can be read by the device itself.

总之,您编写一个应用程序,然后将其编译为由ART读取的二进制代码。 然后,ART将该代码转换为可由设备本身读取的本机代码。

艺术与C ++ (ART & C++)

What if you write an Android app using Java but there is some C++ code that is in contact with the Java? What’s the effect of that C++ code on your app’s build process or run time? Not too much.

如果您使用Java编写了一个Android应用程序,但是有一些C ++代码与Java联系该怎么办? 该C ++代码对您应用的生成过程或运行时间有什么影响? 不会太多

The C++ code is compiled directly to the real machine code by its compiler. So, if you use C++ code, it will be packaged as machine-readable code in your package. The ART will not reprocess it while it converts the ART-readable code into machine-readable code at the first time usage. You don’t need to worry about this process. You’re only responsible for writing an interface which lets Java talk to C++. We’re going to talk about that soon.

C ++代码由其编译器直接编译为真实的机器代码。 因此,如果您使用C ++代码,它将作为机器可读代码打包在您的程序包中。 ART在第一次使用时会将ART可读代码转换为机器可读代码时不会对其进行重新处理。 您无需担心此过程。 您只负责编写一个接口,使Java与C ++通讯。 我们将很快讨论。

C ++构建过程 (C++ Build Process)

We now have to talk about the C++ build process. The source code (the .cpp and .h files) is turned into expanded source code by a preprocessor in the very first step. This source code contains a whole lot of code. While you can get the final executable file using a command like the above, it’s possible to cut the build steps with related flags. You can get the extended source by giving the -E flag to the g++ compiler. I have a 40867 line file for a 4 line ‘hello world’ .cpp source code.

现在我们要谈谈C ++的构建过程。 第一步,将源代码(.cpp和.h文件)转换为扩展的源代码。 此源代码包含大量代码。 尽管您可以使用上述命令获得最终的可执行文件,但可以使用相关标志来削减构建步骤。 您可以通过将-E标志提供给g ++编译器来获取扩展源。 我有一个40867行文件,其中包含4行“ hello world” .cpp源代码。

Use g++ -E hello.cpp -o hello.ii in order to get the extended source code.

使用g ++ -E hello.cpp -o hello.ii以获得扩展的源代码。

The second one is the actual compilation step. The compiler compiles our code to obtain an assembler file. So, the real compilation yields an assembler file, not the executable. This file is assembled by an assembler. The resulting code is called object code. When we have multiple libraries aimed to be linked to each other we have many object codes. Those object codes are linked by a linker. Then we get an executable.

第二个是实际的编译步骤。 编译器编译我们的代码以获得一个汇编文件。 因此,真正的编译会产生一个汇编文件,而不是可执行文件。 该文件由汇编程序汇编。 所得的代码称为目标代码。 当我们有多个旨在相互链接的库时,我们就有许多目标代码。 这些目标代码通过链接器链接。 然后我们得到一个可执行文件。

There are two kinds of linking: dynamic and static.

链接有两种:动态链接和静态链接。

So now it’s time to go a bit deeper as we discuss pure C++ stuff.

所以现在是时候深入讨论纯C ++东西了。

The important thing: You can consider static linked libraries as a part of your code. So be careful when you link a library to your project. Because the library you use might not have a suitable license to be statically linked. Most open source libraries have been restricted to be used as dynamically linked.

重要的是:您可以将静态链接库视为代码的一部分。 因此,在将库链接到项目时要小心。 因为您使用的库可能没有合适的许可证来静态链接。 大多数开放源代码库已被限制只能用作动态链接。

From a technical point of view, a statically linked library is linked to the project at build time by the compiler. On the other hand, a dynamically linked library is linked by the operating system at run time. So you don’t need to distribute your project with the library code you use. You can use another project’s library or system library as well.

从技术角度来看,静态链接库在编译时由编译器链接到项目。 另一方面,动态链接库在运行时由操作系统链接。 因此,您不需要使用您使用的库代码来分发项目。 您也可以使用另一个项目的库或系统库。

Because of this fact dynamic linking may cause vulnerability in your project. While the security case is out of the scope of this post, however.

因此,动态链接可能会在您的项目中引起漏洞。 但是,尽管安全案例不在本文的讨论范围之内。

一些概念 (Some Concepts)

CMake和Gradle (CMake and Gradle)

If we want to add C++ code in our Android project, it’s good to use CMake to handle build operations. Remember the build process I have just introduced above? When you have a bunch of C++ libraries and source code it becomes more complicated to handle all of them. A tool like CMake makes it easier to carry out the build process.

如果我们想在我们的Android项目中添加C ++代码,则最好使用CMake处理构建操作。 还记得我上面刚刚介绍的构建过程吗? 当您有一堆C ++库和源代码时,处理所有这些库和源代码将变得更加复杂。 像CMake这样的工具可以使构建过程更加容易。

CMake will be available by default when you choose to include C++ support at the start of your project. Also you need to use a Gradle closure in order to package libraries to your APK.

当您选择在项目开始时包括C ++支持时,默认情况下CMake将可用。 另外,您需要使用Gradle闭包才能将库打包到APK。

阿比 (ABI)

As you know, Android is distributed for a variety of devices. Each device might have a different CPU architecture. When you develop an Android application that contains C++ code, you should care about the platforms on which your application will run.

如您所知,Android用于各种设备。 每个设备可能具有不同的CPU体系结构。 当开发包含C ++代码的Android应用程序时,应注意应用程序将在其上运行的平台。

Remember the C++ build mechanism I introduced above? The C++ code should be compiled as a library for each platform you target. You can compile the library for all the supported platforms, or you can choose to compile it for only one platform.

还记得我上面介绍的C ++构建机制吗? 应将C ++代码编译为目标平台的库。 您可以为所有受支持的平台编译该库,也可以选择仅针对一个平台编译它。

Please note that 64-bit ABI support will be mandatory with Android Pie release if you want to put your app in the Google Play Store.

请注意,如果您想将应用程序放入Google Play商店,则Android Pie版本必须提供64位ABI支持。

杰尼 (JNI)

This is the last thing I would like introduce you to concerning C++ usage in Android. As I mentioned previously, I’m introducing you these concepts considering you want to develop an app using Java.

这是我要向您介绍的有关Android中C ++用法的最后一件事。 如前所述,考虑到您想使用Java开发应用程序,我将向您介绍这些概念。

JNI is an abbreviation for Java Native Interface. It allows C++ and Java parts to talk to each other in the simplest terms. For example, if you want to call a function from C++ in Java, you should write a JNI interface for this purpose.

JNI是Java本机接口的缩写。 它允许C ++和Java部分以最简单的方式相互交谈。 例如,如果要使用Java从C ++调用函数,则应为此目的编写JNI接口。

The native-lib.cpp is the interface and it connects the C++ code to the Java code. In the above example, the only C++ code is the JNI itself. However, you can include the libraries you want to use and implement a function which calls them. This new function can be called from the Java part. So it works as a bridge in that way.

native-lib.cpp是接口,它将C ++代码连接到Java代码。 在上面的示例中,唯一的C ++代码是JNI本身。 但是,您可以包括要使用的库并实现调用它们的函数。 可以从Java部分调用此新函数。 因此它以这种方式充当桥梁。

想要尝试的事情 (Things to do in case you want to try it out)

Here, you have all the necessary and basic knowledge to use C++ in your Android project. If you want to give it a try, this is how to create a simple Android project with C++ code.

在这里,您具有在Android项目中使用C ++的所有必要和基本知识。 如果您想尝试一下,这就是使用C ++代码创建一个简单的Android项目的方法。

The below images show you the steps to start such a project. After finishing them, you might want to read over this post to modify and understand the mechanism more deeply.

下图显示了启动此类项目的步骤。 完成它们之后,您可能需要阅读这篇文章,以更深入地修改和理解该机制。

This post was only an introduction. Don’t forget there are many more things to learn. However, I aimed to introduce you the most important things about the concept of using C++.

这篇文章只是一个介绍。 不要忘记还有更多的东西要学习。 但是,我旨在向您介绍有关使用C ++概念的最重要的事情。

翻译自: https://www.freecodecamp.org/news/c-usage-in-android-4b57edf84322/

android开发使用c+

android开发使用c+_如何在Android项目中开始使用C ++代码相关推荐

  1. react前端显示图片_如何在react项目中引用图片?

    如何在react项目中引用图片?本文码云笔记将为大家整理在react项目中插入图片以及背景图片的方法,希望对需要的小伙伴提供一些参考. 在react项目中插入图片以及背景图片的方法共有2种: 1.im ...

  2. vfp生成菜单时文件不存在_如何在VFP项目中创建菜单

    接上面两篇文章! 本文,我们来学习一下如何在VFP项目中创建菜单! 打开上文所讲的简单的项目文件,切换到"其他"选项卡! 如上图,选择"菜单"一项,之后点击&q ...

  3. android 文本后图标_如何在Android中更改文本,图标等的大小

    android 文本后图标 Let's face it: no matter how good the screens are on our phones and tablets, the text ...

  4. android 调出键盘表情_如何在Android的G板键盘中搜索表情符号和GIF | MOS86

    老实说,这里是1010mh1112 GIF和表情符号是新形式的沟通.像他们似乎愚蠢的,他们以某种方式添加了一个额外的层次,我们与朋友和家人通过文本或即时消息交互的方式,否则可能会干燥.虽然表情符号长期 ...

  5. android开发 转跳功能,如何在Android中利用Intent实现一个页面跳转功能

    如何在Android中利用Intent实现一个页面跳转功能 发布时间:2021-02-20 17:06:31 来源:亿速云 阅读:113 作者:Leah 本篇文章为大家展示了如何在Android中利用 ...

  6. react本地储存_如何在React项目中利用本地存储

    react本地储存 以及为什么要这么做. 本地存储是现代Web浏览器固有的Web API. 它允许网站/应用程序在浏览器中存储数据(简单和有限),从而使数据在以后的浏览器会话中可用. 在开始学习本教程 ...

  7. android 更改软键盘_如何在Android的Google键盘上更改声音和振动

    android 更改软键盘 Tactile feedback from a touch screen keyboard is crucial, in my opinion, but I don't l ...

  8. android xml黑体字_如何在 Android 上使用思源黑体作为系统字体?

    展开全部 知道我的答案和这个答案崩溃了,我对一个非常详细的答案印象深刻,但62616964757a686964616fe59b9ee7ad9431333365643631是我找不到它.我一直在想如何评 ...

  9. android 控件发光_如何在android中的按钮周围制作动画/常量发光效果?

    的按钮有一个背景绘制的图像,我只是想拥有它周围的发光效果,使它有点与音乐播放 我已经搜查各地的多个线程共鸣,他们要么只是一个ImageView的,或在压制,或位图,所以不一定是我寻求 这里是我的按钮X ...

最新文章

  1. 模拟电路人工智能神经网络的前景
  2. ROS知识【11-1】:建立用户自己的工作空间、功能包
  3. python 基础(十)
  4. 最全知识点总结!| 大数据学习路线指南
  5. 计算机网络中缓存技术,编程达人
  6. Activiti 基础概念
  7. react 组件名称重复_设计可重复使用的React组件
  8. h5文字垂直居中_基于两款开源 h5 媒体编辑器定制一套海报系统
  9. Flex 国际化使用
  10. 微盟数据已经全面找回 并公布商家赔付计划
  11. NBU备份Vmware
  12. switch日版有中文吗_原神Switch日版可以玩吗
  13. lsdyna如何设置set中的node_list_为 Windows PowerShell 设置 User Alias (命令别名)
  14. 在Excel中用VB批量生成二维码
  15. Python入门 类型转换
  16. [BZOJ1922]大陆争霸-最短路略微扩展
  17. c++之 std::tie
  18. 128g固态硬盘服务器,买美光就后悔 浦科特M5S 128G/SSD评测
  19. [Unity实战]一个简单的unity手写摇杆[入门级][手写demo][开箱可用]
  20. Android_基于g-sensor的坐下动作的的识别

热门文章

  1. mmap无血缘关系进程间通信
  2. java高分面试指南:javamvc模式简单案例
  3. CSS清除默认样式,看完这篇彻底明白了
  4. 安卓开发面试书籍,全世界都在问Android开发凉了吗?建议收藏
  5. .net core 杂记:用Autofac替换内置容器
  6. net core 获取网站目录
  7. 软件安装(JDK+MySQL+TOMCAT)
  8. View,SurfaceView,SurfaceHolder
  9. 11个JavaScript颜色选择器插件
  10. asp.net MD5加密函数(c#)