From ec68bed970dcc79d2d9b87fd482e2d452fef61a2 Mon Sep 17 00:00:00 2001 From: hellobody Date: Tue, 11 Apr 2017 16:25:14 +0300 Subject: [PATCH 1/2] fix to problem with updating APK in Google Play keeping OBB from previous version This patch fixes the problem when we are unable to keep OBB from previous version when we are uploading new .apk to Google Play. It was discussed here: http://discuss.cocos2d-x.org/t/problem-with-updating-apk-in-google-play-keeping-obb-from-previous-version-because-versioncode-changes/36039 --- .../src/org/cocos2dx/lib/Cocos2dxHelper.java | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) 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..40d827eaf9f2 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.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; } From 6f021875d4fd0ae2fc37a9816e13108b608178ce Mon Sep 17 00:00:00 2001 From: hellobody Date: Wed, 12 Apr 2017 16:48:09 +0300 Subject: [PATCH 2/2] check for null pointer added Found an issue which could lead to a crash because of null pointer exception. If pathToOBB doesn't exist then File.list will return null which will cause fileNames array be null. Now it is fixed. --- .../android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 40d827eaf9f2..d92c56245f7d 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java @@ -197,7 +197,7 @@ public static String getAssetsPath() String pathToOBB = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + Cocos2dxHelper.sPackageName; - // Listing all files inside the folder (pathToOBB) where OBB files are expected to be found. + // 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. @@ -205,7 +205,7 @@ public boolean accept(File dir, String name) { }); String fullPathToOBB = ""; - if (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. + 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);