[10th day of the Turn group] | ANDROID6.0.0_R1 source code compilation and POC program compilation

Source: Internet
Author: User

2018-05-23

ANDROID6.0.0_R1 Source Code Compilation

Brief description: Android source code compiled four processes: 1. source code download; 2. Build the compilation environment; 3. Compile the source; 4 run. The following will also be described in this procedure.

Host Environment

Ubuntu 16.04 LTS

Android 6.0.0_R1

Open JDK 7

SOURCE download

Because of the reason of a wall, here we use the domestic mirror source to download.
At present, the available image sources are generally hkust and Tsinghua, the specific use of similar, here I choose the Tsinghua University mirror to explain. (Reference: Hkust source, Tsinghua Source)

Google uses repo to manage Android source code, so download the Repo tool first.

Implement the download and installation of the Repo tool by executing the following command:

1 mkdir ~/bin2 path=~/bin: $PATH3 Curl https://  Storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo4chmod a+x ~/bin/repo

Create source folder  

Everyone familiar with git should know that we need to create a corresponding warehouse locally for the project. Again, here we create a folder for the code to manage. Here I created the source folder under the current user directory, and all the downloaded source code and the compiled product are placed here. The command is as follows:

1 mkdir Source 2 CD source


Initializing the warehouse

We use the source folder above as the repository, and now we need to initialize the repository. By executing the Initialize Warehouse command, you can obtain the most recent code on the AOSP Project master and initialize the repository, with the following command:

1 Repo init-u https://aosp.tuna.tsinghua.edu.cn/platform/manifest-b android-6.0.0_r1

(Aosp the current list of all branches of the project see: Branch list) The-b parameter specifies to get a specific Android version, here we specify ANDROID-6.0.0_R1.

Synchronize source to Local

Once the repository is initialized, you can begin to formally synchronize the code locally, with the following command:

1 Sync

Save the file in the source directory, that is, our source directory, and then execute the script, peace of mind waiting for the source download completed, it is expected to take about 3 hours to download. For network reasons, there are multiple errors during the synchronization of the code using Repo Sync, where we use a shell script to automatically retry when the synchronization fails:

1#!/bin/Bash2 Echo "======start Repo sync======"  3RepoSync# download Android source code for the first time4          while[ $? !=0]; Do5         Echo "======sync failed, Re-sync again======"  6         Sleep 27RepoSync# If it goes wrong, call repo sync again after 2 seconds to download Android source code8          Done

Note: Because the. Repo directory is a hidden directory, you do not see anything until the download is complete.

Building a compilation environment

Hardware requirements:

64-bit operating system can only compile more than 2.3.x version, if you want to compile 2.3.x below, then requires 32-bit operating system.

Software Requirements:

Operating system, in AOSP open source, the main branch is developed and tested using Ubuntu long-term versions, so it is also recommended that you compile with Ubuntu, below we list different versions of Ubuntu to compile those Android versions:

Android version

Compile the required Ubuntu minimum version

Android 6.0 to AOSP Master

Ubuntu 14.04

Android 2.3.x to Android 5.x

Ubuntu 12.04

Android 1.5 to Android 2.2.x

Ubuntu 10.04

JDK version,

Android version

Compile the required JDK version

Aosp's Android Mainline

OpenJDK 8

Android 5.x to Android 6.0

OpenJDK 7

Android 2.3.x to Android 4.4.x

Oracle JDK 6

Android 1.5 to Android 2.2.x

Oracle JDK 5

More specific can be see: Google source code compilation requirements

Installing the JDK

Since we are installing android6.0, we need to use OpenJDK7, but only openjdk8 and OPENJDK9 installations are supported in the Ubuntu 15.04 and later versions of the online installation library. Therefore, installing OPENJDK7 on Ubuntu16.04 requires executing the following command:

1 sudo add-apt-repository ppa:openjdk-r/ppa2sudo apt-get update3  sudoinstall openjdk-7-jdk

To configure OPENJDK, open the/etc/profile file:

1 sudo gedit/etc/profile

Append the following code at the end:

1 # jdk Path config 2 Export java_home=/usr/lib/jvm/java-7-openjdk-amd643 export Jre_home=${java_ home}/JRE4 export classpath=.:${java_home}/lib:${jre_home}/lib5 Export path=${ Java_home}/bin: $PATH

Modify the/etc/profile file restart to take effect:

1 source/etc/profile

Check that the OPENJDK configuration is correct:

1 java-version

Configuration success such as:

Installation Environment dependent: (16.04 environment dependent)

1 sudoApt-getInstalllibx11-dev:i386 libreadline6-dev:i386 Libgl1-mesa-dev g++-Multilib2 sudoApt-getInstall-y git Flex bison gperf build-essential libncurses5-dev:i3863 sudoApt-getInstallTofrodos Python-markdown libxml2-utils Xsltproc zlib1g-dev:i3864 sudoApt-getInstallDpkg-dev LIBSDL1.2-dev libesd0-Dev5 sudoApt-getInstallGit-core GnuPG Flex Bison gperf build-Essential6 sudoApt-getInstall ZipCurl Zlib1g-devGCC-multilib g++-Multilib7 sudoApt-getInstalllibc6-dev-i3868 sudoApt-getInstallLib32ncurses5-dev X11proto-core-dev libx11-Dev9 sudoApt-getInstallLibgl1-mesa-dev libxml2-utils XsltprocUnzipM4Ten sudoApt-getInstallLib32z-dev CCache

Note: Different Ubuntu requires different dependencies and must be installed correctly.

Modify the source code:

To modify the Source/art/build/android.common_build.mk file, navigate to line 75 and the following code:

1 ifneq ($ (Without_host_clang),true)

Switch

1 ifeq ($ (Without_host_clang),false)

The purpose of the modification is to turn off the clang compilation option.

Upon completion of the above work, the basic preparatory work has been completed. The following is a formal compilation step.

Initialize the compilation environment:

(The current directory is in the source directory containing Android source code)

Make sure that the process is complete, and then we need to initialize the compilation environment

1) Add at the end of the. bashrc file: Export User_ccache = 1

1 echo export user_ccache=1 >> ~/.BASHRC

2) To improve compilation efficiency, set the compiler cache:

1 prebuilts/misc/linux-x86/ccache/ccache-m 50G

3) Then import the environment variables and other parameters for compiling the Android source code:

1 Source Build/envsetup.h

The execution command results are as follows:

It is not difficult to find that the command simply introduces other execution scripts, and what these scripts do are not currently covered in this article.
Once the command has been executed successfully, we will get some useful commands, such as the lunch command to be used at the bottom.

Compiling source code

Execute Lunch command

1 Lunch

The console lists all the compilation targets, as follows:

Choose Aosp_arm-eng here, which represents the engineer version of the ARM architecture, with maximum permissions (root), plus a number of debug tools.

After entering 1 here, some environment variable information is output:

Start compiling

Code compilation through the make directive, which sets the number of threads participating in the compilation by using the-J parameter to improve compilation speed. For example, here we set 8 threads to compile at the same time:

1  make-j8

If all goes well, after a few hours, it can be compiled. See # # # Make completed successfully (01:18:45 (HH:MM:SS)) # # #表示你编译成功了. It is important to note that the number of threads involved in compiling is not as good as possible. This is usually determined by the core of your machine Cup: core*2, which is twice times the core of the current CPU. For example, my current notebook is a dual core four thread, so according to the formula, the fastest compilation can be make-j8.
(View related CPU information via Cat/proc/cpuinfo)

Running the emulator

Once the compilation is complete, the Android virtual machine can be run with the following command:

1 source Build/envsetup. SH 2 Lunch (Select the target version you just set, like here I chose 1) 3 Emulator

No accident, a little wait, you will see the running interface: like Lunch,emulator these commands belong to envsetup.sh, if you did not leave the terminal just compiled source, direct input emulator is also possible.

Android source code to complete the compilation, I encountered a number of problems during the compilation process, mostly rely on no full, online check 16.04-Pack Android6 article, to re-rely on it again.

POC Source code compilation

This is for the Android source code is dependent on the compilation of programs, because the POC program to compile most to simulate the client's underlying and system services communication, need to call some of the client's underlying functions, as follows:

If the android.mk inside the configuration header file and library file path, it will be very troublesome, and the header files are nested, not very good configuration, I also did not find the configuration library file path parameters, and finally chose the source compilation environment of the MMM instructions to compile.

Here is the compile step:

1) Create a directory containing Poc.cpp and android.mk.

The content in Android.mk is:

1 local_path  : =  $ (call  my-dir)2include  $ (clear_vars)  3 local_module  : =  POC4 local_module_tags  : =   Optional5 local_src_files  : =  poc.  CPP6 local_cflags  + =  -march=armv47 local_shared_libraries  : =  libbinder  libutils  libmedia  libstagefright_foundation8 include  $ (build_executable)

Most of these parameters are introduced in the Official NDK Starter guide, which says two:

Local_module_tags: =optional, which means that it is compiled under any version (Usr,debug,eng).

Include $ (build_executable), which represents the build executable file.

Local_cflags + =-march=armv4, which is equivalent to specifying the-march parameter at compile time, GCC will not compile with compatible instructions, but instead uses its specific instruction set to generate binary code based on the specified CPU structure. Therefore, when you are sure that the compiled program will only run in a particular environment, you can use the-march parameter to specify the CPU architecture so that the compiler can optimize the instructions based on your CPU architecture, and the consequence of this designation is that if you run the program on another machine, It is possible to get illegal instruction run error.

2) Enter the Android source directory, initialize the source code compilation environment

1 CD Source (Android source directory) 2 Source Build/envsetup. SH 3 1

Mmm belongs to the envsetup.sh directive, which compiles all modules in the specified directory.

3) Compiling with MMM

1 mmm androidwp/jni (poc.cpp directory)

The advantage of using MMM is that it automatically looks for header files and library file paths, compiles the module to be compiled as an internal module of Android, and outputs it to out. So you just need to specify the name of the shared library in the android.mk.

Note: In the first few times with the MMM compilation, it will default to do clean operation, will be the Android source code that you compiled and installed all remove, at present I just can recompile again Android source code, but this soon, More is copy and install operation, do not panic.

Specific MMM command details did not delve into, interested in the Internet to see the source analysis.

After compiling, use the ADB to push the program to the Android machine and run it.

Reference:

51718187

51487585

[10th day of the Turn group] | ANDROID6.0.0_R1 source code compilation and POC program compilation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.