cocos2dx中android下动态更新.so文件
作者:HU
转载请注明,原文链接:http://www.cnblogs.com/xioapingguo/p/4037595.html
因为没用lua脚本写游戏,所以每次发布出去后,发现在bug,需要更新APK重新安装,严重影响体验,增加玩家流失率。如果使用直接更新.so文件的话,就可以解决这个问题。
1、下载.so文件,使用游戏本身的资源更新方法,下载下来,到文件的file/res目录
.so文件zip压缩一下会小很多,解压方法参考http://www.cnblogs.com/xioapingguo/p/4037323.html
2、把.so文件拷贝到 存放目录(根据自己喜好,不拷贝也可以),这里我将存到file/libs/目录中,把原来目录file/res中的libgame.so删除
文件拷贝方法:
public static void copyFile(File sourceFile,File targetFile)throws IOException{
if(!targetFile.exists()){
targetFile.getParentFile().mkdirs();
targetFile.createNewFile();
}
FileChannel fc1 = null;
FileChannel fc2 = null;
try
{
fc1 = new FileInputStream(sourceFile).getChannel();
fc2 = new FileOutputStream(targetFile).getChannel();
fc2.transferFrom(fc1, 0L, fc1.size());
}
finally
{
if(fc1!=null)
{
fc1.close();
}
if(fc2!=null)
{
fc2.close();
}
}
}
3、判断是否要重启游戏(.so文件更新后要重启游戏,因为下载资源是在之前的.so文件里执行的,如果想要不重启游戏,必须在java中做资源更新)
因为在小米系统中ALARM_SERVICE是不准的,所以重启有可能是不会成功,只要提示用户自己手动重启了,目前没有解决办法。
public static boolean isNeedRestratApplication() throws IOException
{
String str = sContext.getFilesDir()+"/libs/libgame.so";
File updateFile = new File(sContext.getFilesDir()+"/res/libgame.so");
Log.d(TAG, "str="+str);
if(updateFile.exists())//检查是否有更新
{
Log.d(TAG,"copyFile");
copyFile(updateFile,new File(str));//拷贝文件
updateFile.delete();//删除原来的文件
}
else
{
Log.d(TAG,"return");
return false;
}
//开始重启游戏
str = Cocos2dxHelper.getCocos2dxPackageName();
Intent localIntent = new Intent("android.intent.action.MAIN");
localIntent.addCategory("android.intent.category.LAUNCHER");
localIntent.setComponent(new ComponentName(str,"org.cocos2dx.cpp.AppActivity"));
localIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Context localContext = Cocos2dxActivity.getContext();
PendingIntent localPendingIntent = PendingIntent.getActivity(localContext, (int)System.currentTimeMillis(), localIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager localAlarmManager = (AlarmManager)localContext.getSystemService(android.content.Context.ALARM_SERVICE);
localAlarmManager.set(AlarmManager.RTC, 1000L+System.currentTimeMillis(), localPendingIntent);//延时1秒钟
Process.sendSignal(Process.myPid(),Process.SIGNAL_QUIT);
return true;
}
4、修改Cocos2dxActivity.java下,让游戏使用下载的.so文件运行游戏。(cocos2d-x 3.2下,其它版本按具体情况修改)
protected void onLoadNativeLibraries() {
try {
ApplicationInfo ai = getPackageManager().getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA);
Bundle bundle = ai.metaData;
String libName = bundle.getString("android.app.lib_name");
//System.loadLibrary(libName);
File file = new File(getFilesDir().getAbsolutePath()+ "/libs/libgame.so");//下载到的.so文件,如果不存在,则使用原来安装时的
Log.d(TAG, "onLoadNativeLibraries =" + getFilesDir().getAbsolutePath()+ "libs/libgame.so"+" isexists="+file.exists());
if (file.exists()) {
try
{
System.load(file.getAbsolutePath());
}
catch(UnsatisfiedLinkError err)
{
Log.d(TAG, "onLoadNativeLibraries = fail");
}
} else {
System.loadLibrary(libName);
}
} catch (Exception e) {
e.printStackTrace();
}
}
cocos2dx中android下动态更新.so文件的更多相关文章
- 【转载】cocos2dx 中 Android NDK 加载动态库的问题
原文地址:http://blog.csdn.net/sozell/article/details/10551309 cocos2dx 中 Android NDK 加载动态库的问题 闲聊 最近在接入各 ...
- Android下使用InputStream读取文件
在Android下使用InputStream读取文件. 如果不是从头开始读取文件,使用skip 后 在读取文件 使用read读取的长度为-1会获取不到数据. 换成RandomAccessFile 使用 ...
- 在Android中实现service动态更新UI界面
之前曾介绍过Android的UI设计与后台线程交互,据Android API的介绍,service一般是在后台运行的,没有界面的.那么如何实现service动态更新UI界面呢?案例:通过service ...
- android中实现service动态更新UI界面
案例:通过service向远程服务器发送请求,根据服务器返回的结果动态更新主程序UI界面,主程序可实时关闭或重启服务. 注册BroadcastReceiver 在主程序activity中注册一个Bro ...
- Android ListView动态更新数据
ListView就是可以显示一行行Item的控件,有时候数据非常多,通常需要分页显示,但为了减少用户的输入,我们可以动态更新ListView,把下一页要显示的数据的添加到当前ListView中. 先看 ...
- 【linux】linux下动态库so文件的一些认识
来源:http://mypyg.iteye.com/blog/845915 so其实就是shared object的意思.今天看了上面的博客,感觉好吃力.赶紧做个笔记记录一下.下面的内容大多都是连接中 ...
- 二、Cocos2dx中Android部分的c++和java实现相互调用(高级篇)
本文由qinning199原创,转载请注明:http://www.cocos2dx.net/?p=97 本文目的 要完成在cocos2dx的场景上一个点击事件,传递一个消息到java层,下面让我们看看 ...
- Gradle 载入中 Android 下一个.so档
1.在project下新建 jni/libs 目录 . jni 是和原来的libs 同级 ,将全部的.so文件放入 新建的libs文件下 2.在build.gradle 文件里新增下面内容到a ...
- Android中res下anim和animator文件夹区别与总结
1.anim文件夹 anim文件夹下存放tween animation(补间动画)和frame animation(逐帧动画) 逐帧动画: ①在animation-list中使用item定义动画的全部 ...
随机推荐
- C#和.net之间的关系
What is the difference between C# and .NET? In addition to what Andrew said, it is worth noting that ...
- poj 3414 Pots ( bfs )
题目:http://poj.org/problem?id=3414 题意:给出了两个瓶子的容量A,B, 以及一个目标水量C, 对A.B可以有如下操作: FILL(i) fill the ...
- The only legal comparisons are between two numbers, two strings, or two dates.
The only legal comparisons are between two numbers, two strings, or two dates. Left hand operand is ...
- NLog 传递参数
用NLog记文件日志,一般都用{$basedir}变量,把日志记在运行的目录或者它的子目录下,遇到要写在其他目录的下,看了下Nlog找到用环境变量传参数. .net 里写 Environment.Se ...
- [转]jBoss事务控制
转自:http://blog.csdn.net/trendgrucee/article/details/8545512 一.基础知识 1.JTA,即Java Transaction API,译为J ...
- jquery 图片上传本地预览V1.2
基于JQUERY扩展,图片上传预览插件 目前兼容浏览器(IE 谷歌 火狐) 不支持safari 代码进行小小的压缩 如果看源码 自己解压就行了 版本已升级 修复jquery版本问题 支持任意jqu ...
- 也用 Log4Net 之将日志记录到数据库的配置 (一)
也用 Log4Net 之将日志记录到数据库的配置 (一) 前段时间我一直想做一个通用一点的日志记录系统,可以便于不同的业务组调用进行日志记录及分析.本来打算着自己下手写一个,后面发现各业务组可能会需 ...
- web旋转式
为了获取客户.回馈客户,平台一般会推出抽奖活动类的营销页.因此web页面中,有各式各样的抽奖效果. 格子式(九宫格),背景滚动式(数字/文字/图案),旋转式(转盘),游戏式(砸蛋/拼图...).... ...
- opencv开发的程序分发给客户时所需要的dll文件
这里主要讲在其他裸机,没有搭建开发环境机器上运行自己开发的程序. 为了测试,我专门用visualbox搭建了一个虚拟机(主机和虚拟机都是win7系统) 在发给别人程序运行出现错误:msvcp100d. ...
- 发布代码小助手V2.1发布了——Code2HTML工具
设计起源: 新浪博客似乎没有插入代码的功能,所以不得不用打空格的方法格式化代码.而且没法显示行号. 描述: 发布代码小助手用python和Tkinter开发,可以在任何常见操作系统上运行.主要用于在不 ...