Intro
Tried to loop through files inside an Gdx.files.internal()
folder, couldn’t because it returns a empty list? Here’s the solution.
General setup
Tl;dr: It is not possible to list files inside a .jar file, so .list()
isn’t going to work on .internal()
folders in desktop projects once you create a jar out of it.
To work around that problem, it’s possible to create an assets.txt
file which contains a list of all your assets.
Then all you need to do is parse that file and search in it for whatever you need.
Since I’m lazy and I don’t want to edit the file manually, I’ve created a little gradle script for that.
File
All the changes in this page are done inside the main build.gradle file. (projectfolder/build.gradle
)
Code
Add the following task:
- Standard setup app: above the
allprojects {...}
section - Liftoff: above the
configure(subprojects) {...}
section
1 | task writeAllAssetsToFile { |
That’s pretty much all you need to do.
Running the script
Whenever you add/remove a file, run the task with a terminal:
- Powershell:
.\gradlew.bat writeAllAssetsToFile
- Cmd:
gradlew.bat writeAllAssetsToFile
- Linux/Mac:
gradlew writeAllAssetsToFile
- Powershell:
Whenever you add/remove a file, run the task with your IDE. In IntelliJ you can double click the task in the gradle window:
Automatically run the task whenever you compile the project with gradle.
In my opinion this is the best way. You can tell gradle to always run the task before compiling the project. This ensures that the file is always up to date.
Just slap the following line either:
- Standard setup app: inside
project("core") {
section - Liftoff: inside the
configure(subprojects) {
section
- Standard setup app: inside
1 | compileJava.dependsOn writeAllAssetsToFile |
Standard setup app example
1 | buildscript { |
Liftoff example
1 | buildscript { |