Skip to content

[rcore][android] App crashes when trying to link raylib dynamically #5114

@OussamaTeyib

Description

@OussamaTeyib

Issue Description:

Generally, to use raylib in an Android project, you have two options, both "demonstrated" in raylib-game-template:

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

  2. 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 from libraylib.
  • libraylib “depends” on main from libmain.

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
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions