一,学习android系统源码下载,编译的作用
1,可以自己 DIY 自己的rom系统,从系统层面,宏观的加深理解 android系统
2,编译自己的 userdebug(原生root权限) rom, 方便调试。
2,可以看到系统所有的 source code,修改自己感兴趣的部分,编译,运行后,可以看到效果;通过阅读源码解决问题。
3,学习源码中优秀的设计思想,架构,设计模式,代码风格;针对自己需要实现的功能,可以参考,联系人,电话,图库,短信,邮件,浏览器等系统app的实现。
二,关于AOSP
Android Open Source Project, 官网链接 aosp 官网
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > source_info.png 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > android_framework_details.png
该文档参考 AOSP官网指导,在 ubuntu14.04下,配置下载,编译源码的环境,同步源码,编译源码,运行到 nexus5X上。
当前可用最新源码
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > new _nsource.png
对于 nexus5X 设备可用的最新代码为分支为:android-7.1.2_r8
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > new-able.png
所以我们下载该分支代码即可,编译之后就可以运行到 nexus5X 上
三,简单说下,我们平时使用的 android 手机是如何研发出来的
android手机组成:硬件(CPU芯片,触屏,话筒,扬声器,相机,天线,电池,PCB,各种IC等) + 软件:android系统(内核,rom系统,第三方配件驱动等)
oem厂商基于某一平台(高通,联发科,展讯)规划好自己的产品,买来硬件(或者自己生产),组装,烧录系统(根据自己的硬件设备,配件,修改后的aosp,加上第三方驱动)。
现在的像高通等芯片厂商的集成度,越来越高,研制一部android手机的门槛越来越低,但是做一部,高性能,流畅,高度优化,美观,高用户体验的产品,还是需要投入大量资源的。
比如基于高通8953平台,研发一部android手机,8953平台就是骁龙625,高通会拿到 aosp源码,针对自己的平台加入相关的驱动,接口代码,厂商购买8953平台,可以拿到 高通修改后的aosp源码,平台说明文档,其他硬件接口文档等。如果厂商想使用莱卡的摄像头,三星的OLED曲面屏幕,这些配件厂商会提供相关的硬件驱动和接口文档,oem厂商集成到系统中,可以进行个性化定制,包括功能,性能,ui等。
下面是一个基于 高通8953平台的工程机:
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > IMG_20170410_191705.jpg 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > IMG_20170410_192516.jpg
推荐一本我觉得还不错的电子书,百度网盘链接 ANDROID系统服务开发_13706555.pdf 链接: https://pan.baidu.com/s/1nvAyxiL 密码: 2ucq
感兴趣的同学可以参考一下,该书为韩国android rom开发者所著,比较容易理解,有很多示意图。
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > book.png
四,编译环境配置
官网指导文档
1,硬件要求
64位;150G可用硬盘;如果使用虚拟机 最少16G RAM
2,软件要求
官方建议使用 长期支持的 ubuntu,其他的 LINUX 系统,mac 系统也可以,也可以在 windows 里面安装虚拟机;
个人建议,使用官方推荐系统 ubuntu,因为,有官方支持,文档资料较多,遇到问题,方便在网上寻找解决办法。
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > software.png
1> 本文档编译最新源码,使用 ubuntu14.04 必须安装的软件包有:
OpenJDK 8
aloe@aloe-pc:~
javac -version
javac 1.8.0_45-internal
aloe@aloe-pc:~
aloe@aloe-pc:~
git –version
git version 1.9.1
aloe@aloe-pc:~$ make –version
GNU Make 3.81
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.
There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.
This program built for x86_64-pc-linux-gnu
android 源码控制管理工具使用的是 git,google 把git封装为repo,repo就是一个 python脚本,源码编译系统使用 make工具,所以必须 需要 JDK,git,python,make
链接: https://pan.baidu.com/s/1cAKL1k 密码: x3dq GNU+MakeFile中文手册.pdf 关于android源码的 build的编译系统,推荐一本电子书,百度网盘链接.
2> OpenJDK 8 的安装
oracle jdk 安装,直接下载安装包,解压到 /usr/lib/jvm/ 目录下,目录不存在,自己创建,在 .bashrc 配置环境变量即可.
添加软件源: sudo add-apt-repository ppa:openjdk-r/ppa
更新源: sudo apt-get update
安装 openjdk-8-jdk sudo apt-get install openjdk-8-jdk
我安装后的 /usr/lib/jvm/目录如下:
total 52
drwxr-xr-x 7 root root 4096 2月 11 21:18 .
drwxr-xr-x 172 root root 20480 5月 7 12:25 ..
lrwxrwxrwx 1 root root 24 3月 23 2014 default-java -> java-1.7.0-openjdk-amd64
lrwxrwxrwx 1 root root 20 8月 12 2016 java-1.7.0-openjdk-amd64 -> java-7-openjdk-amd64
-rw-r–r– 1 root root 2439 2月 8 09:10 .java-1.7.0-openjdk-amd64.jinfo
lrwxrwxrwx 1 root root 20 4月 20 2015 java-1.8.0-openjdk-amd64 -> java-8-openjdk-amd64
-rw-r–r– 1 root root 2546 4月 20 2015 .java-1.8.0-openjdk-amd64.jinfo
drwxr-xr-x 7 root root 4096 2月 11 21:18 java-7-openjdk-amd64
drwxrwxrwx 7 root root 4096 9月 18 2016 java-8-openjdk-amd64
drwxrwxrwx 8 aloe aloe 4096 3月 27 2013 jdk1.6.0_45
drwxrwxrwx 8 aloe aloe 4096 4月 11 2015 jdk1.7.0_79
drwxrwxrwx 8 aloe aloe 4096 4月 1 2016 jdk1.8.0_92
我需要编译不同版本的源码,所以安装了不同版本的 jdk,我需要使用那个版本的 jdk在用户目录下,配置aloe@aloe-pc:~$ vim .bashrc 即可:
export JAVA_HOME=/usr/lib/jvm/jdk1.6.0_45
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_79
export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64
export JAVA_HOME=/usr/lib/jvm/jdk1.8.0_92
export JRE_HOME= {JAVA_HOME}/lib: {JAVA_HOME}/bin: {CLASSPATH}:$PATH
export ANDROID_HOME=/home/aloe/develop/Sdk
export ANDROID_HOME=/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android/Sdk
export PATH=
ANDROID_HOME/platform-tools:$PATH
export REPO_HOME=/usr/local
export REPO_HOME=/home/aloe/local
export PATH=
PATH
export GRADLE_HOME=/home/aloe/develop/package/gradle-3.3
export PATH=
PATH
配置完成后,保存退出
source .bashrc 是环境变量生效
验证是否安装成功:
aloe@aloe-pc:~$ java -version
openjdk version “1.8.0_45-internal”
OpenJDK Runtime Environment (build 1.8.0_45-internal-b14)
OpenJDK 64-Bit Server VM (build 25.45-b02, mixed mode)
推荐两个常用命令:
aloe@aloe-pc:~
pwd 打印当前绝对路径
/home/aloe
3> 安装android编译环境 必要的其他依赖环境:
sudo apt-get install git-core gnupg flex bison gperf build-essential zip curl zlib1g-dev libc6-dev lib32ncurses5-dev x11proto-core-dev libx11-dev lib32readline6-dev lib32z1-dev libgl1-mesa-dev g++-multilib mingw32 tofrodos libsdl1.2-dev libesd0-dev libwxgtk2.8-dev build-essential zip curl libncurses5-dev zlib1g-dev valgrind libxml2-utils
直接复制到终端运行,即可.
4>.安装低版本GCC(gcc -v查看版本号,大于4.6.4的版本请降低GCC版本):
安装GCC-4.6 & G++4.6编译工具:
sudo apt-get install gcc-4.6 gcc-4.6-multilib g++-4.6 g++-4.6-multilib
配置GCC-4.6为高优先级,并切换默认的GCC版本为4.6:
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.6 50
sudo update-alternatives –install /usr/bin/gcc gcc /usr/bin/gcc-4.8 40
sudo update-alternatives –config gcc
配置G++-4.6为高优先级,并切换默认的G++版本为4.6:
sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-4.6 50
sudo update-alternatives –install /usr/bin/g++ g++ /usr/bin/g++-4.8 40
sudo update-alternatives –config g++
Tips:务必使用命令:
gcc -v
或
g++ -v
验证版本是否正确(必须为:version 4.6.4 )
aloe@aloe-pc:~$ gcc -v
gcc version 4.8.4 (Ubuntu 4.8.4-2ubuntu1~14.04.3)
5> 配置 adb 环境变量和 nexus5X 的驱动,方便调试
adb 环境变量直接把 sdk 里的adb目录配到 .bashrc 里即可:
export ANDROID_HOME=/home/aloe/develop/Sdk
export ANDROID_HOME=/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android/Sdk
export PATH=
ANDROID_HOME/platform-tools:$PATH
使用如下方法验证,是否配置成功:
aloe@aloe-pc:~
adb -version
Android Debug Bridge version 1.0.31
配置 nexus5X 的驱动:
aloe@aloe-pc:~$ lsusb
Bus 002 Device 003: ID 5986:0294 Acer, Inc
Bus 002 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 002 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 001 Device 003: ID 0bda:0129 Realtek Semiconductor Corp. RTS5129 Card Reader Controller
Bus 001 Device 004: ID 0461:4d80 Primax Electronics, Ltd
Bus 001 Device 002: ID 8087:0024 Intel Corp. Integrated Rate Matching Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
Bus 004 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 003 Device 002: ID 18d1:4ee7 Google Inc.
Bus 003 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub
得到 nexus5X的 厂商 ID 18d1 产品ID 4ee7
在 /etc/udev/rules.d 新建 99-android.rules
vim 99-android.rules
SUBSYSTEM==”usb”, ATTRS{idVendor}==”18d1”, ATTRS{idProduct}==”0fff”, MODE=”0666”, OWNER=”aloe” 写入.
OWNER=”aloe” 电脑用户名,一般为 terminal @ 前的名字
然后重启 udev 服务
sudo service udev restart
adb kill-server
adb start-server
验证是否配置成功
aloe@aloe-pc:/etc/udev/rules.d$ adb devices
List of devices attached
015b6734e9f634e6 device
出现这个,说明配置成功.
上面环境的配置,也可以参考官网文档: 官网文档, 也可以参考这两篇博客: Ubuntu14.04 U盘启动盘的制作、Ubuntu14.04操作系统的安装、android开发环境的配置和android源码下载、编译 ; ubuntu14.04 安装 open-jdk-1.8,下载编译 android nougat 7.1.1
五,下载 android系统源码
官方参考文档
1.下载 repo工具
repo是 android源码管理工具,是对git的封装,使用方法和 git 相似,repo是一个 python脚本文件
cd /usr/local/bin/
curl https://storage.googleapis.com/git-repo-downloads/repo > /usr/local/bin/repo
chmod a+x /usr/local/bin/repo
2.初始化仓库
创建 源码存放目录:
mkdir Nougat_7.1
cd Nougat_7.1
配置git账户
git config –global user.name “Your Name”
git config –global user.email “you@example.com”
配置完成后如下:
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ git config –global –list
user.name=rgchen
user.email=rgchen@neolink.com.cn
color.ui=auto
merge.tool=meld
diff.tool=meld
difftool.prompt=false
初始化仓库:
repo init -u https://android.googlesource.com/platform/manifest -b android-7.1.2_r8
3.同步代码
repo sync
或者 repo sync -j4 用4个线程下载
耐心等待,下载完成。如果中间 什么原因导致下载失败,重新执行 repo sync
注意:如果访问不了官网,或者代码下载特别慢,建议使用 清华大学的镜像资源 AOSP ,会快些
使用清华大学的镜像资源配置如下:
编辑 repo 脚本: 把 REPO_URL 一行替换成下面的:REPO_URL = ‘https://gerrit-google.tuna.tsinghua.edu.cn/git-repo’ 替换后如下:
import os
REPO_URL = os.environ.get('REPO_URL', None)
if not REPO_URL:
REPO_URL = 'https://gerrit-google.tuna.tsinghua.edu.cn/git-repo'
REPO_REV = 'stable'``` 初始化仓库: ```repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-7.1.2_r8 ```
然后 repo sync 同步源码
下载成功如下显示:
<div class="se-preview-section-delimiter"></div>
Fetching project platform/hardware/ti/omap3
Fetching project platform/system/sepolicy
Fetching project platform/prebuilts/ninja/linux-x86
Fetching project platform/external/neven
remote: Counting objects: 56, done.
remote: Compressing objects: 100% (56/56), done.
remote: Total 56 (delta 40), reused 0 (delta 0)
From https://aosp.tuna.tsinghua.edu.cn/platform/system/sepolicy
bf03096..fcfda81 master -> aosp/master
2c019b5..05bc716 nougat-mr1-cts-dev -> aosp/nougat-mr1-cts-dev
5a70547..562da9c nougat-mr1-cts-release -> aosp/nougat-mr1-cts-release
Fetching projects: 97% (515/530) Fetching project platform/external/regex-re2
Fetching project platform/external/junit
Fetching project platform/external/ant-glob
Fetching project platform/dalvik
Fetching project platform/hardware/qcom/display
Fetching projects: 98% (520/530) Fetching project platform/frameworks/opt/photoviewer
Fetching project platform/external/tagsoup
Fetching project platform/frameworks/wilhelm
Fetching project platform/packages/apps/TvSettings
Fetching project platform/external/fio
Fetching projects: 99% (525/530) Fetching project platform/frameworks/ex
Fetching project platform/hardware/invensense
Fetching projects: 100% (530/530), done.
Syncing work tree: 100% (529/529), done.
最新的源码 android-7.1.2_r8 50G左右. <div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ du -ah -d 1
39M ./docs
2.1M ./extract-qcom-bullhead.sh # nexus5X 高通驱动
200M ./device
51G ./.repo #和.git 类似,管理代码
145M ./system #Android初始化进程,蓝牙工具集——C语言
82M ./ndk
128K ./abi
4.5G ./external #android使用的一些开源的模组
3.2M ./platform_testing
67M ./libcore
31M ./bionic Android 标准C运行支持库
31M ./sdk #sdk及模拟器
92M ./extract-lge-bullhead.sh ## nexus5X LG驱动
47M ./art
1.7G ./frameworks #核心框架——java及C++语言
223M ./toolchain
323M ./development
44G ./out #编译完成后的代码输出与此目录
4.0K ./Makefile
1.5G ./tools
204M ./vendor #厂商定制代码
11M ./dalvik #dalvik JAVA虚拟机
900K ./pdk
0 ./Android.bp
15M ./build #Android的Build系统,存放系统编译规则及generic等基础开发包配置
319M ./developers
16K ./.classpath
12G ./prebuilts #x86和arm架构下预编译的一些资源
158M ./hardware #部分厂家开源的硬解适配层HAL代码
21M ./bootable #Android内核加载器参考
556K ./compatibility
4.0K ./.project
244K ./libnativehelper
0 ./bootstrap.bash
450M ./packages #应用程序包
495M ./cts #Android兼容性测试源
412K ./test
16K ./.classpath~
116G . #我这是编译过的 总共116G
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ tree -L 1
.
├── abi
├── Android.bp -> build/soong/root.bp
├── art
├── bionic
├── bootable
├── bootstrap.bash -> build/soong/bootstrap.bash
├── build
├── compatibility
├── cts
├── dalvik
├── developers
├── development
├── device
├── docs
├── external
├── extract-lge-bullhead.sh
├── extract-qcom-bullhead.sh
├── frameworks
├── hardware
├── libcore
├── libnativehelper
├── Makefile
├── ndk
├── out
├── packages
├── pdk
├── platform_testing
├── prebuilts
├── sdk
├── system
├── test
├── toolchain
├── tools
└── vendor
具体每个目录的功能,参考该博客: android7 源码目录结构
3.阅读,修改源码
repo start local_branch_name --all #checkoput 一个本地分支
<div class="se-preview-section-delimiter"></div>
repo branch
* android_7.1.2_r8_nexus5x | in all projects “`
可以使用vi,vim等文本编辑工具,也可以使用 eclipse, android studio ,Source Insight 3 集成工具阅读.官方支持的集成工具配置文件在:
Nougat_7.1/development/ide$ ls -l
total 12
drwxrwxr-x 2 aloe aloe 4096 9月 16 2016 eclipse
drwxrwxr-x 2 aloe aloe 4096 9月 16 2016 emacs
drwxrwxr-x 5 aloe aloe 4096 9月 16 2016 intellij
<div class="se-preview-section-delimiter"></div>
使用 Eclipse 阅读,编辑源码
把 /Nougat_7.1/development/ide/eclipse/.classpath 复制到源码的根目录下 Nougat_7.1/ 下 .classpath 是一个配置文件,声明了,把那些源码导入 Eclipse,可以根据自己的需求修改.
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="packages/apps/Bluetooth/lib/mapapi"/>
<classpathentry kind="src" path="packages/apps/Bluetooth/src"/>
<classpathentry kind="src" path="packages/apps/Browser/src"/>
<classpathentry kind="src" path="packages/apps/Calendar/src"/>
<classpathentry``` new > java project 选择源码目录,点击完成.
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > Screenshot from 2017-05-11 15:10:00.png 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > 2222.png
注意:我们只是把 Eclipse当 文本编辑工具使用,在 project > build automatically 的对勾去掉,不需要eclipse编译代码
六.编译源码
因为我们编译的文件需要运行到 nexus5X 上,刚才同步的 源码(aosp)是不包含第三方驱动代码的,去官网下载相应的驱动文件,解压到 源码根目录下.
官方驱动下载地址
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > build.png 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > build12.png
必须选择正确的版本,否则可能运行不起来。
必须选择正确的版本,否则可能运行不起来。
下载的驱动文件解压到源码根目录下后如下:
<div class="se-preview-section-delimiter"></div>
-rwxr-x–x 1 aloe aloe 95841222 4月 25 06:01 extract-lge-bullhead.sh
-rwxr-x–x 1 aloe aloe 2193841 4月 25 06:01 extract-qcom-bullhead.sh
如果没有可执行权限,需要 sudo chmod a+x 给可执行权限.
然后执行:
<div class="se-preview-section-delimiter"></div>
Nougat_7.1$ ./extract-lge-bullhead.sh“`
按 enter键,根据提示
Type "I ACCEPT" if you agree to the terms of the license: ```
输入 I ACCEPT 即可,相关的驱动会解压到 vendor 目录下;同样的方法执行 extract-qcom-bullhead.sh
2.开始编译
启动编译环境
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ . build/envsetup.sh
including device/asus/fugu/vendorsetup.sh
including device/generic/mini-emulator-arm64/vendorsetup.sh
including device/generic/mini-emulator-armv7-a-neon/vendorsetup.sh
including device/generic/mini-emulator-mips64/vendorsetup.sh
including device/generic/mini-emulator-mips/vendorsetup.sh
选择编译的分支 nexus5x 对应的是 21
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ lunch
You’re building on Linux
Lunch menu… pick a combo:
1. aosp_arm-eng
2. aosp_arm64-eng
3. aosp_mips-eng
4. aosp_mips64-eng
5. aosp_x86-eng
6. aosp_x86_64-eng
7. full_fugu-userdebug
8. aosp_fugu-userdebug
9. mini_emulator_arm64-userdebug
10. m_e_arm-userdebug
11. m_e_mips64-eng
12. m_e_mips-userdebug
13. mini_emulator_x86_64-userdebug
14. mini_emulator_x86-userdebug
15. aosp_dragon-userdebug
16. aosp_dragon-eng
17. aosp_marlin-userdebug
18. aosp_sailfish-userdebug
19. aosp_flounder-userdebug
20. aosp_angler-userdebug
21. aosp_bullhead-userdebug
22. hikey-userdebug
23. aosp_shamu-userdebug
Which would you like? [aosp_arm-eng] 21
如果知道序号的话,直接 lunch 21
``` make clobber ```
clear 一下,把之前编译的文件删除
开始编译 make -j4 # 开启4线程,我的笔记本双核,一般 core*2
<div class="se-preview-section-delimiter"></div>
Which would you like? [aosp_arm-eng] 21
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2
TARGET_PRODUCT=aosp_bullhead
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm64
TARGET_ARCH_VARIANT=armv8-a
TARGET_CPU_VARIANT=cortex-a53
TARGET_2ND_ARCH=arm
TARGET_2ND_ARCH_VARIANT=armv7-a-neon
TARGET_2ND_CPU_VARIANT=cortex-a53.a57
HOST_ARCH=x86_64
HOST_2ND_ARCH=x86
HOST_OS=linux
HOST_OS_EXTRA=Linux-4.4.0-75-generic-x86_64-with-Ubuntu-14.04-trusty
HOST_CROSS_OS=windows
HOST_CROSS_ARCH=x86
HOST_CROSS_2ND_ARCH=x86_64
HOST_BUILD_TYPE=release
BUILD_ID=N2G47O
OUT_DIR=out
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ make -j4
3.编译问题解决
由于的笔记本只有6G 内存,会出现 OOM 相关的问题而编译中断,解决办法,增加可用内存:
<div class="se-preview-section-delimiter"></div>
export JACK_SERVER_VM_ARGUMENTS=”-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4096m”
out/host/linux-x86/bin/jack-admin kill-server
out/host/linux-x86/bin/jack-admin start-server
4096 可以根据自己的机器设置,一般1024 倍数
4.Android 新一代编译 toolchain Jack & Jill 简介
在依赖了 Sun/Oracle 的 Java 编译器十年之后,Android 终于有了自己的 Java 编译器,Jack 是 Java Android Compiler Kit 的缩写,它可以将 Java 代码直接编译为 Dalvik 字节码,并负责 Minification, Obfuscation, Repackaging, Multidexing, Incremental compilation。它试图取代 javac/dx/proguard/jarjar/multidex 库等工具.
官网文档
Android 新一代编译 toolchain Jack & Jill 简介 阿里云社区的一篇博客,讲的挺不错的.
5.编译完成
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/out/target/product/bullhead$ ls -al
total 1403300
drwxrwxr-x 14 aloe aloe 4096 5月 6 18:55 .
drwxrwxr-x 3 aloe aloe 4096 5月 6 13:45 ..
-rw-rw-r– 1 aloe aloe 23 5月 6 15:10 android-info.txt
-rw-rw-r– 1 aloe aloe 12354856 5月 6 17:25 boot.img
-rw-rw-r– 1 aloe aloe 77 5月 6 21:42 build_fingerprint.txt
drwxrwxr-x 2 aloe aloe 4096 5月 6 17:57 cache
-rw-r–r– 1 aloe aloe 5824684 5月 6 17:57 cache.img
-rw-rw-r– 1 aloe aloe 72992 5月 6 13:45 clean_steps.mk
drwxrwxr-x 8 aloe aloe 4096 5月 6 21:43 data
drwxrwxr-x 3 aloe aloe 4096 5月 6 16:04 dex_bootjars
drwxrwxr-x 2 aloe aloe 4096 5月 6 15:09 fake_packages
drwxrwxr-x 7 aloe aloe 4096 5月 6 18:00 gen
-rw-rw-r– 1 aloe aloe 328835 5月 6 18:01 installed-files.json
-rw-rw-r– 1 aloe aloe 102934 5月 6 18:01 installed-files.txt
-rw-rw-r– 1 aloe aloe 10818158 5月 6 15:09 kernel
-rw-rw-r– 1 aloe aloe 1187730 5月 6 13:50 module-info.json
drwxrwxr-x 14 aloe aloe 4096 5月 6 18:00 obj
drwxrwxr-x 7 aloe aloe 4096 5月 6 17:31 obj_arm
-rw-rw-r– 1 aloe aloe 813 5月 6 13:45 previous_build_config.mk
-rw-rw-r– 1 aloe aloe 1527774 5月 6 17:25 ramdisk.img
-rw-rw-r– 1 aloe aloe 5626498 5月 6 17:25 ramdisk-recovery.img
drwxrwxr-x 3 aloe aloe 4096 5月 6 15:10 recovery
-rw-rw-r– 1 aloe aloe 67 5月 6 17:25 recovery.id
-rw-rw-r– 1 aloe aloe 16454956 5月 6 17:25 recovery.img
drwxrwxr-x 17 aloe aloe 4096 5月 6 15:10 root
drwxrwxr-x 7 aloe aloe 4096 5月 6 17:56 symbols
drwxrwxr-x 16 aloe aloe 4096 5月 6 18:01 system
-rw-rw-r– 1 aloe aloe 1024762216 5月 6 18:56 system.img
-rw-r–r– 1 aloe aloe 153140324 5月 6 17:58 userdata.img
drwxrwxr-x 5 aloe aloe 4096 5月 6 17:57 vendor
-rw-r–r– 1 aloe aloe 204663708 5月 6 17:58 vendor.img
ramdisk.img recovery.img system.img userdata.img vendor.img 就是编译出来的镜像文件
七,把编译出来的最新镜像文件 刷到 nexus5X 上
官网指导文档
启动编译环境后,任意目录 执行 croot 命令,可以回到源码根目录
<div class="se-preview-section-delimiter"></div>
Nougat_7.1$ adb reboot bootloader“`
以 bootloader 方式重启手机
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > 1111122.jpg
nexus5X bootloader 是可用的,对于不可用,解决办法如下:
fastboot flashing unlock #解锁
fastboot flashing lock #上锁
然后 fastboot 刷进我们编译的系统 fastboot flashall -w -w 参数 wipe所有的数据.
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > 9999999.jpg
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ fastboot devices
015b6734e9f634e6 fastboot
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ fastboot flashall -w
target reported max download size of 536870912 bytes
wiping userdata...
Creating filesystem with parameters:
Size: 11773390848
Block size: 4096
Blocks per group: 32768
Inodes per group: 8176
Inode size: 256
Journal blocks: 32768
Label:
Blocks: 2874363
Block groups: 88
Reserved block group size: 703
Created filesystem with 11/719488 inodes and 84965/2874363 blocks
wiping cache...
Creating filesystem with parameters:
Size: 100663296
Block size: 4096
Blocks per group: 32768
Inodes per group: 6144
Inode size: 256
Journal blocks: 1024
Label:
Blocks: 24576
Block groups: 1
Reserved block group size: 7
<div class="se-preview-section-delimiter"></div>
Created filesystem with 11/6144 inodes and 1422/24576 blocks
--------------------------------------------
Bootloader Version...: BHZ11h
Baseband Version.....: M8994F-2.6.36.2.20
<div class="se-preview-section-delimiter"></div>
Serial Number........: 015b6734e9f634e6
--------------------------------------------
checking product...
OKAY [ 0.020s]
sending 'boot' (12065 KB)...
OKAY [ 0.374s]
writing 'boot'...
OKAY [ 0.215s]
sending 'recovery' (16069 KB)...
OKAY [ 0.484s]
writing 'recovery'...
OKAY [ 0.286s]
erasing 'system'...
OKAY [ 0.297s]
sending sparse 'system' 1/2 (511476 KB)...
OKAY [ 13.336s]
writing 'system' 1/2...
OKAY [ 9.847s]
sending sparse 'system' 2/2 (489267 KB)...
OKAY [ 12.913s]
writing 'system' 2/2...
OKAY [ 9.090s]
erasing 'vendor'...
OKAY [ 0.100s]
sending 'vendor' (199866 KB)...
OKAY [ 5.144s]
writing 'vendor'...
OKAY [ 3.823s]
erasing 'userdata'...
OKAY [ 0.856s]
sending 'userdata' (136726 KB)...
OKAY [ 3.464s]
writing 'userdata'...
OKAY [ 2.357s]
erasing 'cache'...
OKAY [ 0.074s]
sending 'cache' (5688 KB)...
OKAY [ 0.250s]
writing 'cache'...
OKAY [ 0.123s]
rebooting...
finished. total time: 63.180s
<div class="se-preview-section-delimiter"></div>
到这里我们就成功了.
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > 2222222222.jpg 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > 3333333333.jpg
八.修改android源码,定制自己的 rom
举个例子:
我们把 电话首页的字符串修改一下:
<string name="speed_dial_empty_add_favorite_action" msgid="7944893641496695770">"收藏联系人"</string>
<div class="se-preview-section-delimiter"></div>
修改为: 收藏联系人:we are testing!
<string name="speed_dial_empty_add_favorite_action" msgid="7944893641496695770">"收藏联系人:we are testing!"</string>
<div class="se-preview-section-delimiter"></div>
电话源码目录:Nougat_7.1/packages/apps/Dialer
pwd
/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/packages/apps/Dialer
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/packages/apps/Dialer$ git status
On branch android_7.1.2_r8_nexus5x
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: res/values-zh-rCN/strings.xml
<div class="se-preview-section-delimiter"></div>
已经修改好了,开始编译:
因为我们已经启动过编译环境了,现在只需模块编译,只重新编译我们改动的部分,定位到 需要编译的模块下:
mm -b
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/packages/apps/Dialer$ mm -b ============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.2 TARGET_PRODUCT=aosp_bullhead TARGET_BUILD_VARIANT=userdebug TARGET_BUILD_TYPE=release TARGET_BUILD_APPS=
<div class="se-preview-section-delimiter"></div>
编译成功!
Warning: AndroidManifest.xml already defines minSdkVersion (in http://schemas.android.com/apk/res/android); using existing value in manifest.
Warning: AndroidManifest.xml already defines targetSdkVersion (in http://schemas.android.com/apk/res/android); using existing value in manifest.
[100% 13/13] Install: out/target/product/bullhead/system/priv-app/Dialer/Dialer.apk
make: Leaving directory `/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1'
<div class="se-preview-section-delimiter"></div>
#### make completed successfully (02:52 (mm:ss)) ####
<div class="se-preview-section-delimiter"></div>
[100% 13/13] Install: out/target/product/bullhead/system/priv-app/Dialer/Dialer.apk 告诉我们,我们改动的代码模块的编译出来的安装文件
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/packages/apps/Dialer$ croot
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb push out/target/product/bullhead/system/priv-app/Dialer/Dialer.apk /system/priv-app/Dialer/
<div class="se-preview-section-delimiter"></div>
adb push 到系统的对应目录下.
注意: couldn’t create file: Read-only file system 根据提示,解决如下
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb remount
Not running as root. Try "adb root" first.
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb root
restarting adbd as root
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb remount
dm_verity is enabled on the system partition.
Use "adb disable-verity" to disable verity.
If you do not, remount may succeed, however, you will still not be able to write to these volumes.
remount succeeded
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb disable-verity
Verity disabled on /system
Now reboot your device for settings to take effect
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ reboot
reboot: Need to be root
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb reboot
<div class="se-preview-section-delimiter"></div>
adb reboot 后,再执行:
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb root
restarting adbd as root
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb remount
remount succeeded
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb push out/target/product/bullhead/system/priv-app/Dialer/Dialer.apk /system/priv-app/Dialer/
[100%] /system/priv-app/Dialer/Dialer.apk
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ adb shell sync
<div class="se-preview-section-delimiter"></div>
效果如下:
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > dialer11.jpg 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > dialer22.jpg
Dialer 是一个系统app,修改其他模块,流程是一样的。
现在感兴趣的同学们就可以尝试着 DIY自己的ROM了。
九.其他问题
1.修改 SDK公共API 编译报错的问题
在 android.app.Application.java 添加一个方法
public Application() {
super(null);
}
/** * test make update-api * only show a Toast */
public void testShowToast(){
Toast.makeText(this, "test make update-api", Toast.LENGTH_LONG).show();
}```
当我们修改 sdk 公共api,编译的时候会报如下错误:
<div class="se-preview-section-delimiter"></div>
DroidDoc took 59 sec. to write docs to out/target/common/docs/system-api-stubs
[ 15% 889/5850] Checking API: checksystemapi-current
FAILED: /bin/bash -c “(( out/host/linux-x86/bin/apicheck -JXmx1024m -J\”classpath /usr/lib/jvm/java-8-openjdk-amd64/bin/../lib/tools.jar:out/host/linux-x86/framework/doclava.jar:out/host/linux-x86/framework/jsilver.jar\” -error 2 -error 3 -error 4 -error 5 -error 6 -error 7 -error 8 -error 9 -error 10 -error 11 -error 12 -error 13 -error 14 -error 15 -error 16 -error 17 -error 18 -error 19 -error 20 -error 21 -error 23 -error 24 -error 25 -error 26 -error 27 frameworks/base/api/system-current.txt out/target/common/obj/PACKAGING/system-api.txt frameworks/base/api/system-removed.txt out/target/common/obj/PACKAGING/system-removed.txt || ( cat build/core/apicheck_msg_current.txt ; exit 38 ) ) ) && (mkdir -p out/target/common/obj/PACKAGING/ ) && (touch out/target/common/obj/PACKAGING/checksystemapi-current-timestamp )”
out/target/common/obj/PACKAGING/system-api.txt:4203: error 4: Added public method android.app.Application.testShowToast()
You have tried to change the API from what has been previously approved.
To make these errors go away, you have two choices:
1) You can add “@hide” javadoc comments to the methods, etc. listed in the
errors above.
2) You can update current.txt by executing the following command:
make update-api
To submit the revised current.txt to the main Android repository,
you will need approval.
上面报错的同时,也给出了解决方案:
1) You can add "@hide" javadoc comments to the methods, etc. listed in the
errors above. # 用 @hide 标记该方法,这样添加的方法是不能被应用调用的
2) You can update current.txt by executing the following command:
make update-api
To submit the revised current.txt to the main Android repository,
you will need approval. # 用 make update-api 更新frameworks/base/api/current.txt 这样我们刚才添加的方法就变成 公共API了,可以被第三方应用程序调用
我们使用第二种解决方法,解决该问题:
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$ make update-api
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=7.1.
make update-api 成功如下:
<div class="se-preview-section-delimiter"></div>
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[ 98% 451/457] target Java: android-support-v7-mediarouter (out/target…j/JAVA_LIBRARIES/android-support-v7-mediarouter_intermediates/classes)
Note: Some input files use or override a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[ 99% 454/457] target Java: android-support-v8-renderscript (out/targe…/JAVA_LIBRARIES/android-support-v8-renderscript_intermediates/classes)
Note: frameworks/support/v8/renderscript/java/src/android/support/v8/renderscript/Script.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
[100% 457/457] Docs droiddoc: out/target/common/docs/doc-comment-check
DroidDoc took 273 sec. to write docs to out/target/common/docs/doc-comment-check
make completed successfully (15:20 (mm:ss))
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/frameworks/base/api$ git status
On branch android_7.1.2_r8_nexus5x
Changes not staged for commit:
(use “git add …” to update what will be committed)
(use “git checkout – …” to discard changes in working directory)
modified: current.txt
modified: system-current.txt
modified: test-current.txt
modified: ../core/java/android/app/Application.java
no changes added to commit (use “git add” and/or “git commit -a”)
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/frameworks/base/api
git diff ../core/java/android/app/Application.java
diff –git a/core/java/android/app/Application.java b/core/java/android/app/Application.java
index 156df36..f8bd0b7 100644
— a/core/java/android/app/Application.java
+++ b/core/java/android/app/Application.java
@@ -26,6 +26,7 @@ import android.content.ContextWrapper;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
+import android.widget.Toast;
/**
* Base class for maintaining global application state. You can provide your own
@@ -82,7 +83,15 @@ public class Application extends ContextWrapper implements ComponentCallbacks2 {
public Application() {
super(null);
}
-
- /**
-
- test make update-api
-
- only show a Toast
- */
- public void testShowToast(){
- Toast.makeText(this, “test make update-api”, Toast.LENGTH_LONG).show();
- }
-
/**- Called when the application is starting, before any activity, service,
- or receiver objects (excluding content providers) have been created.
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/frameworks/base/api$ git diff system-current.txt
diff –git a/api/system-current.txt b/api/system-current.txt
index 0fe164a..cc40083 100644
— a/api/system-current.txt
+++ b/api/system-current.txt
@@ -4200,6 +4200,7 @@ package android.app {
method public void onTrimMemory(int);
method public void registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks);
method public void registerOnProvideAssistDataListener(android.app.Application.OnProvideAssistDataListener);
- method public void testShowToast();
method public void unregisterActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks);
method public void unregisterOnProvideAssistDataListener(android.app.Application.OnProvideAssistDataListener);
}
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/frameworks/base/api$ git diff test-current.txt
diff –git a/api/test-current.txt b/api/test-current.txt
index fcbd1b5..19cfbcb 100644
— a/api/test-current.txt
+++ b/api/test-current.txt
@@ -4063,6 +4063,7 @@ package android.app {
method public void onTrimMemory(int);
method public void registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks);
method public void registerOnProvideAssistDataListener(android.app.Application.OnProvideAssistDataListener); - method public void testShowToast();
method public void unregisterActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks);
method public void unregisterOnProvideAssistDataListener(android.app.Application.OnProvideAssistDataListener);
}
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/frameworks/base/api$
可以看到已经把我们添加的方法添加到 current.txt 里了.
然后重新执行
<div class="se-preview-section-delimiter"></div>
make -j4
开始编译,编译成功如下:
<div class="se-preview-section-delimiter"></div>
warning: string ‘title_kr_tv_15’ has no default translation.
warning: string ‘title_kr_tv_19’ has no default translation.
warning: string ‘title_kr_tv_7’ has no default translation.
warning: string ‘title_kr_tv_all’ has no default translation.
Warning: AndroidManifest.xml already defines minSdkVersion (in http://schemas.android.com/apk/res/android); using existing value in manifest.
Warning: AndroidManifest.xml already defines targetSdkVersion (in http://schemas.android.com/apk/res/android); using existing value in manifest.
[100% 693/693] target Strip: keymaster_tests (out/target/product/bullhead/obj/EXECUTABLES/keymaster_tests_intermediates/keymaster_tests)
make completed successfully (13:30 (mm:ss))
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1$
然后到编译的可安装镜像目录下,查看修改时间:
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/out/target/product/bullhead$ stat system.img
File: ‘system.img’
Size: 1024766312 Blocks: 2001512 IO Block: 4096 regular file
Device: 810h/2064d Inode: 19553243 Links: 1
Access: (0664/-rw-rw-r–) Uid: ( 1000/ aloe) Gid: ( 1000/ aloe)
Access: 2017-05-11 16:27:25.339891650 +0800
Modify: 2017-05-13 12:36:30.758216994 +0800
Change: 2017-05-13 12:36:30.774216993 +0800
Birth: -
确实是我们刚才编译成功的时间戳.
<div class="se-preview-section-delimiter"></div>
adb reboot bootloader
fastboot flashall -w
然后刷到nexus5X上.
然后我们编写一个小demo,调用我们刚才自己添加的 testShowToast(),来测试一下:因为我们添加的方法,AS使用的SDK里面并没有,手机系统里才有,所有我们要绕过编译,反射调用:
<div class="se-preview-section-delimiter"></div>
package com.example.com.testapplication;
import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* 这是一个测试Demo
* 我们在 源码SDK Application 里添加了一个 testShowToast()方法,把修改后的源码编译刷进手机里面,这个demo是测试添加的
* 方法;由于AS使用的SDK里并没有这个方法,我们只能使用反射的方式,绕过编译检查,来调用 testShowToast()
*
* public void testShowToast(){
* Toast.makeText(this, “test make update-api”, Toast.LENGTH_LONG).show();
* }
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.bt_test);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Class applicationClass = null;
Method testShowToast = null;
try {
// 通过包名获得 Application class type
applicationClass = Class.forName("android.app.Application");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
// 获取我们自己在 Application 中添加的方法
testShowToast = applicationClass.getDeclaredMethod("testShowToast");
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
//获得 Application 对象
Application application = getApplication();
try {
// 反射调用我们添加的方法
testShowToast.invoke(application);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}
Demo链接地址
运行效果截图,和我们期望的一样:
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > device-2017-05-13-164323.png 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > device-2017-05-13-164528.png
2.通过源码编译 SDK
下面的命令,开始编译SDK. 也可以参考该博客: 自己动手编译最新Android源码及SDK
<div class="se-preview-section-delimiter"></div>
. build/envsetup.sh
lunch sdk-eng
make sdk -j4
SDK 编译成功如下:
<div class="se-preview-section-delimiter"></div>
[ 99% 18976/18977] Docs droiddoc: out/target/common/docs/ds
DroidDoc took 585 sec. to write docs to out/target/common/docs/ds
[ 99% 18976/18977] Docs droiddoc: out/target/common/docs/offline-sdk-referenceonly
DroidDoc took 346 sec. to write docs to out/target/common/docs/offline-sdk-referenceonly
[ 99% 18976/18977] Docs droiddoc: out/target/common/docs/online-sdk
DroidDoc took 616 sec. to write docs to out/target/common/docs/online-sdk
[ 99% 18976/18977] Docs droiddoc: out/target/common/docs/online-sdk-dev
DroidDoc took 459 sec. to write docs to out/target/common/docs/online-sdk-dev
[100% 18977/18977] Package SDK: out/host/linux-x86/sdk/sdk/android-sdk_eng.aloe_linux-x86.zip
SDK: warning: including GNU target out/target/product/generic/system/lib/libavahi-client.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libavahi-common.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libdbus.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libexif.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_blkid.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_com_err.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_e2p.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_profile.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_quota.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2_uuid.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libext2fs.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libiprouteutil.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libmicrohttpd.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libnetlink.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libnl.so
SDK: warning: including GNU target out/target/product/generic/system/lib/libxmlrpc++.so
make completed successfully (01:56:33 (hh:mm:ss))
<div class="se-preview-section-delimiter"></div>
aloe@aloe-pc:/media/aloe/c1493149-2d3a-4cb0-b260-7242e91f962a/android_source/Nougat_7.1/out/host/linux-x86/sdk/sdk$ du -ah -d 1
2.7M ./sdk_deps.mk
3.9G ./android-sdk_eng.aloe_linux-x86
914M ./android-sdk_eng.aloe_linux-x86.zip
4.8G .
现在就可以使用我们自己编译的SDK 开发app了.
<div class="se-preview-section-delimiter"></div>
package com.example.com.testcustomsdk;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.bt_test_custom_sdk);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//这个方法就是我们在源码里 android.app.Application.java 中添加的
/**
* public void testShowToast(){
*Toast.makeText(this, "test make update-api", Toast.LENGTH_LONG).show();
* }
*/
getApplication().testShowToast();
}
}
“`
Demo 地址链接
效果和我们期望的一样,如下:
陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > device-2017-05-13-200142.png 陈荣刚 > android系统源码7.1.2_r8下载,编译,运行到nexus5X上,修改源码并编译SDK进行测试 > device-2017-05-13-200228.png