这篇文章介绍阿里热更新方案的集成步骤,首先给上官方文档连接:
https://help.aliyun.com/document_detail/53240.html
集成准备篇
1.进入阿里云官网,创建一个移动热修复的项目
2.配置app下面的build.gradle
android {
...
buildTypes {
release {
//启用混淆
minifyEnabled true
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
repositories {
maven {
url "http://maven.aliyun.com/nexus/content/repositories/releases"
}
}
}
dependencies {
...
implementation 'com.android.support:multidex:1.0.3'
implementation 'com.aliyun.ams:alicloud-android-hotfix:3.2.2'
implementation 'com.aliyun.ams:alicloud-android-beacon:1.0.1'
}
3.复制SophixStubApplication.class到你的项目中,这里需要将@SophixEntry(MyApplication.class)修改为你项目中真实application。
import android.content.Context;
import android.content.Intent;
import android.support.annotation.Keep;
import android.support.multidex.MultiDex;
import android.util.Log;
import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;
/**
* Sophix入口类,专门用于初始化Sophix,不应包含任何业务逻辑。
* 此类必须继承自SophixApplication,onCreate方法不需要实现。
* 此类不应与项目中的其他类有任何互相调用的逻辑,必须完全做到隔离。
* AndroidManifest中设置application为此类,而SophixEntry中设为原先Application类。
* 注意原先Application里不需要再重复初始化Sophix,并且需要避免混淆原先Application类。
* 如有其它自定义改造,请咨询官方后妥善处理。
*/
public class SophixStubApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
public static final String HOTFIX_NEED_RESTART_BROADCAST = "hotfix.need_restart";
private final String idSecret = "";
private final String appSecret = "";
private final String rsaSecret = "";
// 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
@Keep
@SophixEntry(MyApplication.class)
static class RealApplicationStub {
}
@Override
protected void attachBaseContext(Context base) {
super.attachBaseContext(base);
// 如果需要使用MultiDex,需要在此处调用。
MultiDex.install(this);
initSophix();
}
private void initSophix() {
String appVersion = "0.0.0";
try {
appVersion = this.getPackageManager()
.getPackageInfo(this.getPackageName(), 0)
.versionName;
} catch (Exception e) {
}
final SophixManager instance = SophixManager.getInstance();
instance.setContext(this)
.setAppVersion(appVersion)
.setSecretMetaData(idSecret, appSecret, rsaSecret)
.setEnableDebug(true)
.setEnableFullLog()
.setPatchLoadStatusStub(new PatchLoadStatusListener() {
@Override
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Log.i(TAG, "sophix load patch success!");
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
// 如果需要在后台重启,建议此处用SharePreference保存状态。
Log.i(TAG, "sophix preload patch success. restart app to make effect.");
}else{
Log.i(TAG, "sophix other code is "+code);
}
Intent intent = new Intent(HOTFIX_NEED_RESTART_BROADCAST);
intent.putExtra("code", code);
sendBroadcast(intent);
}
}).initialize();
}
}
4.在管理控制台中,下载项目对应的[aliyun-emas-services.json](步骤1图片中6号箭头指向的位置),里面包含了SophixStubApplication.class中需要用到的三个常量:idSecret 、appSecret 、rsaSecret。
5.修改manifest,指定application.name为SophixStubApplication,并且加上相关权限。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="top.carlwu.hotfixdemo">
<!-- 网络权限 -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<!-- 外部存储读权限,调试工具加载本地补丁需要 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:name=".SophixStubApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
6.配置混淆文件,这里需要将MyApplication 修改为你自己项目中真实application的全类名。
#-------------------------------------------定制化区域----------------------------------------------
#---------------------------------1.实体类---------------------------------
#-------------------------------------------------------------------------
#---------------------------------2.第三方包-------------------------------
#HotFix
#基线包使用,生成mapping.txt
-printmapping mapping.txt
#生成的mapping.txt在app/buidl/outputs/mapping/release路径下,移动到/app路径下
#修复后的项目使用,保证混淆结果一致
#-applymapping mapping.txt
#hotfix
-keep class com.taobao.sophix.**{*;}
-keep class com.ta.utdid2.device.**{*;}
#防止inline
-dontoptimize
-keepclassmembers class top.carlwu.hotfixdemo.MyApplication {
public <init>();
}
#-------------------------------------------------------------------------
#---------------------------------3.与js互相调用的类------------------------
#-------------------------------------------------------------------------
#---------------------------------4.反射相关的类和方法-----------------------
#----------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
#-------------------------------------------基本不用动区域--------------------------------------------
#---------------------------------基本指令区----------------------------------
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontskipnonpubliclibraryclassmembers
-dontpreverify
-verbose
-printmapping proguardMapping.txt
-optimizations !code/simplification/cast,!field/*,!class/merging/*
-keepattributes *Annotation*,InnerClasses
-keepattributes Signature
-keepattributes SourceFile,LineNumberTable
#----------------------------------------------------------------------------
#---------------------------------默认保留区---------------------------------
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class * extends android.view.View
-keep public class com.android.vending.licensing.ILicensingService
-keep class android.support.** {*;}
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclassmembers class * extends android.app.Activity{
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep public class * extends android.view.View{
*** get*();
void set*(***);
public <init>(android.content.Context);
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
-keepclassmembers class * implements java.io.Serializable {
static final long serialVersionUID;
private static final java.io.ObjectStreamField[] serialPersistentFields;
private void writeObject(java.io.ObjectOutputStream);
private void readObject(java.io.ObjectInputStream);
java.lang.Object writeReplace();
java.lang.Object readResolve();
}
-keep class **.R$* {
*;
}
-keepclassmembers class * {
void *(**On*Event);
}
#----------------------------------------------------------------------------
#---------------------------------webview------------------------------------
-keepclassmembers class fqcn.of.javascript.interface.for.Webview {
public *;
}
-keepclassmembers class * extends android.webkit.WebViewClient {
public void *(android.webkit.WebView, java.lang.String, android.graphics.Bitmap);
public boolean *(android.webkit.WebView, java.lang.String);
}
-keepclassmembers class * extends android.webkit.WebViewClient {
public void *(android.webkit.WebView, jav.lang.String);
}
#----------------------------------------------------------------------------
#---------------------------------------------------------------------------------------------------
7.编写广播接收器,处理SophixStubApplication发送过来的热更新信息。
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
import com.taobao.sophix.PatchStatus;
public class HotFixReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(SophixStubApplication.HOTFIX_NEED_RESTART_BROADCAST.equals(action)){
int code = intent.getIntExtra("code", 0);
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Toast.makeText(context,"patch应用成功",Toast.LENGTH_LONG).show();
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
Toast.makeText(context,"patch将在重启后生效",Toast.LENGTH_LONG).show();
}
}
}
}
8.主要的热更新相关api使用示例
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void checkPatch(View view) {
SophixManager.getInstance().queryAndLoadNewPatch();//检查热更新入口方法
}
public void cleanPatch(View view) {
SophixManager.getInstance().cleanPatches();//清空补丁
}
public void killSafely(View view) {
SophixManager.getInstance().killProcessSafely();//安全退出程序
}
}
使用篇
1.检查热更新
入口方法为:
SophixManager.getInstance().queryAndLoadNewPatch();//检查热更新入口方法
当没有该版本的patch时,logcat显示如下:
05-03 21:45:33.762 10914-10914/top.carlwu.hotfixdemo V/BoostFramework: BoostFramework() : mPerf = com.qualcomm.qti.Performance@b9ce4db
05-03 21:45:33.860 10914-11205/top.carlwu.hotfixdemo V/Sophix.SophixInvoker: device is active.
05-03 21:45:33.861 10914-11205/top.carlwu.hotfixdemo I/Sophix.NetworkManager: query start
05-03 21:45:33.861 10914-11205/top.carlwu.hotfixdemo D/Sophix.NetworkManager: openConnection auth reqUrl: https://hotfix-api.aliyuncs.com/u/24723968-1/WTderR%2FBxdUDAJanVkvI2aeS/1.0/5/
05-03 21:45:33.864 10914-11205/top.carlwu.hotfixdemo D/NetworkSecurityConfig: No Network Security Config specified, using platform default
05-03 21:45:34.244 10914-11205/top.carlwu.hotfixdemo I/SophixStubApplication: sophix other code is 6
05-03 21:45:34.246 10914-11205/top.carlwu.hotfixdemo I/Sophix.NetworkManager: query no update
2.发布基线版本
每一个基线版本需要保留其对应的mapping.xml文件,留做打补丁包是备用。
3.生成补丁包
对项目代码做出修改后,首先修改混淆文件proguard-rules.pro为:
#HotFix
#基线包使用,生成mapping.txt
#-printmapping mapping.txt
#生成的mapping.txt在app/buidl/outputs/mapping/release路径下,移动到/app路径下
#修复后的项目使用,保证混淆结果一致
-applymapping mapping.txt
#hotfix
-keep class com.taobao.sophix.**{*;}
-keep class com.ta.utdid2.device.**{*;}
#防止inline
-dontoptimize
-keepclassmembers class top.carlwu.hotfixdemo.MyApplication {
public <init>();
}
注意patch是针对版本发布的,所以不能修改versionCode和versionName。
将打包基线版本时生成的mapping.xml拷贝到项目路径下。
然后打包apk,得到修改后的最新apk包。
下载补丁生成工具:
https://help.aliyun.com/document_detail/53247.html?spm=a2c4g.11186623.6.550.WxZo8v
其中的旧包指的是当前打补丁包针对的基线版本,不论打多少个补丁,基线版本都是最初的那个(和mapping.xml一同生成的)。
新包指的是当前修改完程序后最新打包出来的apk,补丁生成工具通过将上述两个apk包比对差异,然后生成补丁文件。
4.发布patch
按照上图标注顺序发布版本号的patch。
其中,恢复发布指的是小数量发布补丁,全量发布则是所有线上用户均可以接收到patch。
补充干货
如何让app在需要重启应用补丁时,当app切换到后台自动调用:
SophixManager.getInstance().killProcessSafely();//安全退出程序
然后用户再次进入app时,就最小感知的更新了app?
tip,可以利用activity的生命周期。
首先,HotFixReceiver 需要在必须冷启动更新时做一个sp的boolean类型保存:
public class HotFixReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(SophixStubApplication.HOTFIX_NEED_RESTART_BROADCAST.equals(action)){
int code = intent.getIntExtra("code", 0);
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
Toast.makeText(context,"patch应用成功",Toast.LENGTH_LONG).show();
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
Toast.makeText(context,"patch将在重启后生效",Toast.LENGTH_LONG).show();
SharedPreferences.Editor editor =context.getSharedPreferences("hotfix",Context.MODE_PRIVATE).edit();
editor.putBoolean("hotfix_need_restart",true).commit();
}
}
}
}
然后,真是application需要注册activity生命周期回调函数:
import android.app.Activity;
import android.app.Application;
import android.content.SharedPreferences;
import android.os.Bundle;
import com.taobao.sophix.SophixManager;
/**
* Created by Carl on 2018/5/3 003.
*/
public class MyApplication extends Application {
private int alive_count;//当前存活activity总数量
@Override
public void onCreate() {
super.onCreate();
registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
alive_count++;
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
alive_count--;
check4Restart();
}
});
}
private void check4Restart() {
SharedPreferences sp = getSharedPreferences("hotfix", MODE_PRIVATE);
boolean shouldRestart = sp.getBoolean("hotfix_need_restart", false);
if (alive_count == 0 && shouldRestart) {
sp.edit().clear().commit();
SophixManager.getInstance().killProcessSafely();
}
}
}
用户按下back键返回桌面后就可以立即杀死程序应用patch了。
最后,附上源码连接