suikki / simpleSDL

A simple crossplatform libSDL cmake build environment example/test.
MIT License
47 stars 6 forks source link

Unable to run app #3

Open tbrosman opened 5 years ago

tbrosman commented 5 years ago

I am able to build and deploy the app, but when I run it the shared object library fails to load.

In SDLActivity.SDLMain:

        Log.v("SDL", "Running main function " + function + " from library " + library);
        SDLActivity.nativeRunMain(library, function, arguments);

        Log.v("SDL", "Finished main function");

nativeRunMain calls dlopen, which returns a null handle and I get this error:

03-23 15:54:20.564 12504 12530 E SDL     : nativeRunMain(): Couldn't load library libSimpleExample.so

I checked the library name being passed in to dlopen. It definitely exists in the correct arch in the APK. I'm wondering if this is an issue with my NDK version. I'm using the latest stable (19.2.5345600) though I got the same behavior with 19.1.5304403.

Here's a gist with the readelf dump: https://gist.github.com/tbrosman/73d0118f8c5c5f4ea4df9b73b62fbd2a

Any pointers on how to debug this would be greatly appreciated!

suikki commented 5 years ago

I haven't seen that before so I can't offer much help. SDL changed the way it loads libraries at some point and I haven't really looked more closely at what it's actually doing behind the scenes.

I tried a clean build by cloning the simpleSDL project, downloading SDL 2.0.9 release sources and the app built and ran without problems. My ndk version is a bit older 18.1.5063045 but unfortunately I don't have time right now to test with the newer ones.

if you are not using the SDL 2.0.9 release did you make sure to follow these instructions from the readme?:

NOTE:

Currently the SDL2 Android Java code is included in this project. This is not a very good system as it easily leads to the SDL Java and native code being out of sync (i.e. code from different versions of SDL). You should replace the Java sources from the version of SDL you are using to make sure they are from the same version.

(sources in question are in: simpleSDL/platforms/android/android-app/src/main/java/org/libsdl/app)

tbrosman commented 5 years ago

Ah, thank you for pointing out the last part of the instructions. I somehow skipped it the first time I tried this and only populated the contrib folder. Now that I have the java sources copied into /android-app/... I get a slight variation of the error (note: still NDK 19):

2019-03-27 21:39:19.926 9479-9500/com.my.exampleapp E/SDL: nativeRunMain(): Couldn't load library /data/app/com.my.exampleapp-9zTUcNSFntdfjyjvD9dLKw==/lib/x86/libSimpleExample.so

Followed by:

2019-03-27 21:39:21.502 9479-9479/com.my.exampleapp E/m.my.exampleap: No implementation found for void org.libsdl.app.HIDDeviceManager.HIDDeviceReleaseCallback() (tried Java_org_libsdl_app_HIDDeviceManager_HIDDeviceReleaseCallback and Java_org_libsdl_app_HIDDeviceManager_HIDDeviceReleaseCallback__)

The next thing I tried was downgrading my NDK to 18.1.5063045. I got a CMake error saying PkgConfig is missing. Which CMake version are you using?

I appreciate the help! I'm using simpleSDL to troubleshoot my setup so I can build a different app. Currently it fails with the same runtime error.

swinginbird commented 5 years ago

Hi there, it seems like I ran into the very same problem.

My output is as follows:

E/SDL: nativeRunMain(): Couldn't load library /data/app/com.my.exampleapp-0ExxGdcQbCtcnkKMHga-3Q==/lib/arm/libSimpleExample.so E/libc: Access denied finding property "vendor.gralloc.enable_ahardware_buffer"

I downloaded SDL as stated in the readme.txt from https://www.libsdl.org/release/SDL2-2.0.9.zip

Any hints on how to progress on this?

suikki commented 5 years ago

I tried building simpleSDL with the latest android SDK and NDK without problems.

You said that dlopen is returning null. Are you using some additional native libs? Could this somehow be the reason: https://android-developers.googleblog.com/2016/06/improving-stability-with-private-cc.html

For all other libraries you’ll see a warning in logcat and a toast on the display. This will happen only if your app’s targetSdkVersion is less than N. If you change your manifest to target N, loading will fail: Java’s System.loadLibrary will throw, and C/C++’s dlopen(3) will return NULL.

I havent' looked if SDL already does this, but you could use dlerror mentioned in the above link to see what the reason was.

swinginbird commented 5 years ago

@suikki have you also tried running it using Android Studio?

suikki commented 5 years ago

@suikki have you also tried running it using Android Studio?

yes

swinginbird commented 5 years ago

@suikki have you also tried running it using Android Studio?

yes

Then there has to be an issue with my AS configuration, right? Would you kindly be able to send me your AS project?

suikki commented 5 years ago

AS is essentially running the same gradle stuff that you can run from the command line. Are you able to build from the command line? i.e. running something like "gradlew assembleDebug" in the dir that contains gradlew

Well here is the project I was using earlier to test: simpleSDL-master.zip

Note that android studio creates files like "local.properties" that contain paths to my android sdk installation dir

tbrosman commented 5 years ago

@suikki In my case dlopen returns null, but dlerror returns null. It is strange there is no dynamic linker error. As for my project setup vs. yours, the only thing different I saw was the gradle version: I was using 3.3.0 (yours is 3.3.2). I updated and still got the same result.

Could this possibly be a difference in test device/Android OS version? I'm using an emulator running Android 9/API 28. It's simulating a Nexus 5X, though I doubt that part matters. What are you using? If it's an older OS version (e.g. <= API 23) then maybe the reason given in that blog post is why dlopen is returning null.

tbrosman commented 5 years ago

@suikki FYI, I tested on a couple more virtual devices. I get the same behavior with an Android OS at 5.1 (API 22) and 6.0 (API 23). That disproves my working theory. Can you also post the APK you built?

suikki commented 5 years ago

I deleted the android build dir when I created the zip earlier and I was just going to build again and upload the apk, but I thought I should test if it actually still works and now I'm getting the same error. Maybe I didn't do a proper clean rebuild after updating the SDK and NDK (as it was working after the update earlier). I definitely have not changed anything else and now dlopen is returning null for me too.

I tried printing dlerror and enabling some library loading debug prints mentioned here: https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md

I was thinking that maybe the problem is that newer android build tools make apks that don't extract the .so files from the apk, but use the from inside the apk. And maybe that is why the loading fails: https://medium.com/androiddevelopers/smallerapk-part-8-native-libraries-open-from-apk-fc22713861ff

I tried changing the value of the manifest value that is mentioned in the link, but it didn't seem to help, but I haven't had time to debug this properly.

2bt commented 5 years ago

Hi there! Thanks, @suikki, for sharing this project with the world. I have also run into the same problem. It first worked and I could run the app on my phone without problems. After some fiddling around it suddenly stopped working. I first thought I had broken something but that wasn't it. Both SDL2-2.0.8 and SDL2-2.0.9 give basically the same error (with the relative and absolute library path, respectively).

I am not sure, but i think there was an android studio update between it working and not working anymore. Could that be it?

tbrosman commented 5 years ago

@2bt What version of Android Studio are you using? My setup (I'm still hitting the same issue):

2bt commented 5 years ago

I don't really know what NDK version I use. I am a total android noob.

tbrosman commented 5 years ago

@2bt To find the NDK version in Android Studio:

2bt commented 5 years ago

Thanks, @tbrosman! It's 19.2.5345600, just like yours.

Gh0stBlade commented 4 years ago

So, did someone find a fix for this because now I'm hitting this error as well.

Android Studio Version: 3.5.3 NDK Version: 20.1.5948944

:/

SimplyLiz commented 4 years ago

i have the same problem.

I cloned your repo and downloaded the SDL 2.0.9 source with no other changes. Your project seems to auto download NDK 20.

It compiles with ./gradlew assemble and builds an apk, but it instantly quits. adb logcat shows this error.

2020-04-18 13:26:58.360 32136-32177/? E/SDL: nativeRunMain(): Couldn't load library /data/app/com.my.exampleapp-cQgW9QhZg0APkAZIo4wcOg==/lib/arm64/libSimpleExample.so

Edit: Also does not work in the emulator

SimplyLiz commented 4 years ago

i figured out, that this is related to the android ndk. I've tried again with android-ndk-r18b and it's working.