应用版本迭代有时候我们不得不进行大版本升级,让玩家下载最新的包进行覆盖安装掉原来的包,覆盖安装这个动作是操作系统处理的,所以我们要遵循系统规则才能以正确的姿势覆盖掉之前的应用包,这里面有些需要注意的点
- 安卓的覆盖安装涉及到签名以及 versionCode 和 versionName,要正常覆盖安装需要满足以下条件:a.签名一致,即.keystore文件要用同一个。b.versionCode值要比之前的包大(versionCode是给机器看的,versionName虽然是给人看的,但一般值也都比之前包设定的大)
- Unity覆盖安装时Application.persistentDataPath目录并不会被删除,这个目录不仅存放Unity的缓存数据,还存放我们的更新资源,所以覆盖安装的时候为了避免读取到旧包更新下来的资源,我从两方面去规避:
private const string APPLICATION_RESDIR_KEY = "ApplicationResDirKey"; IEnumerator CheckSecondInstall() { if (!Application.isMobilePlatform) { yield break; } yield return new WaitForEndOfFrame(); string localResDir = PlayerPrefs.GetString(APPLICATION_RESDIR_KEY, string.Empty); if (string.IsNullOrEmpty(localResDir)) { PlayerPrefs.SetString(APPLICATION_RESDIR_KEY, Util.DataPath); //记录新版本目录 PlayerPrefs.Save(); } else { string curResDir = Util.DataPath; if (!localResDir.Equals(curResDir)) { if (!string.IsNullOrEmpty(localResDir) && Directory.Exists(localResDir)) { Directory.Delete(localResDir, true); //删除旧资源存放目录 } PlayerPrefs.SetString(APPLICATION_RESDIR_KEY, curResDir); PlayerPrefs.Save(); } } } //Util.DataPath /// <summary> /// 数据存放目录 /// </summary> public static string DataPath { get { string game = AppConst.AppName.ToLower(); game = game + Application.version; //目录名关联版本 if (Application.isMobilePlatform) { return string.Format("{0}/{1}/", Application.persistentDataPath, game); } if (AppConst.DebugMode) { return string.Format("{0}/{1}/", Application.dataPath, AppConst.AssetDir); } if (Application.platform == RuntimePlatform.OSXEditor) { int i = Application.dataPath.LastIndexOf('/'); return Application.dataPath.Substring(0, i + 1) + game + "/"; } return "c:/" + game + "/"; } }
程序启动的时候去调用协程函数CheckSecondInstall(),这里面删除了旧的更新资源存放目录并记录新的资源存放目录Util.DataPath。Util.DataPath关联了打包时的版本亦可避免去旧的更新资源目录去读取资源。