发布时间:2025-12-09 17:59:30 浏览次数:4
从去年中接触到patchrom,而之后也有很大一部分时间投入到之中,所以对patchrom的整个流程也略之一二。而patchrom,可以看成是一个对rom二次开发比较实用的工具,而不是仅仅向对miui, 或者乐蛙等rom进行机型的适配。废话不多说。
1,首先需要装载好ubuntu环境,这个网上都有教程也就不再作介绍了。
2,需要拉取一份Miui 或者 lewa 的pathrom 的平台代码。因为博主熟悉lewa这里就用lewa的patchrom 4.4平台做介绍了。
3,首先从githup上面将lewa4.4 patchrom平台的代码拉取下来。
1.创建githup帐号以及githup帐号的配置。参见http://blog.csdn.net/tangbin330/article/details/9128765
2.配置githup以后可以通过
mkdir patchrom
cd patchrom
repo init -u git://github.com/LeWaCode/patchrom.git -b kitkat
然后同步代码:
repo sync
3.在这个平台下有几款已经适配好了的机型,我们用nexus5进行分析。
我们在patchrom 目录下 执行 . build/envsetup.sh
然后
cd nexus5
make workspace
make firstpatch
make fullota
通过这三部我们就能够在out目录下找到生成的刷机包以及ota包。
patchrom 是如何工作的呢?
下面我们来分析patchrom平台的结构
他包含了android , build , lewa, tools 四个主要目录以及各个机型的目录。
android 目录:
android.policy.jar.out framework2.jar.out framework.jar.out services.jar.out telephony-common.jar.out
这些目录是rom方修改过的framework的代码所成生的smail 通过通过apktools d 将 编译好的framek*.jar 解开放入该目录下。
而base-framework则放入的是google aosp代码所编译的smail 文件。
build 目录:
存放主要编译流程的文件,主要为mk, make 编译可参见http://blog.csdn.net/onlyou930/article/details/6553870
tools 目录:
存放patchrom 需要用到的各种工具,以下会做介绍。
lewa 目录:
存放rom方提供的基础包,该包是在aosp 源码上修改得到的一个rom包。
然后我们进入机型目录nexus5地方下,我们看看make workspace 都做了什么?
我们找到了workspace在 build/util.mk 内
我们来看看 这一句都干了什么呢?
其中 apktool-if 是将需要的系统资源装载好,其中包括乐蛙的framework-res.apk,lewa-res.apk 和 底包的framework-res.apk
第二个JARS_OUTDIR则是将底中的framework.jar framework2.jar services.jar android.policy.jar telephony-common.jar 解压本且反编译成smali 放入机型目录下供之后patch使用
而后面的两句则是针对apktool 预处理framework-res 和 keyguard 的一些问题 可以先不管。
总之现在第一步workspace 就执行完了,也就有了patch的目标目录和之后处理系统apk需要用到的资源。
下面第二部:make firstpatch:
我们来看看代码,PATCH_LEWA_FRAMEWORK 在porting.mk 里面被赋值为
PATCH_LEWA_FRAMEWORK := $(TOOL_DIR)/patch_lewa_framework.sh $(INFO)其实就是执行tool 下的patch_lewa_framework.sh 这个脚本 而后面的三个参数则是需要用的参数。我们看看脚本代码 并不多:`
#!/bin/bash# $1: the old smali code $2: the new smali code $3: the destination smali codeif [ $# -ne 3 ];thenecho "Usage: patchlewa.sh old_smali_dir new_smali_dir dst_smali_dir"fi#其中 $1 $2 $3 分别对应的是aosp framework的smail目录, rom开发方的framework smali目录 和 当前目录PWD=`pwd`old_smali_dir=$1new_smali_dir=$2dst_smali_dir=$3temp_dir=$PWD/temptemp_old_smali_dir=$temp_dir/old_smalitemp_new_smali_dir=$temp_dir/new_smalitemp_dst_smali_orig_dir=$temp_dir/dst_smali_origtemp_dst_smali_patched_dir=$temp_dir/dst_smali_patchedreject_dir=$temp_dir/rejectrm -rf $temp_direcho "<<< create temp directory to store the old, new source and destination smali code with .line removed"echo "dst_smali_dir = " $dst_smali_dirmkdir -p $temp_old_smali_dirmkdir -p $temp_new_smali_dirmkdir -p $temp_dst_smali_orig_dirmkdir -p $temp_dst_smali_patched_dirmkdir -p $reject_dircp -r $old_smali_dir/*.jar.out $temp_old_smali_dircp -r $new_smali_dir/*.jar.out $temp_new_smali_dircp -r $dst_smali_dir/*.jar.out $temp_dst_smali_orig_dir$PORT_ROOT/tools/rmline.sh $temp_dirfunction apply_lewa_patch() {old_code_noline=$temp_old_smali_dir/$1new_code_noline=$temp_new_smali_dir/$1dst_code_noline=$temp_dst_smali_orig_dir/$1dst_code=$dst_smali_dir/$1dst_code_orig=$dst_code.origecho "<<< compute the difference between $old_code_noline and $new_code_noline"cd $old_code_nolinefor file in `find ./ -name "*.smali"`doif [ -f $new_code_noline/$file ]then#将rom开发放的smali与aosp的smali一一对比并生成差别文件diff $file $new_code_noline/$file > /dev/null || {diff -B -c $file $new_code_noline/$file > $file.diff}elseecho "$file does not exist at $new_code_noline"fidonecd $dst_smali_dirmv $dst_code $dst_code_origcp -r $dst_code_noline $dst_codeecho "<<< apply the patch into the $dst_code"cd $old_code_noline#将生成的diff patch到机型目录下的framework 的 smali文件中并将不能patch的内容写入到rej 文件中。 for file in `find ./ -name "*.smali.diff"`domkdir -p $reject_dir/$1/`dirname $file`patch $dst_code/${file%.diff} -r $reject_dir/$1/${file%.diff}.rej < $filedonecp -r $dst_code $temp_dst_smali_patched_dircd $dst_code_nolinefor file in `find ./ -name "*.smali"`dorm -f $file.diffdiff -B -c $file $dst_code_orig/$file > $file.diffpatch -f $dst_code/$file -r /dev/null < $file.diff >/dev/null 2>&1rm -f $file.diffdonefind $dst_code -name "*.smali.orig" -exec rm {} \;find $temp_dst_smali_patched_dir -name "*.smali.orig" -exec rm {} \;rm -rf $dst_code_orig}jar_outs=`grep -rn "JAR_OUTS" $new_smali_dir/README | cut -d'=' -f2`for out in $jar_outsdoapply_lewa_patch $outdoneechoechoecho ">>> patch lewa into target framework is done. Please look at $reject_dir to resolve any conflicts!" fullota: BUILD_NUMBER := $(ROM_BUILD_NUMBER)fullota: target_files@echo ">>> To build out target file: fullota.zip ..."$(BUILD_TARGET_FILES) $(INCLUDE_THIRDPART_APP) fullota.zip@echo "<<< build target file completed!"其中 BUILD_NUMBER 为当前ota包生成的时间。
接下来我们看到一个关键的target_files.
这样整个包的资源也就都准备好了,接下来就是要打包了。打包在
$(BUILD_TARGET_FILES) $(INCLUDE_THIRDPART_APP) fullota.zip这一句, 主要包含的是一个脚本和两个参数
BUILD_TARGET_FILES = $(TOOL_DIR)/build_target_files.sh $(INFO)`我们看看 build_target_files.sh 里面有些什么
# copy the whole # copy the whole target_files_template dir#将我们patch 需要用到的目录模板拷贝到out 目录下function copy_target_files_template {echo "Copy target file template into current working directory"rm -rf $TARGET_FILES_DIRmkdir -p $TARGET_FILES_DIRcp -r $TARGET_FILES_TEMPLATE_DIR/* $TARGET_FILES_DIR}#将我们底包的bootimage 拷贝到out/target_files下function copy_bootimage {echo "Copy bootimage"for file in boot.img zImage */boot.img */zImagedoif [ -f $ZIP_DIR/$file ]thencp $ZIP_DIR/$file $TARGET_FILES_DIR/BOOTABLE_IMAGES/boot.imgreturnfidone}#将低包的system 目录拷贝到target_files目录下function copy_system_dir {echo "Copy system dir"cp -rf $ZIP_DIR/system/* $TARGET_FILES_DIR/SYSTEM}#将低包的data 目录拷贝到target_files目录下function copy_data_dir {#The thirdpart apps have copyed in copy_target_files_template function,#here, just to decide whether delete them.if [ $INCLUDE_THIRDPART_APP = "true" ];thenecho "Copy thirdpart apps"elserm -rf $TARGET_FILES_DIR/DATA/*fiecho "Copy lewa preinstall apps"mkdir -p $TARGET_FILES_DIR/DATA/cp -rf $ZIP_DIR/data/media/preinstall_apps $TARGET_FILES_DIR/DATA/if [ -f customize_data.sh ];then./customize_data.sh $PRJ_DIRfi}#链接工具文件function recover_link {cp -f $METADATA_DIR/linkinfo.txt $TARGET_FILES_DIR/SYSTEMpython $TOOL_DIR/releasetools/recoverylink.py $TARGET_FILES_DIRrm $TARGET_FILES_DIR/SYSTEM/linkinfo.txt}function process_metadata {echo "Process metadata"#copy 刷机需要用到的文件信息cp -f $METADATA_DIR/filesystem_config.txt $TARGET_FILES_DIR/METAcat $TOOL_DIR/target_files_template/META/filesystem_config.txt >> $TARGET_FILES_DIR/META/filesystem_config.txt#copy 分区表信息cp -f $METADATA_DIR/recovery.fstab $TARGET_FILES_DIR/RECOVERY/RAMDISK/etc#合并apk 签名信息python $TOOL_DIR/uniq_first.py $METADATA_DIR/apkcerts.txt $TARGET_FILES_DIR/META/apkcerts.txt $PRJ_DIRcat $TARGET_FILES_DIR/META/apkcerts.txt | sort > $TARGET_FILES_DIR/temp.txtcat $TOOL_DIR/metadata/apkcerts_lewa.txt | sort >> $TARGET_FILES_DIR/temp.txtmv $TARGET_FILES_DIR/temp.txt $TARGET_FILES_DIR/META/apkcerts.txtrecover_link}#将准备好的所有文件打包成 target_files.zip# compress the target_files dir into a zip filefunction zip_target_files {echo "Compress the target_files dir into zip file"echo $TARGET_FILES_DIRcd $TARGET_FILES_DIRecho "zip -q -r -y $TARGET_FILES_ZIP *"zip -q -r -y $TARGET_FILES_ZIP *cd -}#对target_files 内部apk 进行签名function sign_target_files {echo "Sign target files"$SIGN_TARGET_FILES_APKS -d $PORT_ROOT/build/security $TARGET_FILES_ZIP temp.zipmv temp.zip $TARGET_FILES_ZIPif [ "$PARTNER" != "Lewa" ];thencp $TARGET_FILES_ZIP $LEWA_OTA_PACKAGEelsecp $TARGET_FILES_ZIP $OTA_PACKAGEfi}#对target_files.zip 进行前面并转换为最后的卡刷包# build a new full ota packagefunction build_ota_package {echo "Build full ota package: $OUT_DIR/$OUT_ZIP_FILE"$OTA_FROM_TARGET_FILES -n -k $PORT_ROOT/build/security/testkey -o $TARGET_OTA_ASSERT_DEVICE $TARGET_FILES_ZIP $OUT_DIR/$OUT_ZIP_FILEif [ "$PARTNER" != "Lewa" ];thencp $OUT_DIR/$OUT_ZIP_FILE $FULL_OTA_PACKAGEelsecp $OUT_DIR/$OUT_ZIP_FILE $LEWA_OTA_FULL_PACKAGEfi}if [ $# -eq 3 ];thenNO_SIGN=trueOUT_ZIP_FILE=$3INCLUDE_THIRDPART_APP=$1elif [ $# -eq 2 ];thenOUT_ZIP_FILE=$2INCLUDE_THIRDPART_APP=$1elif [ $# -eq 1 ];thenINCLUDE_THIRDPART_APP=$1ficopy_target_files_templatecopy_bootimagecopy_system_dircopy_data_dirprocess_metadataif [ -f "customize_target_files.sh" ]; then./customize_target_files.shif [ $? -ne 0 ];thenexit 1fifizip_target_filesif [ -n "$OUT_ZIP_FILE" ];thenif [ "$NO_SIGN" == "false" ];thensign_target_filesfibuild_ota_packagefi