r/libgdx • u/Thaerious • Oct 31 '15
HALP... Adding external library to libgdx.
I thought I would try the libgdx package as a way of accessing opengl in java. I got everything installed and an example program working (the drop tutorial). So now I would like to use some of my own code with libgdx. However I am new to Gradle, though I have used ant and make extensively. I can not make heads or tails of how to include an external library into netbeans+gradle . Their forums directory is essentially useless for new users as it won't allow me to post there and all the topics I can find are a bit more advanced than "how to use an external jar file". Any help would be greatly appreciated.
2
u/jimeowan Nov 01 '15
Direct answer
Here's a relevant SO question ; apparently the most straightforward way to do it is to add
compile fileTree(dir: 'libs', include: ['*.jar'])"
to your root build.gradle file, inside the "core" project's dependencies block. That way any JAR you'll put in a "libs/" folder will be imported.
Additional advice
A better approach would be to turn your 3rd party lib into a Gradle or Maven project, so you can use it as a classic "group ID/artifact ID/version" dependency. There's no need to get your project into an "official repository": it will work if you enable your local repository in your game project, and run "gradle install" on your library to get it in your local repository.
If you never worked with such build toolchains it can feel like a hassle at first, but not having to copy JARs around is much more comfortable in the long term.
1
u/Thaerious Nov 01 '15
The first part (direct answer) I've done. It shows up as a dependency in netbeans but still doesn't recognize the library is there. It doesn't compile or auto-complete. I'll try the second part.
I have worded with build toolchains, extensively with both make and ant. This one seems wildly different. When I first started with ant I was able to go in blind and still get things going. Gradle seems like there is a rather large ramp-up in knowledge before you can use it.
1
u/jimeowan Nov 01 '15
I'm used to Eclipse so I can't help a lot with Netbeans, but your issue sounds weird... If the JAR appears among the dependencies there's no reason you can't reference its classes. Maybe there's just something to refresh somewhere... You must be close to have it working.
About Gradle: it builds upon both toolchains like Make/Ant and upon Maven, which is more ambitious/intricate in how you manage dependencies, so it can be a bit overwhelming at first indeed. But the basics are not that complicated really ; this article seems like a good initiation for the topic.
2
u/Northburns Oct 31 '15
I'm on mobile so I can't give you a super in depth answer, but hopefully this nudges you onto the right tracks.
With Maven and Gradle, the easiest way to add 3rd party libs to your project is to use the dependency management. This has several advantages, but I'll leave listing them as an excersice for the reader.
If the lib you want to use exists in any Maven compatible repository, adding itvto your Gradle project is as easy as copying a tiny id string to the appropriate place in the gradle file.
So: first go to eg. mvnrepository (.net?) and search for your desired lib. They provide a Gradle compatible snippet! Then check Gradle docs for the dependency "block" syntax and paste the dependency there. (You may need to update the project in your IDE).
Thing is, you won't touch a single jar file yourself. Gradle's dependency management handles all that jazz for you.
If for some reason the lib you want to use is not in any Maven compatible repository, then.. (I'm not trying to be funny here but I'm really beginning to be abit tired. I just got home from a halloweem party. But hopefully this stub post is of some use, anyway.. Cheers!)