高階手機定製/構建自己的ROM
構建自己的 ROM 是你能擁有的最值得的體驗之一。想象一下在你的手機上擁有你自己的 Android 版本!
我們將以流行的自定義作業系統 LineageOS 作為指導參考。
- 你需要開發的手機。
- 一臺相對較新的 64 位電腦(Linux、OS X 或 Windows),擁有合理的記憶體和大約 100 GB 的可用儲存空間(如果你啟用了
ccache或為多個裝置構建,則需要更多)。記憶體越少,構建時間越長(目標為 8 GB 或更多)。使用 SSD 比傳統硬碟可以顯著縮短構建時間。 - 一根與你的手機相容的 USB 資料線。
- 良好的網際網路連線和可靠的電力供應 :)
- 對基本的 Android 操作和術語有一些瞭解。如果你之前在其他裝置上安裝過自定義 ROM 並且熟悉 recovery,這將有所幫助。瞭解一些基本的命令列概念(如
cd,代表“更改目錄”),目錄層次結構的概念以及在 Linux 中它們是如何用 / 分隔的等等,也會有所幫助。
如果你之前沒有安裝 adb 和 fastboot,你可以從 Google 下載它們。提取它執行
unzip platform-tools-latest-linux.zip -d ~
現在你需要將 adb 和 fastboot 新增到你的 PATH 中。開啟 ~/.profile 並新增以下內容
# add Android SDK platform tools to path
if [ -d "$HOME/platform-tools" ] ; then
PATH="$HOME/platform-tools:$PATH"
fi
然後,執行 source ~/.profile 更新你的環境。
構建 LineageOS 需要幾個包。你可以使用你的發行版的包管理器安裝它們。
在 Arch Linux 中,你可以透過從 AUR 安裝 lineageos-devel 來安裝所有需要的包。
要構建 LineageOS,你需要 bc bison build-essential ccache curl flex g++-multilib gcc-multilib git gnupg gperf imagemagick lib32ncurses5-dev lib32readline-dev lib32z1-dev liblz4-tool libncurses5-dev libsdl1.2-dev libssl-dev libwxgtk3.0-dev libxml2 libxml2-utils lzop pngcrush rsync schedtool squashfs-tools xsltproc zip zlib1g-dev
(如果你使用的是 Ubuntu 16.04 之前的版本,請使用 libwxgtk2.8-dev 而不是 libwxgtk3.0-dev)
(libwxgtk3.0-dev 在 Ubuntu 20.04 中更改為 libwxgtk3.0-gtk3-dev)
你還需要 Java。不同版本的 LineageOS 需要不同的 JDK(Java 開發工具包)版本。
- LineageOS 14.1-15.1:OpenJDK 1.8(安裝
openjdk-8-jdk) - LineageOS 11.0-13.0:OpenJDK 1.7(安裝
openjdk-7-jdk)*
* Ubuntu 16.04 及更高版本在標準包庫中沒有 OpenJDK 1.7。請參閱 Ask Ubuntu 中的“如何在 Ubuntu 16.04 或更高版本上安裝 OpenJDK 7?”的問題。請注意,使用 PPA openjdk-r 的建議已經過時(PPA 從未更新他們提供的 openjdk-7-jdk,因此缺少安全修復程式);即使它是投票最多的答案,也要跳過它。
你需要在你的構建環境中設定一些目錄。
要建立它們,請執行
mkdir -p ~/bin
mkdir -p ~/android/lineage
~/bin 目錄將包含 git-repo 工具(通常稱為“repo”),~/android/lineage 目錄將包含 LineageOS 的原始碼。
輸入以下命令下載 repo 二進位制檔案並使其可執行(可執行): curl https://storage.googleapis.com/git-repo-downloads/repo > ~/bin/repo
chmod a+x ~/bin/repo
在最新版本的 Ubuntu 中,~/bin 應該已經在你的 PATH 中。你可以透過文字編輯器開啟 ~/.profile 並驗證以下程式碼是否存在(如果不存在,請新增它)
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
然後,執行 source ~/.profile 更新你的環境。
在 Arch Linux 中執行相同的操作,但將檔名改為 ~/.bashrc
輸入以下命令初始化倉庫: cd ~/android/lineage
repo init -u https://github.com/LineageOS/android.git -b lineage-X --git-lfs
其中 X 是你的裝置支援的 LineageOS 版本。
你需要初始化 git 才能使其正常工作
git config --global user.name "abc"
git config --global user.email "abc@example.com"
要開始將原始碼下載到你的電腦上,請鍵入 repo sync。
LineageOS 清單包含 repo 的合理預設配置,我們強烈建議你使用它(即不要在 sync 中新增任何選項)。作為參考,我們的預設值為 -j 4 和 -c。-j 4 部分意味著將有四個同時的執行緒/連線。如果你在同步時遇到問題,可以將其降低到 -j 3 或 -j 2。另一方面,-c 將要求 repo 只拉取當前分支,而不是 GitHub 上所有可用的分支。
這取決於你的裝置。
如果你想透過執行以下命令來加快後續構建,請使用 ccache
export USE_CCACHE=1
並將該行新增到你的 ~/.bashrc 檔案中。然後,透過鍵入以下命令來指定 ccache 要使用的最大磁碟空間
ccache -M 50G
其中 50G 代表 50GB 的快取。這隻需要執行一次。從 25GB 到 100GB 會導致構建速度明顯加快(例如,典型的 1 小時構建時間可以縮短到 20 分鐘)。如果你只為一個裝置構建,25GB-50GB 就足夠了。如果你計劃為多個不共享相同核心原始碼的裝置構建,目標為 75GB-100GB。這個空間將永久佔用你的驅動器,所以請考慮這一點。有關 ccache 的更多資訊,請參閱 Google 的 Android 構建環境初始化頁面。
你也可以開啟可選的 ccache 壓縮。雖然這可能會導致效能略微下降,但它會增加快取中可以容納的檔案數量。要啟用它,請執行
export CCACHE_COMPRESS=1
或將該行新增到你的 ~/.bashrc 檔案中。
輸入 source ./build/envsetup.sh
輸入 hmm 獲取幫助文字,目前(2020-11-16)
Run "m help" for help with the build system itself.
Invoke ". build/envsetup.sh" from your shell to add the following functions to your environment:
- lunch: lunch <product_name>-<build_variant>
Selects <product_name> as the product to build, and <build_variant> as the variant to
build, and stores those selections in the environment to be read by subsequent
invocations of 'm' etc.
- tapas: tapas [<App1> <App2> ...] [arm|x86|mips|arm64|x86_64|mips64] [eng|userdebug|user]
- croot: Changes directory to the top of the tree, or a subdirectory thereof.
- m: Makes from the top of the tree.
- mm: Builds all of the modules in the current directory, but not their dependencies.
- mmm: Builds all of the modules in the supplied directories, but not their dependencies.
To limit the modules being built use the syntax: mmm dir/:target1,target2.
- mma: Builds all of the modules in the current directory, and their dependencies.
- mmma: Builds all of the modules in the supplied directories, and their dependencies.
- provision: Flash device with all required partitions. Options will be passed on to fastboot.
- cgrep: Greps on all local C/C++ files.
- ggrep: Greps on all local Gradle files.
- jgrep: Greps on all local Java files.
- resgrep: Greps on all local res/*.xml files.
- mangrep: Greps on all local AndroidManifest.xml files.
- mgrep: Greps on all local Makefiles files.
- sepgrep: Greps on all local sepolicy files.
- sgrep: Greps on all local source files.
- godir: Go to the directory containing a file.
- allmod: List all modules.
- gomod: Go to the directory containing a module.
- pathmod: Get the directory containing a module.
- refreshmod: Refresh list of modules for allmod/gomod.
.... (there's more, omitted for brevity) ...
執行 m help 輸出
# m help
The basic Android build process is:
cd /mnt/aosp/lineage
source build/envsetup.sh # Add "lunch" (and other utilities and variables)
# to the shell environment.
lunch [<product>-<variant>] # Choose the device to target.
m -j [<goals>] # Execute the configured build.
Usage of "m" imitates usage of the program "make".
See /mnt/aosp/lineage/build/make/Usage.txt for more info about build usage and concepts.
Common goals are:
clean (aka clobber) equivalent to rm -rf out/
checkbuild Build every module defined in the source tree
droid Default target
nothing Do not build anything, just parse and validate the build structure
java Build all the java code in the source tree
native Build all the native code in the source tree
host Build all the host code (not to be run on a device) in the source tree
target Build all the target code (to be run on the device) in the source tree
(java|native)-(host|target)
(host|target)-(java|native)
Build the intersection of the two given arguments
snod Quickly rebuild the system image from built packages
Stands for "System, NO Dependencies"
vnod Quickly rebuild the vendor image from built packages
Stands for "Vendor, NO Dependencies"
pnod Quickly rebuild the product image from built packages
Stands for "Product, NO Dependencies"
psnod Quickly rebuild the product_services image from built packages
Stands for "ProductServices, NO Dependencies"
onod Quickly rebuild the odm image from built packages
Stands for "ODM, NO Dependencies"
So, for example, you could run:
cd /mnt/aosp/lineage
source build/envsetup.sh
lunch aosp_arm-userdebug
m -j java
to build all of the java code for the userdebug variant of the aosp_arm device.
#### build completed successfully (16 seconds) ####
假設構建完成沒有錯誤(完成後將很明顯),在構建執行的終端視窗中輸入以下內容
cd $OUT
在那裡您將找到所有建立的檔案。最令人感興趣的兩個檔案是
recovery.img,它是 LineageOS 恢復映象。lineage-14.1-20180808-UNOFFICIAL-a5y17lte.zip,它是 LineageOS 安裝程式包。這將根據您的手機和版本而有所不同。
就是這樣!您可以像從網際網路上下載的任何其他自定義 ROM 一樣使用您的 ROM。
本文主要摘自 LineageOS 的維基。