android開發使用c+_如何在Android項目中開始使用C ++代碼

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+

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/392804.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/392804.shtml
英文地址,請注明出處:http://en.pswp.cn/news/392804.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

PowerShell與活動目錄

自從發布以來,Windows PowerShell已經成為Windows自動化平臺的選擇。它的強大和靈活已經在許多環境中被許多Windows技術所證明。不幸的是,在活動目錄支持方面,PowerShell 1并沒有什么可以炫耀。從基礎角度,微軟提供了ADSI“類型加…

408計算機組成原理有匯編嗎,2021考研408計算機組成原理習題:計算機系統概述

10月是2021考研學子們備考的突破提升階段,我們在復習專業課時,需要結合一定量的練習題來查漏補缺。接下來,小編為計算機考研考生們,帶來了408統考計算機組成原理習題:計算機系統概述,供考生參考。2021考研408計算機組成…

react 文本框_React自動完成文本框

react 文本框In this React tutorial for beginners you will learn to create a basic React app and an autocomplete text box React component.在這個面向初學者的React教程中,您將學習創建一個基本的React應用程序和一個自動完成的文本框React組件。 This vid…

MyBatis-Plus入門Demo詳解

一.簡介: 引用官方文檔(本文主要參考官方文檔示例): MyBatis-Plus(簡稱 MP)是一個 MyBatis 的增強工具,在 MyBatis 的基礎上只做增強不做改變,為簡化開發、提高效率而生。 愿景 我們的愿景是成為 MyBatis 最好的搭檔,就…

RHEL 5基礎篇—常見系統啟動類故障

常見系統啟動類故障 在linux系統的啟動過程中,涉及到MBR主引導記錄、GRUB啟動菜單、系統初始化配置文件inittab等各方面,其中任何一個環節出現故障都有可能會導致系統啟動失敗。因此一定要注意做好相關文件的備份工作。 1、MBR扇區故障 MBR引導記錄位…

hcharts生成圖表

借助hcharts插件,可以很方便地在模板頁面中生成圖表。類似插件還有echarts。 補充。。。 轉載于:https://www.cnblogs.com/Forever77/p/11144346.html

css empty_何時使用:empty和:blank CSS偽選擇器

css emptyI made a terrible mistake when I tweeted about :empty and :blank a while ago. I said that :empty wasn’t useful, and :blank is much more useful than :empty.不久前我在Twitter上發布:empty和:blank時,我犯了一個嚴重的錯誤。 我說過:empty沒用&…

浙江大學計算機系統結構,高級計算機體系結構-浙江大學計算機系統結構室.pdf...

高級計算機體系結構-浙江大學計算機系統結構室高級計算機體系結構陳文智 浙江大學計算機學院chenwzzju.edu.cn2014年9月11.1 計算機技術發展綜述(1)?1946年: 在二次世界大戰期間研制成功的世界上第一臺電子計算機ENIAC(Electronic Numerical Intergrator andCalculator)正式對…

PVS 6.1 Configuring Services Failed

好久沒有更新了,嘿嘿,更新一個。 項目中遇到一個問題,PVS安裝到最后一步報錯,如下圖: 環境:PVS 6.1,數據庫是SQL Server 2005 SP4 查了一下文檔,PVS 6.1支持SQL Server 2005 SP4 排查…

javascript動態創建table

function createTable(parentNode,headres,datas){//創建表格var table document.createElement("table");//將表格追加到父容器中parentNode.appendChild(table);//設置table的樣式table.cellSpacing 0;table.cellPadding 0;table.border "1px";//創建…

leetcode 234. 回文鏈表(快慢指針+鏈表倒置)

請判斷一個鏈表是否為回文鏈表。 示例 1: 輸入: 1->2 輸出: false 示例 2: 輸入: 1->2->2->1 輸出: true 代碼 /*** Definition for singly-linked list.* public class ListNode {* int val;* ListNode next;* ListNode(int x) { val x; }* }*/…

面試小問題——Object中有哪些常用方法?

一、equals方法 Object類中的equals方法用于檢測一個對象是否等于另外一個對象。Java語言規范要求equals方法具有下面的特性: (1)自反性:對于任何非空引用x,x.equals(x)應該返回true (2)對稱性&…

職稱計算機證書 評中級職稱,軟考證書如何申請評職稱及職稱申請流程的詳細介紹...

我們很多考友參加軟考。比如信息系統項目管理師和系統集成項目管理工程師考試,目的都是為了評職稱,那么在拿到軟考證書后,很多人最關心的一個問題就是關于職稱評聘問題,今天就以軟考證書如何申請評職稱及職稱申請流程的詳細介紹&a…

播客51:媽媽可以編碼的創始人埃里卡·彼得森(Erica Peterson)

On todays episode of the freeCodeCamp.org podcast, Abbey Rennemeyer chats with Erica Peterson, a founder, entrepreneur, and mother of two who lives and works in Pittsburg, Pennsylvania.在freeCodeCamp.org播客的今天節目中,Abbey Rennemeyer與Erica P…

leetcode 1024. 視頻拼接(dp/貪心)

你將會獲得一系列視頻片段,這些片段來自于一項持續時長為 T 秒的體育賽事。這些片段可能有所重疊,也可能長度不一。 視頻片段 clips[i] 都用區間進行表示:開始于 clips[i][0] 并于 clips[i][1] 結束。我們甚至可以對這些片段自由地再剪輯&am…

java實現時鐘方法匯總

import java.awt.Dimension; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Timer; import java.util.TimerTask;import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; //第一種比較…

js中注冊標識符流程

注冊分為三個階段:分別是注冊階段,函數處理階段,變量處理階段;這三個階段有先后順序的。(注:這三個階段的名字沒有權威性,是作者為了方便記憶自己起的名字) 注冊階段的特征 1.此時不…

jsp論壇網站模版_網站關鍵詞優化怎么做

說到網站關鍵詞優化,大多企業都很陌生,建站公司說的關鍵詞優化頭頭是道。跟聽天書似的,51商務網小編為大家總結的網站優化方法希望可以幫到大家,首先要說的是做網站優化第一點就是要有耐心,如果很長時間沒有收錄的話&a…

feature功能_利用feature-u V1釋放基于功能的JS開發的強大功能

feature功能This article is an introduction to a new JS library called feature-u, that facilitates feature-based development in your React project.本文是對新的JS庫(稱為feature-u )的介紹,該庫促進了React項目中基于功能的開發 。 Note: On 8/14/2018 f…

虛擬實驗工場大學計算機實驗報告答案,虛擬實驗實驗報告 - 實驗報告 - 書業網.doc...

虛擬實驗實驗報告 - 實驗報告 - 書業網虛擬實驗實驗報告 - 實驗報告 - 書業網篇一:虛擬實驗報告第一章 文獻綜述1.1 丙酮酸脫氫酶概述丙酮酸脫氫酶復合體(Pyruvate Dehydrogenase Complex)催化丙酮酸不可逆的氧化脫羧轉化成乙酰輔酶A。該復合體是糖酵解的關鍵限速酶…