mozilla / rust-android-gradle

Apache License 2.0
1.03k stars 67 forks source link

java.lang.UnsatisfiedLinkError: dlopen failed: library "librust.so" not found #127

Closed opfromthestart closed 1 year ago

opfromthestart commented 1 year ago

I am trying to do a simple example at https://github.com/opfromthestart/openfunny. No matter what I do, I always get this error when running the application. The .so files appear in both the rust/target and app/build/rustJniLibs directories, but it always gives this error. I am new to android development and have had no success for multiple hours.

opfromthestart commented 1 year ago

I found a workaround by running ./gradlew installDebug but I would prefer a solution that works with closer to default Android Studio.

SergioRibera commented 1 year ago

@opfromthestart You have to specify the targetDirectory as this is because the rustJniLibs folder is empty, this is better explained here, but in summary add this

cargo {
  // ...
  targetDirectory = "../path/to/target/"
}

Note: If you have problems with the libc++_shared library, add this to your build.gradle

kirillzh commented 1 year ago

For what it's worth, we started seeing the same issue with Android Gradle Plugin 7.4.2+. AGP 7.4.0 worked fine without additional configuration.

opfromthestart commented 1 year ago

The files are being copied fine image but it still gives the error that it cannot find librust.so

kirillzh commented 1 year ago

This workaround seemed to work for me https://github.com/mozilla/rust-android-gradle/issues/118#issuecomment-1569407058

opfromthestart commented 1 year ago

That still does not work. I updated the repo to reflect my changes

opfromthestart commented 1 year ago

I fixed it by changing "cdylib" to "staticlib" and "dylib" It looks like I actually fixed it by temporarily adding , "build/rustJniLibs/android/x86" to sourceSets.main.jniLibs.srcDirs. And yes, this did still work when building for arm64.