Android APK Expansion Files with cocos2d-x v3

Hi,
I would like use android APK Expansion Files with cocos2d-x v3, is there anyone have any experience dealing with APK Expansion files on Android with cocos2d-x v3 ? Please orient me to do it.
Thanks.

Any help please :frowning:

You can just put all assets into anther package, which means the assets will not in .apk after installing. Then you can use FileUtils::setSearchPath() to set the real path of the assets installed.

Thanks, but how i can get the path of my expansion file from c++?

I think you can read Android document to know if you should download expansion by yourself or it will download it for it automatically.

If you should download it by yourself, then you know the path. If Android download it automatically, then i think it will have an API to get the path.

Thanks @zhangxm, can i set zip file to FileUtils::setSearchPath() ?

No, you can’t.

How about .obb file , can i set it to FileUtils::setSearchPath() ?

In fact, you can set anything to FileUtils::setSearchPath(), but engine can not read file into it if it is not a general directory.

Hello yagoub,

Did you manage to android APK Expansion Files with cocos2d-x v3 ?

Thank you

Yes, i have used the zip file, because the obb file wasn’t good

Hi @zhangxm

I also try to use an apk expansion file with cocos2dx 3.0, but I can’t get it working. My current way is:

In AppActivity.java:

// unzip the .obb file and copy the files in  "/data/data/<package name>/files/resources" folder.

In cpp code I tried different things:

// add "/data/data/<package name>/files/resources" to searchPath

std::stringstream ss;
ss << cocos2d::FileUtils::getInstance()->getWritablePath() << "resources";
std::string resourceDir = ss.str();

std::vector<std::string> searchPaths;
searchPaths.push_back(resourceDir);
cocos2d::FileUtils::getInstance()->setSearchPaths(searchPaths);

load resource with absolute path like:

Sprite::create("/data/data/<package name>/files/resources/test.png"));

but both don’t work.

Any ideas what I am doing wrong?

cocos2d::FileUtils::getInstance()->getWritablePath() is not /data/data//files/.
You should set correct search path.

Sorry, the forum didn’t show my post correct. I update it. I know that

cocos2d::FileUtils::getInstance()->getWritablePath()

is

/data/data/<package-name>/files/

so i’m wondering that FileUtils don’t find my unziped files in the

/data/data/<package-name>/files/resources

folder.

In AppActivity.java I check if the unzip und copy actions were successful by loading one of the files:

// after unzip and copy resources
File tmp = new File("/data/data/<package-name>/files/resources/test.png");
if (tmp.exists()) {
    Log.i("Resources", "Resources installation successful.");
}

and it works. So i thought I have to add this path to FileUtil searchPath.

btw, I know that unzipping and copying the files is not the best way to handle the expansion files. But I can’t find any documentation for cocos2dx 3.0 to do that. Is there any API in cocos2dx to handle the expansions?

1- unzip your expansion file to outPutDirPath

private final static String unzip_Dir_Path="/Android/data/"
final String outPutDirPath=Environment.getExternalStorageDirectory()+unzip_Dir_Path+packageName;

and set your SearchPath() to

outPutDirPath+"/" + "main." + xAPKS[0].mFileVersion + "." + packageName + ".obb"

because when you upload your expansion file to the google console developer you have to name your expansion file like

 "main." + mFileVersion + "." + packageName + ".obb"

Hi @yagoub,

I’m not sure if I understand you. Let’s do a simple example.

package name = com.test.app
obb = main.000001.com.test.app.obb

lets create a new folder for the resources called “resources”
so my output directory is:

outPutDirPath=Environment.getExternalStorageDirectory() + "/Android/data/com.test.app/resources"

now I unzip and copy the files from obb file located in /Android/obb/com.test.app/main.000001.com.test.app.obb to the output directory. Everything’s fine!

But now I am a little bit confused. You say I have to add the obb file to the searchPath

cocos2d::FileUtils::getInstance()->addSearchPath(outPutDirPath+"/main.000001.com.test.app.obb");

Are you sure I have to put the path to the obb file to searchPath? If yes, do I really have to unzip it first?
Or do I have to add the new resource directory to searchPath just like this

cocos2d::FileUtils::getInstance()->addSearchPath(outPutDirPath);

Thanks for your help!

Yes you have to unzip the .obb file (actually is a zip file) and if you add resources directory, so your SearchPath is

cocos2d::FileUtils::getInstance()->addSearchPath(outPutDirPath+"/main.000001.com.test.app.obb/resources");

and when you cereate a new sprite just you put the name

Sprite::create("test.png");

OMG, I’m so sorry. It was totally my fault! My .obb file was corrupted! I’m working on Mac and there were some hidden MacOS files in the archive. So the unzip aborts at the very end and i didn’t notice. Arg!!!

I fixed the archive and everything works perfect. Puh, sorry again!

But i still think to copy the archives files isn’t the best way. Do you know if its possible to use the .obb file directly? I noticed that FileUtils has a function getFileDataFromZip but I can’t find any code that uses this function.

No, i don’t know, and if you find something let me know please .

@yagoub Yes, of course!

But i’m currently stucking again. I’m doing the downloading und licencing stuff and I think it will work well. I have followed this guideline http://developer.android.com/google/play/expansion-files.html and take example code from SampleDownloaderActivity.java But my question is how can i prevent the app to start cocos2dx stuff until the download or unzip an copy progress of the obb has complete. I think after initializing the download process the main thread will run out of the onCreate() Method of AppActivity.java and the cocos stuff will be loaded.

I get this error:

E/cocos2d-x assert(17840): /Users/.../proj.android/../cocos2d/cocos/2d/platform/android/CCApplication.cpp function:getInstance line:79

11-29 15:36:06.088: A/libc(17840): Fatal signal 11 (SIGSEGV) at 0x00000000 (code=1), thread 17856 (Thread-9118)