最近公司有个需求需要搭建服务器,通过对终端系统进行网络升级。对相关知识进行整理。
android中OTA进行升级,首先需要使用源码中OTA升级包打包工具: build/tools/releasetools/ota_from_target_files 生成OTA包。包有两类:整体完全升级包和增量升级包。
在android系统编译环境下,在终端下使用make otapackage生成。
android平台使用了提供了android.os.RecoverySystem工具类支持升级,需要添加android.Manifest.permission#REBOOT permission, 而REBOOT权限需要添加android:shareUserId="android.uid.system",进行系统签名,赋予升级平台权限后,才能支持升级,或是升级app在系统源码编译环境中编译后才能使用该功能。
使用RecoverySystem工具类,需要两个步骤。
1、 进行签名验证,public static void verifyPackage(File packageFile,
ProgressListener listener,
File deviceCertsZipFile) throws IOException, GeneralSecurityException
* @param packageFile 待验证的升级包
* @param listener 升级进度监听器,验证的进度
* @param deviceCertsZipFile 验证证书文件,如果为null, 系统默认的是 "/system/etc/security/otacerts.zip"。
private static final File DEFAULT_KEYSTORE = new File("/system/etc/security/otacerts.zip")
/** 通过/cache/recovery/command,与系统通信,接收升级命令指令,详细可以查看源码: bootable/recovery/recovery.c. */
private static File RECOVERY_DIR = new File("/cache/recovery");
private static File COMMAND_FILE = new File(RECOVERY_DIR, "command");
//验证OTA升级包方法
public static void verifyPackage(File packageFile,
ProgressListener listener,
File deviceCertsZipFile)
throws IOException, GeneralSecurityException {
long fileLen = packageFile.length();
RandomAccessFile raf = new RandomAccessFile(packageFile, "r");
try {
int lastPercent = 0;
long lastPublishTime = System.currentTim