Android源码下载(可编译)

1 源码下载

对于源码下载、编译、阅读,小编建议用 Mac 或 Linux 系统,官方推荐的是 Ubuntu 这样你会省去很多麻烦。同时,根据系统版本,建议针对性的下载,比如小编的 Mac 是 Big Sur 11.2.3 Intel,小编确想编译 Android 8.0 的源码。事实证明小编太年轻了,被教育了,各种问题,如果有人买的是 M1 芯片的,直接更简单,暂不支持 arm64。而且网上有用资源有限,所以如果电脑不是特别老的,没有对版本那么有要求的,直接下最新的源码把,这样出问题的概率比较小。

科大的镜像不好用,亲测的。建议磁盘分配大小为200G,不然会不够用。


Google Java编码规范
Android 源码资源官网
国内镜像教程
官方教程

2 源码编译、导入Studio

官方编译教程
刘望舒编译教程
CMake 教程
android-building 论坛

3 遇到的问题

1 mac 默认是不区分大小写的,所以需要分区去下载源码。

2 解决 repo sync error: Exited sync due to fetch errors

repo sync -f –j10

3 error: in sync -f: revision refs/tags/android-8.1.0_r1 in platform/art not found

是因为初始化是写错的了版本号

4 error.GitError: manifests rev-list (’^HEAD’, ‘8aaf703b41d25833d3b2d29d89c83ee7efef434b’, ‘–’): fatal: bad revision ‘^HEAD’

删除重新 init rm -rf .repo/manifest*

5 如果翻墙不稳定可以替换为国内镜像地址 https://mirrors.tuna.tsinghua.edu.cn/git/git-repo 打开repo文件

在这里插入图片描述

6 SSL: CERTIFICATE_VERIFY_FAILED 是Python3的问题

在这里插入图片描述
在这里插入图片描述
打开安装的Python包,点击如图文件进行安装即可

7 代理问题

fatal: the remote end hung up unexpectedly
fatal: early EOF
fatal: index-pack failed

关闭所有代理软件

8 Java 环境变量问题

build/core/config.mk:663: error: Error: could not find jdk tools.jar at /usr/local/Cellar/openjdk/13.0.1+9/libexec/openjdk.jdk/Contents/Home/bin/../lib/tools.jar, please check if your JDK was installed correctly.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_281.jdk/Contents/Home
export PATH=$JAVA_HOME/bin:$PATH:.
export CLASSPATH=$JAVA_HOME/lib/tools.jar:$JAVA_HOME/lib/dt.jar:.
export JAVA_HOME
export PATH
export CLASSPATH

9 prebuilts/misc/darwin-x86/bison/bison: Bad CPU type in executable

cd external/bison
touch patch-high-sierra.patch
vim patch-high-sierra.patch
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 -p0 < patch-high-sierra.patch  
mm
cp ./out/host/darwin-x86/obj/EXECUTABLES/bison_intermediates/bison ./prebuilts/misc/darwin-x86/bison/bison 

10 destroy_build_var_cache:unset:5: var_cache_ 2ND_TARGET_GCC_VERSION ANDROID_BUILD_PATHS BUILD_OS LLVM_PREBUILTS_VERSION TARGET_ARCH TARGET_BUILD_VARIANT TARGET_DEVICE TARGET_GCC_VERSION TARGET_PLATFORM_VERSION TARGET_PRODUCT print report_config : invalid parameter name

setopt shwordsplit
# 去除所有本地化的设置,让命令能正确执行
export LC_ALL=C

11 internal error: Could not find a supported mac sdk: [“10.10” “10.11” “10.12”]

由于 系统、SDK 版本不同,将编译代码的版本改成当前系统的版本会出现 一系列 bug ,所以我们可以下载想要的 SDK 版本加入系统中。需要注意的是,源码版本过老也会出现很多问题

MacOS SDK 下载地址
下载文件中所列最高版本的 SDK包。将解压后的文件夹移到 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs。然后将 /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Info.plist 将文件中 MinimumSDKVersion 版本号改成所下的版本号即可。

# Xcode中添加的SDK版本
xcodebuild -showsdks

12 The remote end hung up unexpectedly

git config --global http.lowSpeedLimit 0
git config --global http.lowSpeedTime 999999
git config --global http.postBuffer 524288000

包括 https 也加一遍

13 编译报错可能会遇到dexdump2和dex2oat被编译成32bit的,导致报错为bad cpu这个错误

art/build/Android.common.mk ,他这个地方有一个判断,看注释mac os不支持最低4g内存申请在64位进程中。

ifneq ($(HOST_OS),darwin)
  ART_HOST_SUPPORTED_ARCH := x86 x86_64
else
  # Mac OS doesn't support low-4GB allocation in a 64-bit process. So we won't be able to create
  # our heaps.
  ART_HOST_SUPPORTED_ARCH := x86_64
  ART_MULTILIB_OVERRIDE_host := 64
endif

14 glob: fcntl: too many open files

首先确认磁盘是否已满,如果磁盘满了,重新分配大小即可。如果磁盘可用空间在 10G 以上,那说明是最大文件数的问题(Mac 有最大读取文件数量限制,默认 256)。

sysctl -w kern.maxfiles=1048600
sysctl -w kern.maxfilesperproc=1048576
ulimit -n 4096

如果设置的数量过大,会报错。
注意:这么修改只针对当前 Shell 有效,关闭后设置会失效

15 external/python/cpython2/Modules/getpath

external/python/cpython2/Modules/getpath.c:414:50: error: incompatible pointer types passing 'unsigned long *' to parameter of type 'uint32_t *' (aka 'unsigned int *') [-Werror,-Wincompatible-pointer-types]
     else if(0 == _NSGetExecutablePath(progpath, &nsexeclength) && progpath[0] == SEP)
                                                 ^~~~~~~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/mach-o/dyld.h:98:54: note: passing argument to parameter 'bufsize' here
extern int _NSGetExecutablePath(char* buf, uint32_t* bufsize)

external/python/cpython2/Modules/getpath.c 找到下面代码

#ifdef __APPLE__
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
    uint32_t nsexeclength = MAXPATHLEN;
#else
    unsigned long nsexeclength = MAXPATHLEN;
#endif
#endif

替换成

#ifdef __APPLE__
    uint32_t nsexeclength = MAXPATHLEN;
#endif

如果是 cpython3报错,external/python/cpython3/Modules/getpath.c 找到下面代码

#ifdef __APPLE__
    char execpath[MAXPATHLEN + 1];
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_4
    uint32_t nsexeclength = Py_ARRAY_LENGTH(execpath) - 1;
#else
    unsigned long nsexeclength = Py_ARRAY_LENGTH(execpath) - 1;
#endif
#endif

替换成

#ifdef __APPLE__
    char execpath[MAXPATHLEN + 1];
    uint32_t nsexeclength = Py_ARRAY_LENGTH(execpath) - 1;
#endif

16 Cannot Found https://mirrors.tuna.tsinghua.edu.cn/

建议 repo sync 之前先 ping 一下这个网址,目前只在 Mac 上发现 ping 不通,应该是 DNS 解析找不到或者不在同一个服务器上。所以最简单的办法就是增加 hosts。

  1. 根据网址查询 IP
  2. 输入 sudo vi /private/etc/hosts
  3. 输入 IP地址 https://mirrors.tuna.tsinghua.edu.cn
  4. 按下「Esc」,并输入「:wq」退出编辑状态
  5. 输入 dscacheutil -flushcache 清除 DNS 缓存

17 /bin/bash: xz: command not found

brew install xz

18 Unsupported curl, please use a curl not based on SecureTransport

系统的 curl 版本是带 SecureTransport,而编译源码需要的是 OpenSSL 版本的。具体可以执行 curl --version 来进行查看。

curl 下载地址

查看本地 OpenSSL 版本,这个每个人版本不同,具体可以到相应的目录看一下。

ls /usr/local/Cellar/openssl@1.1/

下载好 curl 后,进行解压并进入目录,执行下面代码。

./configure --prefix=/usr/local/curl --with-ssl=ls /usr/local/Cellar/openssl@1.1/1.1.1k
make
make install

接着添加环境变量到 .zshrc 文件中。

export PATH="/usr/local/curl/bin:$PATH"

重启命令行查看 curl 版本,看看是否是 OpenSSL 版本的。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值