What are my options for loading my game files for an android app?

Started by
9 comments, last by frob 8 years, 6 months ago

For my game engine, I have all my files in a folder structor (organised as folders within folders) that includes XML's, shaders & PNG's. I just discovered that I can't just drop the root folder (named data) into the Android res folder and have it compile.

What are my options at this point?

Advertisement

In my opinion you should use the AssetManager

Then you just need to create a folder named assets and drop your folder structure in there.
Then using the AssetManager you can load your files like this:


//Java code
AssetManager assetManager = myActivity.getAssets();

//Load the file from the shaders folder.
// Folder struct:
// - OtherFolders
// - res
// - assets
//     - Shaders
//        -myShader.txt
//The assets part is automatically added in the function below. So you don't need to work about it
InputStream fileInputStream = assetManager.open("Shader/myShader.txt"); 

Thanks noodleBowl. I added the "assets" folder to the "res" folder and added my "data" folder to the "assets" folder but I get this compile error.

Do I need to define the new "assets" folder somewhere? I'm compiling from the command line btw so everything I do is manual.


Buildfile: /home/development/svnLapCatGames/org.lapcatgames.template/build.xml

-set-mode-check:

-set-debug-files:

-check-env:
 [checkenv] Android SDK Tools Revision 24.4.0
 [checkenv] Installed at /home/Android/Sdk

-setup:
     [echo] Project Name: org.lapcatgames.template
  [gettype] Project Type: Application

-set-debug-mode:

-debug-obfuscation-check:

-pre-build:

-build-setup:
[getbuildtools] Using latest Build Tools: 23.0.1
     [echo] Resolving Build Target for org.lapcatgames.template...
[gettarget] Project Target:   Android 4.0.3
[gettarget] API level:        15
     [echo] ----------
     [echo] Creating output directories if needed...
     [echo] ----------
     [echo] Resolving Dependencies for org.lapcatgames.template...
[dependency] Library dependencies:
[dependency] No Libraries
[dependency] 
[dependency] ------------------
[dependency] API<=15: Adding annotations.jar to the classpath.
     [echo] ----------
     [echo] Building Libraries with 'debug'...
   [subant] No sub-builds to iterate on

-code-gen:
[mergemanifest] No changes in the AndroidManifest files.
     [echo] Handling aidl files...
     [aidl] No AIDL files to compile.
     [echo] ----------
     [echo] Handling RenderScript files...
     [echo] ----------
     [echo] Handling Resources...
     [aapt] Found new input file
     [aapt] Generating resource IDs...
     [aapt] invalid resource directory name: /home/development/svnLapCatGames/org.lapcatgames.template/res assets

BUILD FAILED
/home/Android/Sdk/tools/ant/build.xml:649: The following error occurred while executing this line:
/home/Android/Sdk/tools/ant/build.xml:694: null returned: 1

Never mind. I found some info that said the "assets" folder goes in the root directory of the project. It compiles now and the size of my *.APK file reflects the addition of the added assets.

Thanks for your help!

I would like also to ask for that question what if my project weights more than 100 MB? i saw that android apps are max 50 MB, which confuses me, because i would like not to download any additional files from internet.

I would like also to ask for that question what if my project weights more than 100 MB? i saw that android apps are max 50 MB, which confuses me, because i would like not to download any additional files from internet.

Well to be honest, I never really thought that the max application size you could have would be around that small.

I always thought that if a game was over 50mb you just needed to be on wi-fi to download it, since I have downloaded games that range from 1 to 2+ GBs

But officially it looks like you are in luck. It seems like Google increased the max application size to 4GB:

http://www.cnet.com/news/android-apps-max-size-shoots-from-50mb-to-4gb/

Dev blog:

http://android-developers.blogspot.com/2012/03/android-apps-break-50mb-barrier.html

The max size for your main apk used to be 50MB, but it was very recently (few weeks ago) raised to 100MB but only for apps that only target Android 4.0 and above.

However, you can also have two 'expansion apk' files which will be hosted by Google Play which can be 2GB each.

If you use expansion apk(s) then there's some extra code you'll have to write to handle the case where they're missing from the original download and you need to download them dynamically.

If you can avoid using expansion apk files then I believe your app can be downloaded over a cellular connection which might improve downloads a little, so if you're near that 100MB threshold it might be worth putting in the effort to get under it.

im using fopen (C++) to load files directly from sdcard/internal memory <- i would like not to change that but loading things through assets and then saving them to sd card seem pretty lame since game will take 2x more space than it should.

I dont want to change the loading code.

The ideal thing would be that apk (game only could be downloaded with all game files - without any additional download from my app.)

So during apk installation it could extract files to desired path, then delete the zipped content.

im using fopen (C++) to load files directly from sdcard/internal memory <- i would like not to change that but loading things through assets and then saving them to sd card seem pretty lame since game will take 2x more space than it should.

I dont want to change the loading code.

The ideal thing would be that apk (game only could be downloaded with all game files - without any additional download from my app.)

So during apk installation it could extract files to desired path, then delete the zipped content.

I'm pretty sure you can't delete the apk contents, I'm afraid.

One option would be for you to disable compression of your files within the apk. Certain extensions like .mp3 .zip, etc don't get compressed, so if you rename your game files to one of those then you can disable compression, alternatively you can get your hands dirty and figure out whereabouts in the build process the compression decision is made and turn it off completely or just for your particular file extensions.

With an uncompressed asset in your apk, you can use AAsset_openFileDescriptor to get a file descriptor for a package file, then you can use fdopen followed by fread/fclose. So you'd only have to rewrite your file opening code and not your file manipulation code which might be a less daunting prospect.

Obviously if the build process isn't compressing for you, then you probably ought to be compressing your game files yourself, plus it's worth noting that the docs don't really make promises that AAsset_openFileDescriptor will work. I'm pretty certain that AAsset_openFileDescriptor does always work for non-compressed main apk assets and that it's unlikely to ever change on future revisions, but that's very different from it actually being a guarantee in the documentation.

All that said, you're probably best introducing a layer of abstraction in your file loading code and just using AssetManager functions on Android.

Crap!,

I don't get why android devs won't allow you to unpack the apk to a specific destination then remove it.

This topic is closed to new replies.

Advertisement