美文网首页
如何在Majove下编译aapt2

如何在Majove下编译aapt2

作者: ScratchPad | 来源:发表于2019-06-18 20:07 被阅读0次

基本的逻辑

  1. source build/envsetup.sh来配置环境
  2. /path/to/aosp/frameworks/base/tools/aapt2下执行mma来编译aapt2

这些命令最后会调用soong,soong根据上下文来解析对应的一些Android.bp文件,soong最后会生成对应的xxxx.ninja文件,ninja命令会根据这些规则执行具体的build逻辑。

可以通过注释掉Android.bp文件中一些不需要的module,来加快速度,另外aosp项目的所有的module类型定义在/Volumes/workspace/aosp/build/soong/android/register.go

一些在macOS Mojave上的tips:

1. 不兼容的mac sdk

就如Apple开发者下载页面中对Command Line Tool的描述一样

This package enables UNIX-style development via Terminal by installing command line developer tools, as well as macOS SDK frameworks and headers.
Many useful tools are included, such as the Apple LLVM compiler, linker, and Make. If you use Xcode, these tools are also embedded within the Xcode IDE.

macOS共有两处存放了开发使用到的的macOS SDK,不同的项目可能使用不同的:

一套是Xcode内嵌的,位于/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs,例如在编译virtualbox、dart-lang-sdk、skia的时候都是使用了Xcode内嵌的。

一套是单独的Command Line Tools,位于/Library/Developer/CommandLineTools/SDKs

macOS Mojave最低只支持Command_Line_Tools_macOS_10.13_for_Xcode_9.4.1,所以在编译的过程中会遇到以下错误

internal error: Could not find a supported mac sdk: ["10.10" "10.11" "10.12"]

除了进行系统降级之外,我们可以尝试使用10.13版的SDK进行编译,通过修改/path/to/aosp/build/soong/cc/config/x86_darwin_host.go中的

darwinSupportedSdkVersions = []string{
"10.10", "10.11",  "10.12","10.13",
}

我们在这里加上10.13就行了。

然后分别安装Xcode9.4.1和对应的CommandLineTools就行了。

2. tools.jar无法定位

在执行mma的过程中,可能会遇到:

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

根据build/core/config.mk,我们知道编译系统查找tools.jar的逻辑在/path/to/aosp/build/make/core/find-jdk-tools-jar.sh中,了解其逻辑后知道,我们可以通过指定ANDROID_JAVA_HOME环境变量来手动指定

export ANDROID_JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home; mma

3. bison出错

在执行mma的过程中,可能会遇到:

[  0% 35/75651] yacc out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/yacc/system/tools/aidl/aidl_language_y.cpp
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  
[  0% 39/75651] lex out/soong/.intermediates/system/tools/aidl/libaidl-common/darwin_x86_64_static/gen/lex/system/tools/aidl/aidl_language_l.cpp
ninja: build stopped: subcommand failed.  
14:24:58 ninja failed with: exit status 1 

方法一,可以参考http://effie.io/aosp-8-1-failing-to-build-on-mac-high-sierra/手动编译bison

With format string strictness, High Sierra also enforces that %n isn't used
in dynamic format strings, but we should just disable its use on darwin in
general.

--- lib/vasnprintf.c.orig   2017-06-22 15:19:15.000000000 -0700
+++ lib/vasnprintf.c    2017-06-22 15:20:20.000000000 -0700
@@ -4869,7 +4869,7 @@ VASNPRINTF (DCHAR_T *resultbuf, size_t *
 #endif
                   *fbp = dp->conversion;
 #if USE_SNPRINTF
-# if !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
+# if !defined(__APPLE__) && !(((__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 3)) && !defined __UCLIBC__) || ((defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__))
                 fbp[1] = '%';
                 fbp[2] = 'n';
                 fbp[3] = '\0';

将上述的内容保存到patch-high-sierra.patch中,然后在external/bison目录下执行

patch -p0 < patch-high-sierra.patch

patch完毕后,通过在aosp的根目录下执行export ANDROID_JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/; make bison来编译patch后的bison。

最后执行cp /path/to/aosp/out/host/darwin-x86/obj/EXECUTABLES/bison_intermediates/bison /path/to/aosp/prebuilts/misc/darwin-x86/bison/bison

方法二,通过/path/to/aosp/external/bison可以知道aosp使用的是2.7版的bison,恰巧homebrew提供了2.7版本的bison,因此我们可以直接brew install bison@2.7,然后
cp /usr/local/opt/bison@2.7/bin/bison /path/to/aosp/prebuilts/misc/darwin-x86/bison

  • 注意:本文提到的路径只是示意作用,并非是实际的路径

相关文章

网友评论

      本文标题:如何在Majove下编译aapt2

      本文链接:https://www.haomeiwen.com/subject/srmqqctx.html