Mac Mojave编译AOSP趟坑

编译AOSP源码 官方文档写的很详细,Mac上有几个坑需要注意下:(OS 10.14.5)
错误零区分大小写的磁盘分区
官方文档Creating a case-sensitive disk image也是有的,但是我感觉文档放的稍微位置靠后,有时候会导致前面事情白做。原因是Mac文件系统不区分大小写,但是AOSP编译要求区分大小写。
步骤:打开Disk Utility (磁盘工具)然后添加一个Volume,记得选择区分大小写的选项就好了

错误一:找不到 JDK 的 tools.jar 包

build/core/config.mk:663: error: Error: could not find jdk tools.jar at /System/Library/Frameworks/JavaVM.framework/Versions/Current/Commands/…/lib/tools.jar, please check if your JDK was installed correctly.

我首先 Java -v 看了下 JDK 确实是安装了的,且包含在系统路径下,后来发现编译时使用的环境变量并不是 “JAVA_HOME” 而是 “ANDROID_JAVA_HOME”,解决方法很简单,就是在~/.bash_profile 中添加
export ANDROID_JAVA_HOME=$JAVA_HOME

错误二:找不到对应的 sdk 版本

FAILED: out/soong/build.ninja
out/soong/.bootstrap/bin/soong_build -t -l out/.module_paths/Android.bp.list -b out/soong -n out -d out/soong/build.ninja.d -o out/soong/build.ninja Android.bp
internal error: Could not find a supported mac sdk: [“10.10” “10.11” “10.12” “10.13”]
ninja: build stopped: subcommand failed.

运行命令查看本机安装的 sdk 版本

find /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs -iname "*.sdk"

发现我本机安装的版本是 10.14,确实不在配置文件的要求中,最简单粗暴的方法就是修改配置文件,在 /build/soong/cc/config/x86_darwin_host.godarwinSupportedSdkVersions 变量中添加对应的版本。

事实证明,这样子做是不行的,编译之后出现了新的问题,就是 MacOSX10.14.sdk 版本导致的

/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSUUID.h:26:49: error: nullability specifier ‘_Nullable’ cannot be applied to non-pointer type ‘uuid_t’ (aka ‘unsigned char [16]’)
(instancetype)initWithUUIDBytes:(const uuid_t _Nullable)bytes;
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSUUID.h:29:30: error: nullability specifier ‘_Nonnull’ cannot be applied to non-pointer type ‘uuid_t’ (aka ‘unsigned char [16]’)
(void)getUUIDBytes:(uuid_t _Nonnull)uuid;

(所以,这个故事告诉我们,不要随便乱改配置文件,别人没有写新的版本肯定是有原因的)
于是下载了一个低版本的 MacOSX.sdk,解压复制到 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/ 目录下
注意下载文件是 .tar.xz 格式的,需要先用 xz -d 将 XXX.tar.xz 文件解压成 XXX.tar 格式,然后用tar -xvf 解压

错误三:Mac OS 版本过高导致 bison 有什么问题(我也不太清楚)

FAILED: out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/yacc/system/tools/aidl/aidl_language_y.cpp
out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/yacc/system/tools/aidl/aidl_language_y.h
BISON_PKGDATADIR=external/bison/data prebuilts/misc/darwin-x86/bison/bison -d --defines=out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/yacc/system/tools/aidl/aidl_language_y.h -o out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/yacc/system/tools/aidl/aidl_language_y.cpp system/tools/aidl/aidl_language_y.yy
[ 2% 1270/59686] cc out/soong/.intermediates/system/core/adf/libadf/libadf/android_arm_armv7-a_static_core/obj/system/core/adf/libadf/adf.o
ninja: build stopped: subcommand failed.
11:47:11 ninja failed with: exit status 1
make: *** [run_soong_ui] Error 1

解决办法是安装 bison 的补丁并重新编译
首先进入 bison 的目录,并安装补丁

cd /external/bison
git cherry-pick c0c852bd6fe462b148475476d9124fd740eba160

重新编译

mm

这里 mm 命令如果执行失效,则需退回根目录重新执行 source build/envsetup.shlunch XXX,然后再回来执行 mm

编译成功后,替换 bison 目录,然后重新编译即可

cp /Volumes/Android/android-8.1.0_r1/out/host/darwin-x86/bin/bison /Volumes/Android/android-8.1.0_r1/prebuilts/misc/darwin-x86/bison/

错误四:内存不够了

GC overhead limit exceeded.
Try increasing heap size with java option ‘-Xmx’.

怎么操作都提示了,就是增加虚拟机的堆栈

export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx4g"

修改后注意重启下服务

./prebuilts/sdk/tools/jack-admin kill-server 
./prebuilts/sdk/tools/jack-admin start-server

编译完了之后,屏幕会出现这样的画面,就表示成功了(顺便吐槽一句编译完整个源码目录大概占了 128 GB 左右的磁盘空间,阔怕)

写着写着,发现了一篇文章写的很好,防止链接过时,拷贝了内容,附链接:
https://www.twblogs.net/a/5c9941bcbd9eee68b3b5abbd/zh-cn

你可能感兴趣的:(Mac Mojave编译AOSP趟坑)