-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Description
Issue Description:
Generally, to use raylib in an Android project, you have two options, both "demonstrated" in raylib-game-template:
-
Compile raylib as a static library and link your code against it. You can either load your generated shared library using
NativeActivity
or a custom Java class. This works fine, and the app launches properly. -
Compile raylib as a shared library and link your code against it. In this case, you need a custom Java class to load the two shared libraries since
NativeActivity
by default accepts only one library. A custom loader might look like this:
package com.company.app;
public class NativeLoader extends android.app.NativeActivity {
static {
System.loadLibrary("raylib");
System.loadLibrary("main");
}
}
However, this approach does not work, and the app crashes immediately after launch.
Possible Cause:
After trying multiple Java native loader implementations from the web, I believe the issue is not caused by Java, but rather by raylib itself.
Android requires native apps to use android_main()
. Raylib, to improve portability and allow Android users to write a regular main()
, handles android_main()
internally and calls the user-defined main()
function.
This creates a circular dependency:
libmain
depends on symbols fromlibraylib
.libraylib
“depends” onmain
fromlibmain
.
In the case of static linking, this isn’t a problem because the libraries are merged. However, with dynamic linking, each library is loaded separately at runtime, and libraries that depend on each other must be loaded in a specific order. In this case, libraylib.so
and libmain.so
depend on each other, making it impossible to satisfy the load order. As a result, the app crashes immediately after launch.
Additional Info:
To prove that this is not a Java issue, I tested with an external standalone library (Box2D), and everything worked fine. Here’s the loader for that case:
package com.company.app;
public class NativeLoader extends android.app.NativeActivity {
static {
System.loadLibrary("box2d");
System.loadLibrary("main"); // my code + raylib static library
}
}