ACBM
ACBM

Reputation: 667

Prebuilt C library in Android Studio

Some months ago I developed an Android Library which uses some native code in C. I developed such library in Eclipse, since Android Studio doesn't support Android NDK.

To distribute the library I compile the project as .jar (excluding the AndroidManifest.xml). If someone wants to use the library in Eclipse all it needs to do is add the .jar in the folder /libs as well as the pre-built C code (.so) for all the architectures. This works and the client project ends up with this files:

MyProject
---| libs/
-------| Library.jar
-------| armeabi/
-------| x86/
-------| ...

Now I want to be able to use my library in a similar way in an Android Studio project.

What I have tried:

Add Library.jar in the folder /libs of the app module. Then, add in the build.gradle file (in dependencies ) compile files('libs/Library.jar'). Finally, add in jniLibs the C code (.so) compiled for all the architectures. Meaning, I have this files:

MyProject
---| app
-------| libs/
-----------| Library.jar
-------| jniLibs/
-----------| armeabi/
-----------| x86/
-----------| ...

However, I always endup with this exception:

java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.app-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]] couldn't find "lib.so"

I have looked up this exception and it's generated because the system can't find the compiled C library. However, I don't know where can I put my pre-built C library in order to be found by the Library.jar.

Thank you for your answers.

Upvotes: 1

Views: 2208

Answers (2)

acidflow
acidflow

Reputation: 61

You could also define the jniLibs location in your app build.gradle sourceSets { main { jniLibs.srcDirs = [ 'your/path/to/jniLibs')] } }

Upvotes: 1

FD_
FD_

Reputation: 12919

It seems you have to put the jniLibs folder into the src folder, under your main module. This way, Android Studio should be able to pick the .so Files.

The structure should look like this:

MyProject
---| libs/
-----| Library.jar
---| src/
-----| main/
-------| jniLibs/
---------| armeabi/
---------| x86/
---------| ...

Source: https://stackoverflow.com/a/29508776/1691231

Upvotes: 1

Related Questions