AppUpdate
Cocos应用更新:包含整包更新和热更新
实现了多线程下载、断点续传、md5包体校验等功能。
商品链接
https://store.cocos.com/app/detail/6980
使用说明
整包更新配置
Android
配置Gradle
在./native/engine/android/app 的 build.gradle 文件下添加第三方库依赖
implementation 'com.squareup.okhttp3:okhttp:3.5.0'
implementation "androidx.core:core:1.3.0"
配置AndroidManifest.xml
在./native/engine/android/app 的 AndroidManifest.xml 文件下添加权限
<!-- 整包更新申请权限 start -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<!-- 整包更新申请权限 end -->
在application里加入
<!-- 整包更新配置更新整包权限 start -->
<!-- 需要手动替换android:authorities更改为项目包名,例如:com.cocos.xxxx -->
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.cocos.update"
android:exported="false"
android:grantUriPermissions="true"
>
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
<!-- 整包更新配置更新整包权限 end -->
添加file_paths.xml文件
在./native/engine/android/res/xml 添加file_paths.xml 文件
内容为
<?xml version="1.0" encoding="utf-8"?>
<resources>
<external-cache-path path="/download" name="download"/>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</resources>
添加更新代码文件
将native/engine/android/app/src/com/cocos/libupdate整个文件夹放入项目中的相应位置中。
在AppActivity.java添加代码
在native/engine/android/app/src/com/cocos/game/AppActivity.java的onCreate函数加入
// 整包更新初始化start
Permission.getInstance().init(this);
ApkUpdate.getInstance().init(this);
// 整包更新初始化end
onRequestPermissionsResult函数加入
// 整包更新权限返回start
Permission.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults);
// 整包更新权限返回end
iOS
AppDelegate.mm添加代码
在native/engine/ios/AppDelegate.mm 里添加
+(NSString *)getVersion{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [infoDictionary objectForKey:@"CFBundleVersion"];
}
+(NSString *)getVersionName{
NSDictionary *infoDictionary = [[NSBundle mainBundle] infoDictionary];
return [infoDictionary objectForKey:@"CFBundleShortVersionString"];
}
+(void)openURL:(NSString *)url {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:url] options:@{} completionHandler: nil];
}
工程资源分析
界面
| 界面 | 用途 |
|---|---|
| loadUpdatePanel | 更新进度条界面 |
| updatePanel | 更新内容展示界面 |
| updateTip | 更新提示框界面 |
代码
| 文件夹 | 用途 |
|---|---|
| framework | 开发框架代码 |
| plugin | 解压库插件 |
| update | 更新逻辑代码 |
| ui/update | 界面相关逻辑 |
服务器地址配置
在./assets/script/update/update_constant.ts 里配置服务器下载地址
启动更新检测
UpdateSdk.showUpdatePanel(() => {
//回调
console.log('热更结束');
});
热更工具接入
./tool 文件夹为更新工具,工具的使用方式,见工具文件夹tool/hotUpdate/hotUpdate.md
最佳实践
发布第一个版本1.0.0.1的Android包
- 配置构建环境
{
"cocosCreatorPath": "/Applications/Cocos/Creator/3.8.4/CocosCreator.app/Contents/MacOS/CocosCreator",
"remoteUrl": "https://preview.cocos.org/app-update/",
"projectPath": "/Users/daisy/git/Game/update/",
"updateInfo": "1、更新的信息/n2.asdfad/n3.asdfsdfss",
"versionString": "1.0",
"versionHotUpdate": "0",
"versionBig": "1"
}
-
构建Android工程
node ./tool/hotUpdate/runBuildHotUpdate.js -a -
修改
/native/engine/android/app/build.gradle文件下的
versionCode 1
versionName "1.0.0.1"
- 使用Android Studio 发布release版本生成1.0.0.1.apk
发布第二个版本1.0.0.1000的Android包
- 修改配置构建环境
{
"updateInfo": "1、更新的信息/n2.asdfad/n3.asdfsdfss",
"versionString": "1.0",
"versionHotUpdate": "0",
"versionBig": "1000"
}
-
构建Android工程
node ./tool/hotUpdate/runBuildHotUpdate.js -a -
修改
/native/engine/android/app/build.gradle文件下的
1. versionCode 1000
versionName "1.0.0.1000"
- 使用Android Studio 发布release版本生成1.0.0.1000.apk
- 计算APK 的md5码
node getmd5.js -md5 update-release.apk - 配置 version.json 文件
{
"serverVersion": 1000,
"upgradeInfo": "1.更新版本号1000...",
"appname": "更新Demo",
"updateUrl": "https://preview.cocos.org/app-update/android/update-release.apk",
"md5": "05b3a0c0502ff3d88dee65d885c8f600"
}
-
制作lastVerison.zip, 压缩
./build/android/data文件夹,重命名为lastVerison.zip -
上传文件version.json 、lastVerison.zip、update-release.apk 到服务器上。
发布第三个版本热更新包 1.0.1.1000
- 配置构建环境
{
"updateInfo": "1、更新的信息/n2.asdfad/n3.asdfsdfss",
"versionString": "1.0",
"versionHotUpdate": "1",
"versionBig": "1000"
}
-
制作热更新包
node ./tool/hotUpdate/runBuildHotUpdate.js -a -h -
./tool/hotUpdate/versionUpdate中,压缩1.0.1.1000 文件夹和lastVersion 文件夹,上传 hotUpdate.json、1.0.1.1000.zip 和 lastVersion.zip文件夹到服务器。
`