升级包文件组成
编译出来的ota升级包是个zip包:
merged-xxxx-ota.zip
解压后,目录结构如下:
$ ls -R
.:
care_map.pb compatibility.zip META-INF payload.bin payload_properties.txt
./META-INF:
com
./META-INF/com:
android
./META-INF/com/android:
metadata otacert
payload.bin文件是固件,
payload_properties.txt文件中包含了四个文件元信息,内容如下:
FILE_HASH=Q40yBZtFMqLhnIRXsBNxjkBf1UhjVgIIk50XXPzqWxc=
FILE_SIZE=1126778303
METADATA_HASH=vE+sRaqhxNwghSuY1VWzrtzuEbVx+WSQIUMbcwRhY6w=
METADATA_SIZE=122970
metadata文件中包含了固件的版本信息:
$ cat META-INF/com/android/metadata
ota-property-files=payload_metadata.bin:9459:123234,payload.bin:9459:1126778303,payload_properties.txt:1126787820:156,care_map.pb:724:670,compatibility.zip:1447:7965,metadata:69:608
ota-required-cache=0
ota-streaming-property-files=payload.bin:9459:1126778303,payload_properties.txt:1126787820:156,care_map.pb:724:670,compatibility.zip:1447:7965,metadata:69:608
ota-type=AB
post-build=qti/xxxx/xxx:10/QKQ1.191116.001/89:userdebug/test-keys
post-build-incremental=89
post-sdk-level=29
post-security-patch-level=2019-10-05
post-timestamp=1630379437
pre-device=xxxx
升级前可以通过这些基础信息与当前Android系统做比较,看满不满足升级条件。
升级代码
Activity代码
package com.xxx.abota;
import android.app.Activity;
import android.os.Bundle;
import android.os.UpdateEngine;
import android.util.Log;
import android.widget.ProgressBar;
import android.view.View;
import android.widget.*;
public class UpdateActivity extends Activity {
UpdateEngine ue;
float progress;
int status;
ProgressBar progressBar;
Button startButton, pauseButton, stopButton;
boolean paused;
final String TAG = "updateApp";
private static String[] getInfo() {
//从payload_properties.txt文件解析出相关值
return new String[]{
"FILE_HASH=Q40yBZtFMqLhnIRXsBNxjkBf1UhjVgIIk50XXPzqWxc=",
"FILE_SIZE=1126778303",
"METADATA_HASH=vE+sRaqhxNwghSuY1VWzrtzuEbVx+WSQIUMbcwRhY6w=",
"METADATA_SIZE=122970"
};
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.btn1);
pauseButton = (Button) findViewById(R.id.btn2);
stopButton = (Button) findViewById(R.id.btn3);
progressBar = findViewById(R.id.pb);
progressBar.setMax(100);
paused = false;
setupButtonListeners();
try {
UECallback cb = new UECallback();
ue = new UpdateEngine();
ue.bind(cb);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void onStart() {
super.onStart();
}
@Override
public void onRestart() {
super.onRestart();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onStop() {
super.onStop();
}
private class UECallback extends android.os.UpdateEngineCallback {
public void onStatusUpdate(int aStatus, float aPercent) {
progress = aPercent * 100;
status = aStatus;
progressBar.setProgress(Math.round(progress), true);
Log.e(TAG, "current progress is " + (int) progress);
}
public void onPayloadApplicationComplete(int errCode) {
Log.e(TAG, "Payload application complete, error: " + Integer.toString(errCode));
if (errCode == 0) {
Log.e(TAG, "Installation succeeded!");
//这里需要添加重启代码
}
}
}
private void setupButtonListeners() {
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "#######start clicked");
try {
//payload.bin位置
ue.applyPayload("file:///data/ota_package/payload.bin",
0, 1126778303, //payload.bin文件大小,从payload_properties文件里读取
getInfo());
} catch (Throwable e) {
e.printStackTrace();
}
}
});
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "######pause clicked");
if (paused) {
paused = false;
ue.resume();
pauseButton.setText("resume");
return;
}
paused = true;
pauseButton.setText("pause");
ue.suspend();
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.e(TAG, "######stop clicked");
ue.cancel();
paused = false;
}
});
}
}
xml文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".UpdateActivity">
<Button
android:id="@+id/btn1"
android:text="start"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn2"
android:layout_below="@+id/btn1"
android:text="pause"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btn3"
android:layout_below="@+id/btn2"
android:text="stop"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="@+id/pb"
android:orientation="horizontal"
android:layout_below="@+id/btn3"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Manifest中添加system uid声明:
android:sharedUserId="android.uid.system"
在AS中配置使用platform签名参考博客:https://www.jianshu.com/p/7ac171669f57
检验升级结果
在onStatusUpdate回调函数中会告知调用者升级进度。
在onPayloadApplicationComplete回调中会告知升级成功或失败。
升级前后可以通过系统属性来判断:
#升级前
$ getprop | grep ro.system.build.version.incremental
[ro.system.build.version.incremental]: 87
#升级后
$ getprop | grep ro.system.build.version.incremental
[ro.system.build.version.incremental]: 89
这个数值保存在ota升级包的META-INF/com/android/metadata文件中。