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();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM