Android源代码下载、编译、运行

版权声明:转载请注明出处。 https://blog.csdn.net/rentee/article/details/53911408

注:此文内所用代码工具等一般都采自官网,所以请先有能翻墙的环境,并且是Ubuntu系统(最好64位)。
我所采用的环境:Ubuntu 14.04 64位、8G RAM、Intel core i3双核四线程,硬盘剩余100G以上。
官方环境要求的文档:https://source.android.com/source/requirements.html


1.下载源码
官方文档镇楼:https://source.android.com/source/downloading.html
首先就碰到了安装repo的坑,
官方文档中:$ curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo,但是repo的地址不对。
请使用repo官网(https://code.google.com/p/git-repo)提供的下载方案:
即使用git克隆下来repo源码:git clone https://android.googlesource.com/tools/repo 或者 git clone https://gerrit.googlesource.com/git-repo

下载下来后按照官方文档的步骤下载源码即可。
另外,最好使用 $ repo init -u https://android.googlesource.com/platform/manifest -b android-2.3.7_r1 指定要下载的Android源码版本,如果不指定版本会下载最新的master分支的版本(我下载的时候,肯定是Android 7.1.1及以上版本了,master分支的可能有bug),我下载的是2.3.7的版本(其源码和编译后生成的文件一起,有20多万个文件,占用7.3G磁盘)。如果只是为了编译运行,了解Android系统的基本架构,没必要下载最新的。


2.编译源码
官方文档镇楼:https://source.android.com/source/building.html

最大的坑写在前面:如果以下这些坑你使用了解决方案但是还是报相同的错误,请使用make clobber清理build空间,然后 source build/envsetup.sh 重设下环境。再重新make。

提高编译速度可参考此篇文章:
http://blog.csdn.net/liucheng2009/article/details/7874331


亲身经历记录的坑如下列出,可对照查询:
坑1:

/bin/bash: bison: command not found 

解决方案:
安装bison:

sudo apt-get install bison

坑2:
下载下来的如果是老版的Android代码,例如我的是:2.3.7,make会出现java版本的错误。

You are attempting to build with the incorrect version
of java.

Your version is: java version "1.8.0_66".
The correct version is: 1.6.

Please follow the machine setup instructions at
    http://source.android.com/download

解决方案:
下载java 1.6,如何下载老版本java,请看此教程:http://jingyan.baidu.com/article/9989c746064d46f648ecfe9a.html
推荐下载bin二进制包,rpm分发包配置方便点,但是毕竟你电脑中如果有新版本java的话,怕引起冲突,所以还是下载二进制压缩包自己可配置性高很多。我下载的是:jdk-6u45-linux-x64.bin
安装jdk:参考此教程:http://blog.csdn.net/garfield2005/article/details/41120831
安装jdk的坑:你可能会发现,环境配好了,可是java -version命令查看java版本时,显示是Ubuntu自带的openjdk。此时是因为java快捷方式指向不对,参考此教程修改:http://blog.csdn.net/qiebobobo/article/details/46604785


坑3:
报头文件找不到:

/usr/include/features.h:374:25: fatal error: sys/cdefs.h: 没有那个文件或目录

解决方案:

sudo apt-get purge libc6-dev
sudo apt-get install libc6-dev
sudo apt-get install libc6-dev-i386(Android 2.3没有64位版本,所以需要下载32位兼容库)

坑4:

/bin/bash: g++: 未找到命令

解决方案:
安装g++:

sudo apt-get install g++

坑5:

/usr/include/c++/4.8/string:38:28: fatal error: bits/c++config.h: 没有那个文件或目录

缺少4.8版本gcc 和 g++的一些库。
解决方案:

sudo apt-get install g++-4.8-multilib
sudo apt-get install gcc-4.8-multilib

坑6:

external/clearsilver/cgi/cgi.c:22:18: fatal error: zlib.h: 没有那个文件或目录

解决方案:

sudo apt-get install zlib1g-dev

坑7:

error: ‘indexOfKey’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation

解决方案:
在源码frameworks/base/libs/utils/Android.mk第64行末尾加上 -fpermissive 。
即改为:

LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -fpermissive

提示:改完再编译可能还是报这个错误,这时建议执行 make clobber 清理下build空间,然后 source build/envsetup.sh 重设下环境。再次make。


坑8:

make:  [out/host/linux-x86/obj/EXECUTABLES/mksnapshot_intermediates/src/accessors.o] 

由于 gcc g++版本过高,应该都降级为4.4才行
解决方案:

$sudo apt-get install gcc-4.4
$sudo apt-get install gcc-4.4-multilib
$sudo apt-get install g++-4.4
$sudo apt-get install g++-4.4-multilib
然后重定向gcc、g++版本:
$sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.4 40
$sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 60
$sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.4 40
$sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 60
$sudo update-alternatives --config gcc
$sudo update-alternatives --config g++
选择指向:
* 0 /usr/bin/g++-4.6 60 auto mode
1 /usr/bin/g++-4.4 40 manual mode
2 /usr/bin/g++-4.6 60 manual mode
选择对应4.4版本前面的数字  (这里是1)
如果想改为高版本的随时用update-alternatives --config 命令切换!
记得,修改后如果仍然存在错误,make clobber,source build/envsetup.sh

坑9:

/bin/bash: flex: 未找到命令

解决方案:

sudo apt-get install flex

坑10:

make: [out/host/linux-x86/obj/SHARED_LIBRARIES/libdvm_intermediates/native/dalvik_system_Zygote.o]

解决方法:
修改 dalvik/vm/native/dalvik_system_Zygote.c

#include "Dalvik.h"
#include "native/InternalNativePriv.h"
下面加上一行:
#include <sys/resource.h>

坑11:

gperf: not found

解决方案:

sudo apt-get install gperf

坑12:

fatal error: X11/Xlib.h: 没有那个文件或目录

解决方案:

sudo apt-get install libx11-dev 

坑13:

make: [out/target/product/generic/obj/STATIC_LIBRARIES/libwebcore_intermediates/WebCore/HTMLNames.cpp] 错误 2
Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: external/webkit/WebCore/bindings/scripts /etc/perl /usr/local/lib/perl/5.18.2 /usr/local/share/perl/5.18.2 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.18 /usr/share/perl/5.18 /usr/local/lib/site_perl .) at external/webkit/WebCore/dom/make_names.pl line 38.
BEGIN failed--compilation aborted at external/webkit/WebCore/dom/make_names.pl line 38.

解决方案:
安装perl的switch库:

sudo apt-get install libswitch-perl

坑14:

gcc: error trying to exec ‘cc1plus’: execvp: No such file or directory

解决方案:
安装g++:

sudo apt-get install g++

g++是c++的编译器,安装好之后,gcc会自动寻找c++程序所需的编译环境,进而编译成功


坑15:

In file included from system/extras/ext4_utils/output_file.c:25:
/usr/include/zlib.h:34: fatal error: zconf.h: 没有那个文件或目录
compilation terminated.

解决方案:
少了一个头文件,原因是zlib1g-dev有个头文件修改了路径,及zconf.h放到了/usr/include/x86_64-linux-gnu/,所以将其拷贝到/usr/include/下即可了


坑16:

/usr/bin/ld: cannot find -lncurses
host Executable: adb (out/host/linux-x86/obj/EXECUTABLES/adb_intermediates/adb)
/usr/bin/ld: cannot find -lncurses
collect2: ld returned 1 exit status
make: *** [out/host/linux-x86/obj/EXECUTABLES/adb_intermediates/adb] Error 1

解决方法:

sudo apt-get install libncurses5-dev(32位系统)
sudo apt-get install lib32ncurses5-dev(64位系统)

坑17:

/usr/bin/ld: cannot find -lz
collect2: ld returned 1 exit status

解决方案:

sudo apt-get install zlib1g-dev

3.运行源码在Android模拟器
模拟器请使用Android源码目录的out/host/linux-x86/bin下的emulator。如果自带sdk目录下的不一定能跑起来。

终端命令:

#配置零时环境
export PATH=$PATH:~/android-2.3.7_r1/out/host/linux-x86/bin
export ANDROID_PRODUCT_OUT=~/android-2.3.7_r1/out/target/product/generic

#运行模拟器
emulator

猜你喜欢

转载自blog.csdn.net/rentee/article/details/53911408