Skip to content

Latest commit

 

History

History
87 lines (63 loc) · 1.9 KB

File metadata and controls

87 lines (63 loc) · 1.9 KB

SDL Library Loading Fix

Problem

dlopen failed: library "libmain.so" not found

SDL's SDLActivity looks for a library named libmain.so by default, but our CMake was building libswrenderersample.so.

Solution

1. CMakeLists.txt

Changed the library target name from ${CMAKE_PROJECT_NAME} to main:

# Before (Wrong)
add_library(${CMAKE_PROJECT_NAME} SHARED native-lib.cpp)
# Creates: libswrenderersample.so ❌

# After (Correct)
add_library(main SHARED native-lib.cpp)
# Creates: libmain.so ✅

2. MainActivity.kt

Removed manual library loading since SDLActivity does it automatically:

// Before (Unnecessary)
companion object {
    init {
        System.loadLibrary("swrenderersample")
    }
}

// After (Automatic)
// SDLActivity loads libmain.so automatically ✅

How SDL Loads Libraries

SDLActivity has this behavior:

  1. Looks for libmain.so in the APK
  2. Loads libmain.so automatically on startup
  3. Calls SDL_main() function from that library
  4. Also loads libSDL3.so which libmain.so depends on

Verification

Check the APK contains the right libraries:

unzip -l app/build/outputs/apk/debug/app-debug.apk | grep "\.so$"

Should show:

lib/arm64-v8a/libSDL3.so
lib/arm64-v8a/libmain.so
lib/armeabi-v7a/libSDL3.so
lib/armeabi-v7a/libmain.so
...

Alternative Approach (If You Want Custom Name)

If you want to keep a custom library name, you can override in SDLActivity:

class MainActivity : SDLActivity() {
    override fun getLibraries(): Array<String> {
        return arrayOf("SDL3", "yourcustomname")
    }
}

But the standard approach is to just use libmain.so. ✅

Status

Fixed - The app now builds libmain.so correctly
Packaged - Both libSDL3.so and libmain.so are in the APK
Loading - SDL will automatically load the library on startup

Ready to run! 🚀