Mac编译Android 8.1

一、配置环境
• 安装XCode
https://developer.apple.com/xcode/
xcode-select --install
• 安装JDK
https://docs.oracle.com/javase/8/docs/technotes/guides/install/mac_jdk.html
• 安装Homebrew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
• 安装工具
brew install git python make curl
• 安装MacPort
https://www.macports.org/install.php
• 安装工具
POSIXLY_CORRECT=1 sudo port install gmake libsdl git gnupg
二、创建磁盘镜像

hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 200g ~/Documents/android/android.dmg
hdiutil resize -size 250g ~/Documents/android/android.dmg.sparseimage #重新设置映像的大小
hdiutil attach ~/Documents/android/android.dmg.sparseimage -mountpoint /Volumes/android #挂载
hdiutil detach /Volumes/android #弹出映像

三、配置环境变量.bash_profile

# set the number of open files to be 1024
ulimit -S -n 1024
# Compiler cache
export USE_CCACHE=1
# mount the android file image
function mountAndroid { hdiutil attach ~/Documents/android/android.dmg.sparseimage -mountpoint /Volumes/android; }
# unmount the android file image
function umountAndroid() { hdiutil detach /Volumes/android; }
export PATH="/opt/local/bin:/opt/local/sbin:$PATH"
export PATH=~/Documents/android/bin:$PATH
export ANDROID_JAVA_HOME=$(/usr/libexec/java_home -v 1.8)
export PATH=$ANDROID_JAVA_HOME/bin:$PATH
export REPO_URL='https://mirrors.tuna.tsinghua.edu.cn/git/git-repo/'

四、下载AOSP源代码
• 下载repo并设置权限

mkdir ~/Documents/android/bin
curl https://mirrors.tuna.tsinghua.edu.cn/git/git-repo > ~/Documents/android/bin/repo
chmod a+x ~/Documents/android/bin/repo

• 初始化仓库

mountAndroid
cd /Volumes/android
mkdir WORKING_DIRECTORY
cd WORKING_DIRECTORY

repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest
repo init -u https://aosp.tuna.tsinghua.edu.cn/platform/manifest -b android-8.1.0_r62
repo init -u git://aosp.tuna.tsinghua.edu.cn/aosp/platform/manifest
repo init -u git://aosp.tuna.tsinghua.edu.cn/aosp/platform/manifest -b android-8.1.0_r62
repo sync -c

五、编译AOSP源代码

cd /Volumes/android/WORKING_DIRECTORY/ 
# Set ccache
prebuilts/misc/darwin-x86/ccache/ccache -M 50G 
# Clean out directory
make clobber 
# Jack server configuration
export JACK_SERVER_VM_ARGUMENTS="-Dfile.encoding=UTF-8 -XX:+TieredCompilation -Xmx6g"
./prebuilts/sdk/tools/jack-admin kill-server
./prebuilts/sdk/tools/jack-admin start-server 
# Setup environment 
source build/envsetup.sh

# support Mac OS Catalina 10.15
# change ./build/soong/cc/config/x86_darwin_host.go
  darwinSupportedSdkVersions = []string{
          "10.10",
          "10.11",
          "10.12",
          "10.13",
          "10.14",
          "10.15",  // 增加该行
      }
# Segmentation fault
# change ./system/sepolicy/tests/Android.bp
  stl: "libc++_static", // 去除该行

# Select device target
lunch
# Build sources (cofee time)
make -j8

六、安装

cd out/target/product/<product_name> 
adb reboot bootloader
fastboot oem unlock 
fastboot flashall -w

#烧入
adb reboot bootloader
fastboot flash boot boot.img
fastboot flash system system.img
fastboot flash userdata userdata.img
fastboot flash ramdisk ramdisk.img
fastboot flash vendor vendor.img
fastboot erase cache
fastboot reboot

七、重新编译单个包

cd WORKING_DIRECTORY
source build/envsetup.sh 
# Select device target
lunch
# Rebuil single pacage (exemple Dialer)
mmm packages/apps/dialer/

# Recompiled package output
cd /out/target/product/<device_targeted>/obj/APPS/<app_name>_intermediates

adb install -r out/target/product/<device_targeted>/system/priv-app/<app_name>/<app_name>.apk
adb reboot

八、编译系统UI

cd WORKING_DIRECTORY
source build/envsetup.sh
# Select device target
lunch
# Rebuil single pacage (exemple Dialer)
mmm frameworks/base/packages/SystemUI/

adb install -r out/target/product/<device_targeted>/system/priv-app/SystemUI/SystemUI.apk
adb reboot

九、编译Framework
• Build Framework JAR only: mmm framework/base/
• Build Framework Resources only: mmm framework/base/core/res/

# Root the device
adb root
adb disable-verity
adb reboot
adb remount
# Remove older framework
adb shell
cd /system/framework/
rm framework-res.apk
exit
# Push the new Framework
adb push out/target/product/<device_targeted>/system/framework/framework-res.apk /system/framework/
# Reboot the device
adb reboot

十、签名
标准 Android 版本使用四个密钥,所有这些密钥都位于 :build/target/product/security
• testkey:不指定密钥的包的通用默认键。
• platform:作为核心平台一部分的包的测试密钥。
• shared:家庭/联系人进程中共享的内容的测试密钥。
• media:作为媒体/下载系统一部分的包的测试密钥。
资料来源:https://source.android.com/devices/tech/ota/sign_builds#certificates-keys
若要使用现有密钥之一对应用进行签名,请使用以下指令 (LOCAL_CERTIFICATE)到关联的makefile Android.mk:
LOCAL_CERTIFICATE := platform