cocos2dx中android下動態更新.so檔案

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   ar   使用   

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檔案

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.