1 Star 1 Fork 4

无心love / CompileAndroidSource

forked from 云加 / CompileAndroidSource 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

编译Android源码

仓库归档

##如何观看视频教程

  1. 如何下载文件
  2. 如何观看梦宇录制的视频教程
  3. 在线观看视频

说明

本教程演示如何编译 Android 源码,目的是教会你如何基于aosp(Android open source project)定制Android系统。教程演示所使用的手机是Nexus6,但本教程是通用教程,并不局限于Nexus6,只要你有合适的驱动包,你就可以面向任意机型做定制。

推荐学习机型

  • Nexus 5X
  • Nexus 6
  • Nexus 6P
  • Nexus 9
  • Nexus Player
  • Pixel
  • Pixel C
  • Pixel XL

基本硬件要求

名称 需求
电脑CPU 双核4线程2GHz或者更高配置的CPU
电脑内存 16G物理内存
电脑磁盘 至少200G可用的磁盘空间,固态硬盘更好
电脑操作系统 Ubuntu 16.04
Android手机 Google Nexus 6

准备Ubuntu环境

  1. 全新安装Ubuntu 16.04 操作系统(安装到虚拟机也可以,至少为虚拟机分配16G内存,否则容易出错,这可能要求你的宿主计算机具有24G以上的物理内存)
  2. 执行如下命令进行更新
    sudo apt-get update
  3. 执行如下命令安装jdk8
    sudo apt-get install openjdk-8-jdk
  4. 执行如下命令安装依赖项
    sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev gcc-multilib g++-multilib libc6-dev-i386 lib32ncurses5-dev x11proto-core-dev libx11-dev lib32z-dev ccache libgl1-mesa-dev libxml2-utils xsltproc unzip
  5. 如果想使用SELinux tool 则还应该安装 python-networkx ,通过执行如下命令进行安装
    sudo apt-get install python-networkx
  6. 如果使用LDAP,则还应该安装 libnss-sss:i386 ,通过如下命令进行安装
    sudo apt-get install libnss-sss:i386
  7. Ubuntu默认不允许普通用户使用USB,所以还应该配置Android USB设备能够被当前用户访问到,则需要执行如下命令
    wget -S -O - http://source.android.com/source/51-android.rules | sed "s/<username>/$USER/" | sudo tee >/dev/null /etc/udev/rules.d/51-android.rules; sudo udevadm control --reload-rules
    注意:执行该命令需要翻墙

安装并配置代码管理工具 repo

  1. 在当前用户目录下创建 bin 目录,可通过如下命令创建
    mkdir ~/bin
  2. 通过如下命令下载 repo ,并将其放在 ~/bin 目录中
    curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
    注意:执行该命令需要翻墙
  3. 通过如下命令给repo添加可执行权限
    chmod a+x ~/bin/repo
  4. 通过如下命令初始化git用户名
    git config --global user.name "Your Name"
  5. 通过如下命令初始化git邮箱
    git config --global user.email "you@example.com"

下载Android源代码

  1. 创建一个目录用于存放Android源码,如我存放的路径是 /Work/AndroidSource/Nexus6/source ,并使用终端进入该目录
  2. 通过如下代码初始化 Nexus 6 源码仓库
    repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.1_r28
    其中 -b 参数指明将下载的代码分支 如果要下载master分支,则不需要加任何参数,则执行的命令如下
    repo init -u https://android.googlesource.com/platform/manifest
    您也可以根据自己的需要来选择合适的分支进行下载,分支与匹配机型参考表网址是
    http://source.android.com/source/build-numbers.html#source-code-tags-and-builds
    注意:访问该网址需要翻墙
  3. 执行如下命令下载(同步)代码
    repo sync
    目前默认使用4个任务同步代码,如果你的机器足够好,可以指定更多的任务,如下命令演示使用8个任务同步代码
    repo sync -j8
    说明:同步代码可能需要很长时间,你需要一个较好的网络环境和一个较稳定的翻墙工具,按平均状况来看大约需要2天时间,视频教程里有介绍如何使用我已经下载好的用百度云共享的代码,好的网络环境和好的电脑配置可在2小时内完成该任务

配置厂商提供的驱动

  1. 访问页面 https://developers.google.com/android/drivers 查找配置自己机型的驱动(我这里查找的是Nexus 6的驱动)
    注意:访问该页面需要翻墙
  2. 下载 Nexus6 驱动包,如下图所示这三项均需下载
    驱动包截图
  3. 驱动包文件都是压缩包,在压缩包中包括一个sh文件,该sh文件为自解压脚本,分别将这3个sh文件放在Android源码树的根目录,比如/Work/AndroidSource/Nexus6/source,并执行依次执行这3个文件以自动配置驱动包,配置完成后将在源码树根目录中多出一个vendor目录。
    注意:在执行3个sh文件时会呈现协议条款,请慢慢看,不要跳过I ACCEPT那一步,否则不解压

编译Android源码

  1. 执行如下命令进行清理
    make clobber
  2. 执行如下命令加载环境变量
    source build/envsetup.sh
  3. 执行如下命令选择构建目标
    lunch aosp_shamu-userdebug
    如果想选择其它目标,请直接执行 lunch ,将会呈现一个目标列表,可根据自己的手机型号进行选择,如果只想运行在模式器中,可以选择第一项或者直接执行 lunch aosp_arm-eng
  4. 执行如下命令进行编译
    make -j4
    该命令使用4个任务进行编译,如果电脑不够好,编译过程可能会持续10小时或更久,如果电脑足够好,也可以指定更多任务,如 make -j32 是指定32个任务进行编译。

编译必备的工具

说明:如果你已经安装Android sdk,也可以使用sdk内置的命令。

  1. 执行如下命令编译 adb
    make adb
  2. 执行如下命令编译 fastboot
    make fastboot

运行

  1. 进入手机 设置 -> 开发者选项 (如果没有该选项,请进入 设置 -> 关于手机,连续点击 Build number 7次以启用开发者模式),启用USB调试和OEM解锁
  2. 将手机通过USB连接电脑,执行命令 adb reboot bootloader 以进入手机的Bootloader模式,也可以通过手机硬键操作进入Bootloader模式,查看不同手机操作方式列表请访问 http://source.android.com/source/running.html注意:访问该网址需要翻墙)。
  3. 解锁Bootloader,执行命令 fastboot flashing unlock ,2014年或更老的手机请执行 fastboot oem unlock ,你必须通过手机屏幕确认该操作。
  4. 执行如下命令格式化缓存
    fastboot format cache
  5. 执行如下命令格式化用户数据
    fastboot format userdata
  6. 执行如下命令将系统写入手机
    fastboot flashall -w
    写入成功后手机将重启进入您编译的操作系统
  7. 如果您想长期安全的使用该手机,可以通过命令 fastboot flashing lock 锁定Bootloader,2014年或更老的手机请执行 fastboot oem lock ,之后进入手机系统,在开发者选项中取消OEM解锁。

#捐助

如果您觉得我的工作对您有所帮助,请支持我继续,谢谢。

捐助

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: You must give any other recipients of the Work or Derivative Works a copy of this License; and You must cause any modified files to carry prominent notices stating that You changed the files; and You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "{}" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright 2017 plter Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

简介

本仓库演示一个通用的完整的编译Android源码的过程 展开 收起
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/wuxinlove/CompileAndroidSource.git
git@gitee.com:wuxinlove/CompileAndroidSource.git
wuxinlove
CompileAndroidSource
CompileAndroidSource
master

搜索帮助