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+