Managing assets between desktop and device version in libgdx
Asked Answered
B

6

10

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?

Baranowski answered 27/3, 2012 at 15:44 Comment(0)
S
10

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.

Stans answered 27/3, 2012 at 15:59 Comment(0)
M
9

Assets folder of desktop project can be also chosen with Gradle - add this line to build.gradle in desktop folder:

sourceSets.main.resources.srcDirs = [ "../android/assets/" ]

Or, if you didn't generate an Android project:

sourceSets.main.resources.srcDirs = [ "../core/assets/" ]

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.

Masochism answered 20/11, 2015 at 21:45 Comment(2)
That did it. This should be the accepted answer. Thanks!Affaire
Not sure why this solution is not described in libgdx official site, as it is the cleanest solution...Sanctus
A
3

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.

% cd project
% ln -s android/assets/sound sound
% ln -s android/assets/images images

Bonus tip - if using windows, you can use the mklink command at the cmd shell to create these:

mklink /d sound d:\project\android\assets\sound
mklink /d images d:\project\android\assets\images

(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.

Arnett answered 28/7, 2014 at 21:6 Comment(1)
This is the easiest way, works like a charm and is not IDE SpecificAnnulment
H
1

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, click Link 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.

Henbit answered 15/2, 2014 at 7:7 Comment(0)
L
0

Check if all of your files are in lower case in android assets folder

Leung answered 19/7, 2012 at 15:26 Comment(0)
A
0

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:


package com.badlogic.gdx.backends.jogl;
/*******************************************************************************
 * Copyright 2011 See AUTHORS file.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 ******************************************************************************/
package com.badlogic.gdx.backends.jogl;

import java.io.File;

import com.badlogic.gdx.Files;
import com.badlogic.gdx.files.FileHandle;

/**
 * Gdx class overrided in order to keep internal files paths platform independent.
 * Android search at "assets/" folder. And so we do here for jogl.
 */
final class JoglFiles implements Files {
    private static final String ASSETS_FOLDER = "assets";
    private final String externalPath = System.getProperty("user.home") + "/";

    @Override public FileHandle getFileHandle (String fileName, FileType type) {
        return new JoglFileHandle(fileName, type);
    }

    @Override public FileHandle classpath (String path) {
        return new JoglFileHandle(path, FileType.Classpath);
    }

    @Override public FileHandle internal (String path) {
        // append "asset/" prefix to those urls who did not already have it.
        if (!path.startsWith(ASSETS_FOLDER)) {
            path = ASSETS_FOLDER + File.separator + path;
        }
        return new JoglFileHandle(path, FileType.Internal);
    }

    @Override public FileHandle external (String path) {
        return new JoglFileHandle(path, FileType.External);
    }

    @Override public FileHandle absolute (String path) {
        return new JoglFileHandle(path, FileType.Absolute);
    }

    @Override public String getExternalStoragePath () {
        return externalPath;
    }

    @Override public boolean isExternalStorageAvailable () {
        return true;
    }
}

Affirmatory answered 26/7, 2012 at 22:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.