写在最前:用虚拟机装 ubuntu ,还下载 android

为了编译的更加流畅,强烈要求虚拟机内存要至少 1G 、任务管理器中虚拟机进程优先级设置为实时

4

开始正式编译,在源码目录下 make

make

我完全不知道跑了个啥,只知道超级卡。完成后产生的 out 文件夹有 3.9G

镜像生成在 out/target/product/generic 下: android 源码编译后得到 system.img,ramdisk.img,userdata.img 映像文件。其中, ramdisk.img 是 emulator 的文件系统, system.img 包括了主要的包、库等文件, userdata.img 包括了一些用户数据, emulator 加载这 3 个映像文件后,会把 system 和 userdata 分别加载到 ramdisk 文件系统中的 system 和 userdata

当然编译是件很痛苦的事情:

错误 1

************************************************************

You are attempting to build on a 32-bit system.

Only 64-bit build environments are supported beyond froyo/2.2.

************************************************************

解决:

需要进行如下修改即可,将

./external/clearsilver/cgi/Android.mk

./external/clearsilver/java-jni/Android.mk

./external/clearsilver/util/Android.mk

./external/clearsilver/cs/Android.mk

四个文件中的

LOCAL_CFLAGS += -m64

LOCAL_LDFLAGS += -m64

注释掉,或者将 “64” 换成 “32”

LOCAL_CFLAGS += -m32

LOCAL_LDFLAGS += -m32

然后,将 ./build/core/main.mk

ifneq (64,$(findstring 64,$(build_arch)))

改为:

ifneq (i686,$(findstring i686,$(build_arch)))

错误 2

host C++: libutils <= frameworks/base/libs/utils/RefBase.cpp

frameworks/base/libs/utils/RefBase.cpp: In member function ‘void android::RefBase::weakref_type::trackMe(bool, bool)’:

frameworks/base/libs/utils/RefBase.cpp:483:67: error: passing ‘const android::RefBase::weakref_impl’ as ‘this’ argument of ‘void android::RefBase::weakref_impl::trackMe(bool, bool)’ discards qualifiers [-fpermissive]

make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/RefBase.o] 错误 1

解决:

gedit frameworks/base/libs/utils/Android.mk

Change the line:

LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)

To:

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

错误 3 :

 

host Executable: aapt (out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt)

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'

collect2: ld returned 1 exit status

make: *** [out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt] 错误 1

解决:

打开 frameworks/base/tools/aapt/Android.mk

然后打开文件 Android.mk

ifeq ($(HOST_OS),linux)

#LOCAL_LDLIBS += -lrt       把这行注释掉,改为下面一行。

LOCAL_LDLIBS += -lrt -lpthread

endif

错误 4

target Dex: core

#

# An unexpected error has been detected by HotSpot Virtual Machine:

#

#  SIGSEGV (0xb) at pc=0x4003d848, pid=7668, tid=2889534320

#

# Java VM: Java HotSpot(TM) Client VM (1.5.0_22-b03 mixed mode)

# Problematic frame:

# C  [libpthread.so.0+0xa848]  pthread_cond_timedwait+0x168

#

# An error report file with more information is saved as hs_err_pid7668.log

#

# If you would like to submit a bug report, please visit:

#   http://java.sun.com/webapps/bugreport/crash.jsp

#

make: *** [out/target/common/obj/JAVA_LIBRARIES/core_intermediates/classes.dex] 已放弃 (core dumped)

解决:

虚拟机给的内存 512 太小,给个 1G

错误 5

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'

out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'

/home/leno/works/android_dev/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'

collect2: ld returned 1 exit status

make: *** [out/host/linux-x86/obj/EXECUTABLES/localize_intermediates/localize] 错误 1

解决:类似问题 3

修改 ./framework/base/tools/localize/Android.mk

ifeq ($(HOST_OS),linux)

#LOCAL_LDLIBS += -lrt       把这行注释掉,改为下面一行。

LOCAL_LDLIBS += -lrt -lpthread

endif

 

5 、编译 SDK

执行 $sudo make PRODUCT-sdk-sdk 命令,生成对应于该版本源代码的用于生产环境的 sdk

编译的 SDK 版本,实际位置是 ./out/host/linux-x86/sdk/android-sdk_eng.root_linux-x86

因为它比较常用,我们给它高优先级:

update-alternatives --install /usr/bin/AndroidSDK AndroidSDK ./out/host/linux-x86/sdk/android-sdk_eng.root_linux-x86 255

然后使用 update-alternatives --display AndroidSDK

如果要切换配置,使用 update-alternatives --config AndroidSDK

配置 AndroidSDK 环境变量。 终端中执行 gedit ~/.bashrc

在文件最后添加下面三行:

# set android environment

export ANDROID_SDK_HOME=/usr/bin/AndroidSDK

export PATH=$ANDROID_SDK_HOME/tools:$PATH

保存文件。在终端中执行 source ~/.bashrc

 

最近又编译了一次2.2sdk,但是在编译doc时,提示空指针,怎么都搞不定,求解。

最后使用的是jdk1.6编译的2.3的源码。 

6

打开终端,执行 android

$android

选择左边第一项 Virtual Devices ,然后在右边选择 New ,新建一个 AVD

Name : AVD 的名称,随便取,但只能包含字母和数字以及点、下划线和连字符,这里取名 test

Traget :目标 SDK 版本,这里选自己编译的 2.2 版 Android SDK

SD Card : SD

Skin

Hardware

然后点 Create AVD ,就创建好了一个 AVD 。点击 Start

可启动模拟器,其运行的是自己编译的镜像