Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The principle of Android source code compilation and what preparation is there?

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

In this issue, the editor will bring you about the principle of Android source code compilation and what preparation there is. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

As you all know, Android is open source and can be downloaded from Android Open Source Project (click on the open link). For the process and method of downloading, you can visit the above web page to view detailed instructions.

First of all, we should have some understanding of how Android compiles. Most of the ordinary Android application development is developed in eclipse. In eclipse, Android Project is compiled by installing the ADT plug-in in eclipse. This compilation method is different from that under the Liunx system.

Under the Liunx system, the Android source code is compiled through make file (Android.mk). That is, during the compilation process, the compile command looks for the existence of an Android.mk file in each folder, and if so, the system compiles according to the compilation rules in the Android.mk file.

"jdk version does not match"

As we all know, the command compiled by Android is make-j * (* represents the number of kernels of CPU). After executing make-J*, the file that executes is build/core/main.mk. Before starting compilation, the system is checked to see if it meets the requirements of compilation. For example, check whether the system installs jdk and the version of jdk and so on.

Different Android versions have different requirements for jdk versions. This article will take android-4.1.2_r1 as an example to explain.

The jdk version specified in Android 4.1 is 1.6, that is, compilation will not continue if the jdk version of the Linux system is not installed.

The following message appears at the terminal:

You are attempting to build with the incorrect version

Your version is: 1.7.0_21.

The correct version is: Java SE 1.6.

The solution to this problem is to modify the file in the Android source code and change its specified version number to the version number of the installed jdk on this machine.

Modify the file:

Android-4.1.2_r1/build/core/main.mk

# Check for the correct version of javajava_version: = $(shell java-version 2 > & 1 | head-n 1 | grep'^ java. * ["] 1\ .6 [\. "$]') ifneq ($(shell java-version 2 > & 1 | grep-I openjdk),) java_version: = endififeq ($(strip $(java_version) ) $(info * *) $(info You are attempting to build with the incorrect version) $(info of java.) $(info $(space)) $(info Your version is: $(shell java-version 2 > & 1 | head-n 1).) $(info) The correct version is: Java SE 1.6.) $(info $(space)) $(info Please follow the machine setup instructions at) $(info $(space) $(space) https://source.android.com/source/download.html)$(info * * *) $(error stop) endif

If the jdk version installed on this machine is 1.7, you only need to set the

Java_version: = $(shell java-version 2 > & 1 | head-n 1 | grep'^ java. * ["] 1\ .6 [\." $]')

Modify to

Java_version: = $(shell java-version 2 > & 1 | head-n 1 | grep'^ java. * ["] 1\ .7 [\." $]')

That's it.

Similarly, the version number of javac needs to be changed.

# Check for the correct version of javacjavac_version: = $(shell javac-version 2 > & 1 | head-n 1 | grep'["] 1\ .6 [\. "$$]') ifeq ($(strip $(javac_version)) ) $(info * *) $(info You are attempting to build with the incorrect version) $(info of javac.) $(info $(space)) $(info Your version is: $(shell javac-version 2 > & 1 | head-n 1).) $(info) The correct version is: 1.6.) $(info $(space)) $(info Please follow the machine setup instructions at) $(info $(space) $(space) https://source.android.com/source/download.html)$(info * * *) $(error stop) endif

Set

Javac_version: = $(shell javac-version 2 > & 1 | head-n 1 | grep'["] 1\ .6 [\." $]')

Modify to

Javac_version: = $(shell javac-version 2 > & 1 | head-n 1 | grep'["] 1\ .7 [\." $]')

"frequently encountered problems in compilation"

After the problem with the jdk version is resolved, other errors (mainly command not found) may occur as the compilation progresses, causing the compilation to abort.

If you compile in the unmodified source code, most of the errors are due to the lack of necessary software in the system.

After my own practice, the following software will be used in the compilation:

Bison

Zlib1g-dev

Flex

Libncurses-dev

Gperf

Xsltproc

Build-essential

Glossy +

G++-multilib

Ia32-libs

Libxml2-utils

The solution is to install the above software in advance before compilation, with the following installation command:

Sudo apt-get install bisonsudo apt-get install zlib1g-devsudo apt-get install flexsudo apt-get install libncurses-devsudo apt-get install gperfsudo apt-get install xsltprocsudo apt-get install build-essentialsudo apt-get install g++sudo apt-get install g++-multilibsudo apt-get install ia32-libssudo apt-get install libxml2-utils

After installing the above software, recompile it, and you can compile normally. The whole compilation process of an ordinary 4-core computer will take about 4 hours.

"out folder"

When the compilation is successful, an out folder is generated in the android-4.1.2_r1 directory. The files in out are compiled.

The following is a brief explanation of the composition of the out folder.

There are two folders under out

Host

Target

There is something we need in the target folder.

There are two more folders under target

Common

Product

The most commonly used common folder is to get the compiled java libraries. Because all methods or variables marked @ hide in sdk are inaccessible, you must use the library in the folder below when you need to access them.

Out/target/common/obj/JAVA_LIBRARIES

The product folder, as its name implies, is the location where the compiled files are stored.

In standard Android source code, the generated folder is called generic. After the manufacturer customizes, the name of this folder will change and become the model of the phone.

Within the generic folder, there are two places that are important.

One is system.img.

This is the image file of the compiled system folder, which is essential for the production of ROM.

The other is the system folder.

Here are the apk files we need, which are stored in the system/app folder.

"odex and dex"

It is important to note that the apk under the sytem/app folder includes two files by default:

Launcher2.apk

Launcher2.odex

This is because instead of typing the classes.jar and the resource file in the same file (classes.dex) during compilation, the odex file is generated separately.

The way to turn off odexization is

Modify android-4.1.2_r1/build/core/package.mk

Ifneq (true,$ (WITH_DEXPREOPT)) LOCAL_DEX_PREOPT: = elseifeq (, $(TARGET_BUILD_APPS)) ifneq (, $(LOCAL_SRC_FILES)) ifndef LOCAL_DEX_PREOPT#feizl mod start#LOCAL_DEX_PREOPT: = trueLOCAL_DEX_PREOPT: = false#feizl mod endendifendifendifendififeq (false,$ (LOCAL_DEX_PREOPT)) LOCAL_DEX_PREOPT: = endif

Set

LOCAL_DEX_PREOPT: = true

Modify to

LOCAL_DEX_PREOPT: = false

Similarly, if you want framework not to generate odex files, you can modify the following files

Android-4.1.2_r1/build/core/java_library.mk

Ifneq (true,$ (WITH_DEXPREOPT)) LOCAL_DEX_PREOPT: = elseifeq (, $(TARGET_BUILD_APPS)) ifndef LOCAL_DEX_PREOPT#feizl mod start#LOCAL_DEX_PREOPT: = trueLOCAL_DEX_PREOPT: = false#feizl mod endendifendifendififeq (false,$ (LOCAL_DEX_PREOPT)) LOCAL_DEX_PREOPT: = endif

Set

LOCAL_DEX_PREOPT: = true

Modify to

LOCAL_DEX_PREOPT: = false

After the modification, re-execute the compilation command, but be careful to delete the files under system/app and system/framework in advance.

After the compilation is complete, the files in the above folder are classes and resource files.

The above is the editor for you to share the Android source code compilation principles and what preparation, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report