How does Libgdx deal with natives?

I really like how Libgdx deals with natives. Don’t need to create fat jar or have a folder near the jar with natives.
How do they do it? :open_mouth:
Is there any tutorial like that?

I would imagine they either rewrite the natives to go into a jar or put them in a jar somehow,

this might help http://stackoverflow.com/questions/2937406/how-to-bundle-a-native-library-and-a-jni-library-inside-a-jar

Eclipse has several export modes, one of which is the Jar-in-Jar model I believe you are thinking of.
Upon opening a libGDX app in winRAR:

We see that the natives and libraries are packages as sub-jars, and that the main-class (the one that is first executed) is not the program main class, but a pre-loader supplied by Eclipse, with an extra Rsrc-Main-Class tag that tells the loader what the real main class is.

Jar in Jar loader source

No idea what that Rsrc-Main-Class stuff is, I guess it is something for Eclipse. It is not how libgdx handles natives.

libgdx extracts native libs from the JAR to a temp location and loads them from there. This removes the need to mess with java.library.path, which is a source of many problems for beginners. It uses a CRC to skip extraction if it is not needed and also for the extraction directory, so multiple apps with different versions of the same lib won’t conflict.

The class that does it is called SharedLibraryLoader:

Used like this:


new SharedLibraryLoader().load("nativeLibName");

You should have the natives in the JAR. I usually have a JAR per platform to reduce the size.


nativeLibName.dll
nativeLibName64.dll
libnativeLibName.dylib
libnativeLibName.so
libnativeLibName64.so
armnativeLibName.so
armnativeLibName64.so

It can be used outside of libgdx, eg:

libgdx also does things to make LWJGL native libs work in the same way (since LWJGL doesn’t work like normal native libs).

Thank you very much… Will use this for sure! :slight_smile:

EDIT-----
Umm wait so can I use that code in my projects? I didn’t really understand if I can use it from the license in the top of the class…