diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java index 6edb15d2c027..d92c56245f7d 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -56,6 +56,7 @@ of this software and associated documentation files (the "Software"), to deal import java.io.IOException; import java.io.File; +import java.io.FilenameFilter; import java.io.UnsupportedEncodingException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; @@ -193,16 +194,23 @@ public static void init(final Activity activity) { public static String getAssetsPath() { if (Cocos2dxHelper.sAssetsPath == "") { - int versionCode = 1; - try { - versionCode = Cocos2dxHelper.sActivity.getPackageManager().getPackageInfo(Cocos2dxHelper.sPackageName, 0).versionCode; - } catch (NameNotFoundException e) { - e.printStackTrace(); - } - String pathToOBB = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + Cocos2dxHelper.sPackageName + "/main." + versionCode + "." + Cocos2dxHelper.sPackageName + ".obb"; - File obbFile = new File(pathToOBB); + + String pathToOBB = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + Cocos2dxHelper.sPackageName; + + // Listing all files inside the folder (pathToOBB) where OBB files are expected to be found. + String[] fileNames = new File(pathToOBB).list(new FilenameFilter() { // Using filter to pick up only main OBB file name. + public boolean accept(File dir, String name) { + return name.startsWith("main.") && name.endsWith(".obb"); // It's possible to filter only by extension here to get path to patch OBB file also. + } + }); + + String fullPathToOBB = ""; + if (fileNames != null && fileNames.length > 0) // If there is at least 1 element inside the array with OBB file names, then we may think fileNames[0] will have desired main OBB file name. + fullPathToOBB = pathToOBB + "/" + fileNames[0]; // Composing full file name for main OBB file. + + File obbFile = new File(fullPathToOBB); if (obbFile.exists()) - Cocos2dxHelper.sAssetsPath = pathToOBB; + Cocos2dxHelper.sAssetsPath = fullPathToOBB; else Cocos2dxHelper.sAssetsPath = Cocos2dxHelper.sActivity.getApplicationInfo().sourceDir; }