Slava Kamzen
Slava Kamzen

Reputation: 527

Use library module with JNI in application module Android library module

I have a project with 3 modules - a library and 2 apps that using this library. Now I want to add native-lib (one c file) into the library module, and use it in one of the library classes. I'm new to NDK so I followed the Google guide and created new project with NDK support and it works fine. Then I copy the cpp directory, CMakeList, and kept the correct structure of the project. Now, what happening is that the project compiles successfully, but crashes when I trying to call the native func from java class. Exception: Method threw 'java.lang.UnsatisfiedLinkError' exception.

Upvotes: 4

Views: 953

Answers (1)

SaPropper
SaPropper

Reputation: 523

I had this exception with a subsequent ClassNotFoundException when I wrote a Java library that referenced other native libraries. I didn't use NDK like you did, but maybe these steps (that solved it for me) can provide you some new ideas:

  1. I imported the native library (for me it was OpenCV for Java) in AndroidStudio as a library.
  2. I put the native source files in the jniLibs folder of the (imported) library.
  3. Then I exported the library as AAR file. (Make Module -> take out the AAR from /build/outputs/aar/)
  4. I used the AAR file in my own library. That way the classes of the referenced native library could be used in my own exported library.

Upvotes: 2

Related Questions