之前用的是腾讯的热更新服务,但过几天腾讯就要停止服务了,换成阿里云的热修复。以前没做过原生,搞得比较痛苦,记录一下!
只有集成安卓端
平台操作
- 在右方研发工具中开启移动热修复
代码集成
官方文档挺详细的,不过我也记录一下我的步骤,方便以后复用(官方文档地址)
- 配置AndroidManifest.xml
<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"/>
······
<!-- SophixStubApplication为自定义新增的sophix入口类名字 -->
<application
android:name=".SophixStubApplication"
······>
······
<meta-data
android:name="com.taobao.android.hotfix.IDSECRET"
android:value="" />
<meta-data
android:name="com.taobao.android.hotfix.APPSECRET"
android:value="" />
<meta-data
android:name="com.taobao.android.hotfix.RSASECRET"
android:value="" />
······
</application>
- 配置build.gradle
dependencies {
······
implementation 'com.aliyun.ams:alicloud-android-hotfix:3.3.5'
······
}
repositories {
······
maven { url 'http://maven.aliyun.com/nexus/content/repositories/releases/' }
······
}
- 混淆配置 proguard-rules.pro
# sophix
#基线包使用,生成mapping.txt
-printmapping mapping.txt
#生成的mapping.txt在app/build/outputs/mapping/release路径下,移动到/app路径下
#修复后的项目使用,保证混淆结果一致
#-applymapping mapping.txt
#hotfix
-keep class com.taobao.sophix.**{*;}
-keep class com.ta.utdid2.device.**{*;}
-dontwarn com.alibaba.sdk.android.utils.**
#防止inline
-dontoptimize
-keepclassmembers class com.my.demo.MyApplication {
public <init>();
}
# 如果不使用android.support.annotation.Keep则需加上此行
# -keep class com.my.pkg.SophixStubApplication$RealApplicationStub
- Sophix入口类
贴一下官方代码
import android.app.Application;
import android.content.Context;
import android.support.annotation.Keep;
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;
import com.my.pkg.MyRealApplication;
/**
* Sophix入口类,专门用于初始化Sophix,不应包含任何业务逻辑。
* 此类必须继承自SophixApplication,onCreate方法不需要实现。
* 此类不应与项目中的其他类有任何互相调用的逻辑,必须完全做到隔离。
* AndroidManifest中设置application为此类,而SophixEntry中设为原先Application类。
* 注意原先Application里不需要再重复初始化Sophix,并且需要避免混淆原先Application类。
* 如有其它自定义改造,请咨询官方后妥善处理。
*/
public class SophixStubApplication extends SophixApplication {
private final String TAG = "SophixStubApplication";
// 此处SophixEntry应指定真正的Application,并且保证RealApplicationStub类名不被混淆。
@Keep
@SophixEntry(MyRealApplication.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(null, null, null)
.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.");
}
}
}).initialize();
}
}
我们需要在PatchLoadStatusListener中添加
public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
if (code == PatchStatus.CODE_LOAD_SUCCESS) {
String filesPath = getBaseContext().getFilesDir().getAbsolutePath();
String parentPath = getBaseContext().getFilesDir().getParentFile().getAbsolutePath();
String patchFilePath = FlutterPatch.findLibraryFromSophix(getBaseContext(), filesPath+"/sophix", "libapp.so");
SharedPreferences settings = getBaseContext().getSharedPreferences("FlutterSharedPreferences", 0);
int sophixPatchVersion = settings.getInt("flutter.sophixPatchVersion",-99);
boolean isNewPatch = sophixPatchVersion == -99 || handlePatchVersion != sophixPatchVersion;
if (!new File(parentPath+ "/libapp.so").exists() || isNewPatch) {
//拷贝libapp.so到配置好的加载路径
FlutterPatch.copyFileByPath(patchFilePath, parentPath + "/libapp.so");
File sof = new File(parentPath + "/libapp.so");
sof.setExecutable(true);
sof.setReadable(true);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("flutter.sophixPatchVersion", handlePatchVersion);
editor.commit();
Log.i(TAG, "sophix补丁加载成功!版本号为"+handlePatchVersion);
} else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
// 如果需要在后台重启,建议此处用SharePreference保存状态。
long now = System.currentTimeMillis();
SharedPreferences settings = getBaseContext().getSharedPreferences("LoadNewPatch", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putLong("LoadNewPatchTime", now);
editor.commit();
Looper.prepare();
Toast.makeText(getBaseContext(),"检测到版本更新完成,重启后生效",Toast.LENGTH_SHORT).show();
Looper.loop();
Log.i(TAG, "sophix补丁预加载成功. 重启后生效.本次加载时间为"+now);
} else if (code == PatchStatus.CODE_REQ_NOUPDATE || code == PatchStatus.CODE_REQ_NOTNEWEST
|| code == PatchStatus.CODE_DOWNLOAD_BROKEN || code == PatchStatus.CODE_UNZIP_FAIL
|| code == PatchStatus.CODE_REQ_UNAVAIABLE || code == PatchStatus.CODE_REQ_SYSTEMERR) {
long now = System.currentTimeMillis();
SharedPreferences settings = getBaseContext().getSharedPreferences("LoadNewPatch", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putLong("LoadNewPatchTime", now);
editor.commit();
Log.i(TAG, "sophix查询结束.本次加载时间为"+now);
}
}
TIPS
监听中使用Toast需要在前后加上Looper.prepare();Looper.loop();
(否则会报错 Can't toast on a thread that has not called Looper.prepare())
发布时设置setEnableDebug(false)
LoadNewPatchTime是我写入缓存中的上一次更新时间
设置时间的状态码可能有遗漏
flutter使用so需要复制到项目路径下,与原生位置不同
!!!更新包与基准包需要有原生代码的区别才能生效!!!
- MainActivity
queryAndLoadNewPatch方法是计费接口,我在main中设置定时调用(newPatch中)
class MainActivity: FlutterActivity() {
private var mHandler: Handler = Handler()
private var mTimeCounterRunnable: Runnable = Runnable { }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
// 全局变量 定时任务
mHandler = Handler(Looper.getMainLooper())
mTimeCounterRunnable = object : Runnable {
override fun run() { //在此添加需轮询的接口
FlutterPatch.findNewPatch(baseContext) //执行的任务
mHandler.postDelayed(this, (10 * 60 * 1000).toLong())
}
}
mHandler.post(mTimeCounterRunnable)
}
override fun onDestroy() {
//关闭定时任务
mHandler.removeCallbacks(mTimeCounterRunnable)
super.onDestroy()
}
}
另一种定时任务方法
class MainActivity: FlutterActivity() {
private lateinit var mHandler: Handler
private lateinit var thread:Thread
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
// 全局变量 定时任务
mHandler = Handler(Looper.getMainLooper())
thread = object:Thread(){
override fun run() {
while (!this.isInterrupted){
try{
//执行的任务
mHandler.post { FlutterPatch.findNewPatch(baseContext)}
sleep((10 * 60 * 1000).toLong())
}catch (e:InterruptedException){
interrupt();
e.printStackTrace();
}
}
}
}
thread.start();
}
override fun onDestroy() {
//关闭定时任务
mHandler.removeCallbacks(thread)
thread.interrupt();
super.onDestroy()
}
}
- FlutterPatch.java
public class FlutterPatch {
private static final String TAG = "FlutterPatch";
private static String libPathFromSophix = "";
/**
* 更新补丁
*/
public static void findNewPatch(Context context) {
Log.i(TAG, "--------------------版本1.0.0-0------------------");
Log.i(TAG, "--------------------补丁版本-0------------------");
SharedPreferences settings = context.getSharedPreferences("LoadNewPatch", 0);
long now = System.currentTimeMillis();
Log.i(TAG, "--当前时间-"+now);
long last = settings.getLong("LoadNewPatchTime", 0L);
Log.i(TAG, "--上一次更新时间-"+last);
if(last == 0L || timeCompare(last,now,80)) {
// 第一次进入app或者距离上次拉取时间大于80分钟 拉取更新
SophixManager.getInstance().queryAndLoadNewPatch();
}
}
///相差分钟
private static boolean timeCompare(long date1, long date2, int basicDiff) {
long nd = 1000 * 24 * 60 * 60;// 一天的毫秒数
long nh = 1000 * 60 * 60;// 一小时的毫秒数
long nm = 1000 * 60;// 一分钟的毫秒数
long ns = 1000;// 一秒钟的毫秒数
//格式化时间
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String startDate = simpleDateFormat.format(date1);//开始的时间戳
String endDate = simpleDateFormat.format(date2);//结束的时间戳
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
Date beginTime = df.parse(startDate);
Date endTime = df.parse(endDate);
assert endTime != null;
assert beginTime != null;
long diff = endTime.getTime() - beginTime.getTime();
long mins = diff / nm;
return mins >= basicDiff;
} catch (ParseException e) {
e.printStackTrace();
}
return false;
}
public static String findLibraryFromSophix(Context context, String relativePath, String libName) throws UnsatisfiedLinkError {
File file = new File(relativePath+"/libs/libapp.so");
if (file.exists() && !file.isDirectory()) {
libName = file.getAbsolutePath();
Log.i(TAG, "so路径 is " + libName);
} else {
Log.i(TAG, "so路径 is not exist");
}
return libName;
}
public static void copyFileByPath(String fromPath, String toPath) {
File fromFile = new File(fromPath);
File toFile = new File(toPath);
if (!fromFile.exists()) {
return;
}
if (!fromFile.isFile()) {
return;
}
if (!fromFile.canRead()) {
return;
}
if (!toFile.getParentFile().exists()) {
toFile.getParentFile().mkdirs();
}
if (toFile.exists()) {
toFile.delete();
}
try {
FileInputStream fosfrom = new FileInputStream(fromFile);
FileOutputStream fosto = new FileOutputStream(toFile);
byte[] bt = new byte[1024];
int c;
while((c=fosfrom.read(bt)) > 0){
fosto.write(bt,0,c);
}
//关闭输入、输出流
fosfrom.close();
fosto.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
findNewPatch中的版本日志是我用来修改基准包与补丁包原生代码差异的地方,毕竟flutter项目基本不会改变原生代码, 所以需要手动修改一下
打补丁包时需要去掉检查初始化勾选
参考链接
带你不到80行代码搞定Flutter热更新https://cloud.tencent.com/developer/article/1531498