Search code examples
android-studiolibgdxassets

Cannot find assets when running libgdx app on android, works fine on desktop


After getting out of a mode of procastinating, I've finally gotten to the item on the projects todo-list that says "Run on virtual device to see why it crashes".

My project is a libgdx application that I plan on porting to various platforms, the two most important ones being desktop and android. During development I've exclusively used the desktop launcher, as it's a lot easier to fire up when just checking minor things.

I did build an .apk at one point just to see if it'd run out of the box, but it didn't. Now that I've tried via the virtual device, this is what the log says:

com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: rifleman.png

The same goes for any other assets that my game uses. The files are all placed in projectname/android/assets, as I remember learning way back that this was the way to go.

The virtual device I tried with is the stock Nexus 5, but I tried running the .apk by uploading it to my actual phone with a crash right after start, and I assume the problem is the same and is therefore unrelated to the test-platform.

I am currently not using an assetmanager, as implementing this is scheduled for after getting the basic core mechanics up and running. In the mean time I'm loading them as such: new Pixmap(new FileHandle("rifleman.png"));

Using android-studio 2.2 on Linux Mint 17 Cinnamon.

Please comment if more info is needed.


Solution

  • Use Gdx.files.internal() instead of FileHandle().

    From filehandle(string) method info

    Creates a new absolute FileHandle for the file name. Use this for tools on the desktop that don't need any of the backends.

    Do not use this constructor in case you write something cross-platform.