Android obb expansion files

How to overwrite the loading of assets with the path of an .obb file.
Im intending to move all my assets to an obb file.
I look into the engine codes and found out that it is using a aaesstmanager from ndk to load the assets. Im using cocos2d-x 3.0 final.

I know that i can use getFileDataFromZip from FileUtils to retrieve the data. But I’m interested to know if it is possible to change the primary way of loading assets from the main APK to the main obb file.

please anyone who has experience about splitting apk share their knowledge.

I also want to know how to handle this issue, since google play only accept apk less than 50MB.

thank you.

For our project I packed all the data into the expansion file so the APK is only 3-5MB. Then I made a number of changes to the java code for loading audio and music as well as changed the c++ file loading code to support the zip file. I would love to improve our method, but it’s enough for the time being. The better method would be to check if an asset/file exists in the zip and then if not try the APK or vice versa. The expansion file is downloaded into a specific folder hierarchy so it is easy to reference.

If you want when I get some extra free time I can pull out some of the code and post a gist here. Our methods or code is probably not the best, but at least it works. I do have a couple resources I found to be helpful which I’ll find and edit this post with them.

Basically we’ve added support for downloading the expansion file in the manner Google suggests if it doesn’t exist, which was a major PITA to get working and still doesn’t work properly. Usually the expansion file downloads correctly and this isn’t an issue, and you can always have the customer uninstall and reinstall if the expansion file is missing.

You do have to upload a beta version APK of the app along with the expansion file(s) into the google developer portal in order to test it out the download process, but you should be able to drop the expansion .obb file in the correct directory and test locally.

Another option is to roll your own downloader and store the .zip file(s) in amazon S3 storage, for example. But then Google does provide you with the hosting for free.

Anyway, hopefully I’ll find some time later or tomorrow and post some code.

Thank you very much for your help.
Since i don’t have any experience about splitting apk, it’s very detailed and helpful.

I agree that Google’s expansion files are really PITA to implement. To make the OBB, i tried using the google’s method and tool to make it. I tried using the JOBB(http://developer.android.com/tools/help/jobb.html) to pack the OBB. But i seems to have a problem reading it later on. I read somewhere else that, the file can just be zipped, and change the extension to .obb. It works for me when i use the cocos2d::ZipFile to load that obb.

Cocos2d-x 3.0 is using the native c++ AssetManager code from Google NDK Library. And the Google’s AssetManager only provides the connection to the main APK file. It cannot be used for the additional OBBs. The OBBs are stored in the external storage file folder, which you need to write some codes in java and pass it to C++ using JNI.

To access the OBBs in native C++, i tried using the cocos2d::ZipFile in one of the files within the support sub-folder inside the engine codes. That file has to be included manually, as it is not added in the main “cocos2d.h”.

If you are using the cocos2d::ZipFile and most of you other codes are already using the native method of creation, which they internally use cocos2d::FileUtil to load their files. You will have the same problems as me. Most of my codes are now using the original create functions. If i were to change those codes now, i would need to change a lot of the codes and i have to prep the cross-platform changes as well.

Eariler in Cocos2d-x 2.x, i notice that the engine use the cocos2d::ZipFile to load from the apk. I believe it can be easily change the path to the OBB file path. But it is no longer available in the new engine. I hope they can think of an option to allow Android OBB file loading as well.

@stevetranby Can you post the java code to download the missing obb files?
as you said, it’s really a major PITA to get working.

Thank you very much.

It would be great if someone can write a tutorial on how to use OBB files in cocos2dx since it is one of the basic features.

Here’s a gist of files. Updated to remove any of our game’s info.

I’m probably missing a few things, hopefully this will help in some way until a full tutorial with proof of example test (published in store) can be created and written by myself or another.

NOTE: This was developed with Cocos2dx 2.2.x so there are no guarantees it will work at all with 3.x. I’ll do the tutorial to example test app when I get time with 3.x for both our game’s update as well as hopefully others like yourself.

To make things easier we put all assets (png, wav, ogg, plist, etc) into the expansion file so that it could specify single directory for looking up resources. However, I think with the 3.x asset management through file paths search order could be used instead. Also, expansion files are disabled on Amazon and any other Android platforms. Hopefully I can find time to create an empty game project and upload to google play for full testing along with a tutorial.

Also you need to import three of google’s libraries.

  • APK Expansion Zip Library
  • Downloader Library
  • Licensing Library

Resources

api docs
http://developer.android.com/google/play/expansion-files.html

adding licensing
http://developer.android.com/google/play/licensing/adding-licensing.html

upload w/out exp file first
https://support.google.com/googleplay/android-developer/answer/2481797?hl=en

Files and changes description

FileUtils.cpp: changed to use zip file and obb path if using extension files.
FileUtilsAndroid.cpp: changed to use zip file and obb path if using extension files.
Cocos2dxActivity.java: enable support for using expansion files and using new inits in the helper classes
Cocos2dxGLSurfaceView.java: Nothing needed here. I think the patch file showed changes to onPause/onResume in this class.
Cocos2dxHelper.java: setting the nativeAPKPath here and flag for using exp. files or not
Cocos2dxMusic.java: stream music using native APK zip
Cocos2dxSound.java: play audio using native APK zip
GameActivity.java: The meat of the game’s code changes. Interfacing with the changes to Cocos2dxActivity for delayed init is first methods. Last section of methods is where the downloading expansion files occurs. It’s kinda a mess, but implements the various callback methods to receive progress updates on the expansion file download status.
GameDownloadReceiver.java: pretty much Google’s version
GameDownloadService.java: pretty much Google’s version, just replace BASE64_PUBLIC_KEY_GAME data as well as SALT
GameLib.java: small class to use between Google, Amazon, Samsung, and DRM-Free versions

2 Likes

Hello,

Has anyone managed to get this to work on Cocos2D-X 3.x ?

Thank you

Hi,

Anyone try with working on cocos2dx version 3.x ? Is it working ?

Thanks

hello.
your project is very interest for me.
i tried to use your method for expansion ,but failed due to missing main.xml file.
so please send me your main.xml file.
thanks.

Added main.xml and strings.xml to the gist group.

./res/layout/main.xml
./res/values/strings.xml

I’m getting close to upgrading our android project to use the code migrated to 3.x, so I’ll hopefully get around to checking it works with NativeActivity and cocos2d-x 3.4 soon.