Dynamic Update. So file under Android in COCOS2DX

Source: Internet
Author: User

HU

Reprint Please specify, original link: http://www.cnblogs.com/xioapingguo/p/4037595.html

Because the Lua script does not write the game, so every time after the release, found in the bug, need to update the APK reinstall, seriously affect the experience, increase the player churn rate. If you use the direct update. So file, you can solve the problem.

1. Download the. So file, use the game's own resource Update method, download it down to the file's File/res directory

. so file zip compression will be much smaller, decompression method reference http://www.cnblogs.com/xioapingguo/p/4037323.html

2, copy the. So files to the storage directory (according to their preferences, do not copy also can), here I will be stored in the file/libs/directory, the original directory file/res libgame.so deleted

File Copy method:

 Public Static voidCopyFile (File sourcefile,file targetfile)throwsioexception{if(!targetfile.exists ())             {Targetfile.getparentfile (). Mkdirs ();         Targetfile.createnewfile (); } FileChannel FC1=NULL; FileChannel FC2=NULL; Try{FC1=NewFileInputStream (sourcefile). Getchannel (); FC2=NewFileOutputStream (targetfile). Getchannel (); Fc2.transferfrom (FC1,0L, Fc1.size ()); }         finally         {             if(fc1!=NULL) {fc1.close (); }             if(fc2!=NULL) {fc2.close (); }         }     }

3, determine whether to restart the game (. So file to restart the game, because the download resources are executed in the previous. So file, if you want to do not restart the game, you must do a resource update in Java)

Because Alarm_service is not allowed in the Xiaomi system, restarting may not be successful, as long as the user is prompted to manually restart, there is no workaround.

 Public Static BooleanIsneedrestratapplication ()throwsIOException {String str= Scontext.getfilesdir () + "/libs/libgame.so"; File Updatefile=NewFile (Scontext.getfilesdir () + "/res/libgame.so"); LOG.D (TAG,"Str=" +str); if(Updatefile.exists ())//Check if there is an update {LOG.D (TAG,"CopyFile"); CopyFile (Updatefile,NewFile (str));//Copy Files Updatefile.delete ();//delete original file}Else{log.d (TAG,"Return"); return false; }
Start restarting the game str
=Cocos2dxhelper.getcocos2dxpackagename (); Intent localintent=NewIntent ("Android.intent.action.MAIN"); Localintent.addcategory ("Android.intent.category.LAUNCHER"); Localintent.setcomponent (NewComponentName (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);//delay 1 seconds process.sendsignal (Process.mypid (), Process.signal_ QUIT); return true; }

4, modify the Cocos2dxactivity.java, let the game use the downloaded. So file to run the game. (Cocos2d-x 3.2, other versions are modified as per the specific circumstances)

protected voidonloadnativelibraries () {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=NewFile (Getfilesdir (). GetAbsolutePath () + "/libs/libgame.so")///download to the. So file, if it does not exist, use the original installation of the 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 (); }    }

Dynamic Update. So file under Android in COCOS2DX

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.