I'm building a little Android game using libgdx. For now I have a copy of the game's assets in the desktop project folder and the Android project folder. For some strange reason I have to access those files differently in each of the two versions.
This works fine in the desktop app but gives me a FileNotFound exception in the android app:
Texture texture = new Texture(Gdx.files.internal("assets/someImage.png"));
If I remove the "assets" from the filename it's the other way round (Android works, desktop crashes):
Texture texture = new Texture(Gdx.files.internal("someImage.png"));
I'm not sure what the problem is. The folder structure is exactly the same for both projects... What is the right way to this with libgdx?
You should be storing all of your assets in the Android assets folder and linking your desktop project to that folder. There is a quick description of this at http://www.badlogicgames.com/wordpress/?p=1537
EDIT: The official Project Setup tutorial describes how to perform this as well. It's found at http://code.google.com/p/libgdx/wiki/ProjectSetup#Asset_folder_setup
Aside from project setup I believe that your second method is the correct way of referencing the assets from both projects. After you fix your setup it should work properly in both environments.
libGDX Wiki extract:
Asset folder setup
The Android project has a sub-folder named
assets
, which was created automatically. Files available to the Android application must be placed here. This is problematic, because these same files must be available to the desktop application. Rather than maintain two copies of all the files, the desktop project can be configured to find the assets in the Android project:Click the
Source
tab, clickLink Source
,Browse
, select the "assets" folder from your Android project and click OK.Specify "assets" for the folder name and click Finish then OK.
Note: If your desktop and Android projects are in the same parent folder, you can use "PARENT-1-PROJECT_LOC/gamename-android/assets" for the location of the linked assets folder, where "gamename-android" is the name of your Android project. This is better than a hard-coded path if you plan on sharing your projects with others.
Referencing the android assets folder is not a really good idea, because:
1) Eclipse will probly take copy your referenced assets folder to the apk of the android project, duplicating the size of 2) paths should be platform agnostics. As I see it, this is a JogFiles implementation issue.
You better copy the folloing class to your classpath:
After a lot of experimentation I discovered a way to get this to work:
For the android module, Gdx.files.internal is rooted in the android assets folder. For the desktop module, Gdx.files.internal is rooted in the toplevel project folder.
So if you put a symbolic link from files or directories in your android assets folder to just underneath your top level project folder (ie, parallel to android, core, desktop, etc. dirs) then the Gdx.files.internal will work for both.
Bonus tip - if using windows, you can use the mklink command at the cmd shell to create these:
(note windows cant handle relative paths in symlinks. also you have to run cmd as administrator)
make sure to sync your files if you are doing this while running, too.
Check if all of your files are in lower case in android assets folder
Assets folder of desktop project can be also chosen with Gradle - add this line to
build.gradle
indesktop
folder:Or, if you didn't generate an Android project:
This should properly link the folder in both Eclipse and IntelliJ when running
eclipse
/idea
Gradle tasks. Compared to creating links or manually adding the folder to source, I think this is the easiest to set up and manage by far.