Skip to content

Commit 92e713b

Browse files
hellobodyminggo
authored andcommitted
fix to problem with updating APK in Google Play keeping old OBB (#17689)
* 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 * 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.
1 parent fb7a2fd commit 92e713b

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxHelper.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ of this software and associated documentation files (the "Software"), to deal
5555

5656
import java.io.IOException;
5757
import java.io.File;
58+
import java.io.FilenameFilter;
5859
import java.io.UnsupportedEncodingException;
5960
import java.lang.reflect.InvocationTargetException;
6061
import java.lang.reflect.Method;
@@ -192,16 +193,23 @@ public static void init(final Activity activity) {
192193
public static String getAssetsPath()
193194
{
194195
if (Cocos2dxHelper.sAssetsPath == "") {
195-
int versionCode = 1;
196-
try {
197-
versionCode = Cocos2dxHelper.sActivity.getPackageManager().getPackageInfo(Cocos2dxHelper.sPackageName, 0).versionCode;
198-
} catch (NameNotFoundException e) {
199-
e.printStackTrace();
200-
}
201-
String pathToOBB = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + Cocos2dxHelper.sPackageName + "/main." + versionCode + "." + Cocos2dxHelper.sPackageName + ".obb";
202-
File obbFile = new File(pathToOBB);
196+
197+
String pathToOBB = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Android/obb/" + Cocos2dxHelper.sPackageName;
198+
199+
// Listing all files inside the folder (pathToOBB) where OBB files are expected to be found.
200+
String[] fileNames = new File(pathToOBB).list(new FilenameFilter() { // Using filter to pick up only main OBB file name.
201+
public boolean accept(File dir, String name) {
202+
return name.startsWith("main.") && name.endsWith(".obb"); // It's possible to filter only by extension here to get path to patch OBB file also.
203+
}
204+
});
205+
206+
String fullPathToOBB = "";
207+
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.
208+
fullPathToOBB = pathToOBB + "/" + fileNames[0]; // Composing full file name for main OBB file.
209+
210+
File obbFile = new File(fullPathToOBB);
203211
if (obbFile.exists())
204-
Cocos2dxHelper.sAssetsPath = pathToOBB;
212+
Cocos2dxHelper.sAssetsPath = fullPathToOBB;
205213
else
206214
Cocos2dxHelper.sAssetsPath = Cocos2dxHelper.sActivity.getApplicationInfo().sourceDir;
207215
}

0 commit comments

Comments
 (0)