0

I am writing an app for android that interfaces with HID devices. The backend is written in Rust using hidapi-rs and uses Jetpack Compose for the UI. I compile my rust library into a shared library using rust-android-gradle and can successfully load the library and call from it in the app if I comment out all of the HID-related stuff, but as soon as I include the HID-realated stuff, my app crashes as soon as the library is loaded using System.loadLibrary("MyLibrary"). It returns a runtime error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.me.myapp, PID: 24129
    java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "libusb_init" referenced by "libMyLibrary.so"...
        at java.lang.Runtime.loadLibrary0(Runtime.java:1077)
        at java.lang.Runtime.loadLibrary0(Runtime.java:998)
        at java.lang.System.loadLibrary(System.java:1661)
        at com.me.myapp.MainActivity.onCreate(MainActivity.kt:34)

I am at a loss and have been trying to figure out how to fix this for four days.

I have tried a few different approaches: 1. I tried compiling libusb for Android according to their instructions and manually moving the .so files to my libs folder in my project. I then tried to load both libraries with

System.loadLibrary("usb1.0")
System.loadLibrary("MyLibrary")

libusb1.0.so loads successfully, but again, the app crashes when libMyLibrary.so is loaded.

2. I attempted using a different Rust HID library, hidapi-rusb, which is a drop in replacement that builds off of rusb instead of libusb. This did not solve the problem either.

3. Finally, I went back to hidapi-rs and attempted to build it locally to use with my Rust library. This did not fix the issue either.

I also made sure to add the correct permissions to my AndroidManifest.xml file according to this by adding

<uses-feature android:name="android.hardware.usb.host" />
<uses-sdk android:minSdkVersion="12" />

I want to try to link a libusb library to my Rust library when I build, but I do not know how and there are not many resources to do so.

This app has been released successfully on MacOS using hidapi-rs, but Android does not want to cooperate. Any help is very much appreciated.

  • Maybe related to this https://stackoverflow.com/questions/54373254/load-dependent-so-from-other-shared-library-via-jni#58842312 – Markus Mar 29 '23 at 19:09
  • @Markus That sounds like the same problem, but I'm not sure how to accomplish that same thing with cargo. – HParish Mar 29 '23 at 22:56

0 Answers0