Cannot find boost shared library files in Android application - android

For Intellij (and Android Studio) I built a JNI shared library that links to boost libraries that I'd link to include in my Android app. I called System.loadLibrary on my .so file but it fails to find boost libraries when I run it. I get the following error:
java.lang.UnsatisfiedLinkError: dlopen failed: library "libboost_unit_test_framework-clang-mt-x32-1_77.so.1.77.0" not found: needed by /data/app/~~28p8gv9ihFbZAejYd9c9yw==/sensoft.applications.dt1demo-0IEJ8o6cHOk0kputNbnbNQ==/base.apk!/lib/x86/libssi_utils_java.so in namespace classloader-namespace
Even though the boost .so files are there in the libs/x86 directory (I built them in x86 with the android toolchain to run on my emulator) but it did manage to find the .so file that I built in the same directory.
I placed my .so file and all the required boost .so files in the libs/x86 directory and included the following as a jni source in my build.gradle file:
sourceSets {
main.jniLibs.srcDirs = ['libs']
}
But my application can't seem to find the boost shared libraries specifically.

Android's install process will not extract libraries that don't have the suffix .so. You have to remove the version suffix of the library (which serves no purpose on Android anyway, because libraries are not all installed to a single common path).

Related

Include native .so library with specific version into Android .aar archive

It seems to me that gradle Android plugin does not include versioned .so libraries into .aar archive.
My case:
I need to load some library in Android application that requires another library: libz.so.1
I do have libz.so and libz.so.1 in my native libraries directory.
After running gradle there is only libz.so library in created .aar archive.
It results in java.lang.UnsatisfiedLinkError: dlopen failed: library "libz.so.1" not found exception when I try to run my application.
It looks like my otherlibrary tries to load only that specific version of libz.
readelf -d otherlibrary.so also tells me that it wants specific library:
0x00000001 (NEEDED) Shared library: [libz.so.1]
Both libraries that I want to load are third party libraries.
It looks like I need to find a way for one of the following:
pack libz.so.1 into .aar archive, so otherlibrary.so could easily link with it
load libz.so and somehow make otherlibrary.so to link with it
but I'm running out of ideas, how to do it.
Part of build.gradle that builds .aar archive:
sourceSets {
main {
jniLibs.srcDirs = ['libs']
}
}
libs directory contains both libz.so and libs.so.1 libraries. Only libz.so is assembled into .aar file.
This page: https://developer.android.com/studio/projects/android-library#aar-contents
suggests me that only /jni/abi_name/name.so pattern is supported.
Looking forward for some help.

aes_ctr_128_encrypt in Android using boringSSL

I am trying to implement aes_ctr128_encrypt in android using BoringSSL as defined by google.
I built the application with CMAKE and Gninja by following the link
https://boringssl.googlesource.com/boringssl/+/master/BUILDING.md
Now I have crypto and ssl folders in the build folder but there is no AES definition in any of the files as AES.h was in the include folder in root directory.
Anyhow, The binaries thus generated with CMAKE are also giving .a files and not .so files. How can I use those .a files in my project to access the aes_ctr_128_encrypt?

How do you link third party library in Android cmake external build system?

Android Studio 2.2 introduces cmake external build system. The problem is that documentation is really lacking and I do not know how should I link third party libraries? I've tried cmake directive target_link_libraries:
target_link_libraries(native-lib libs/libSomething.so)
And it "works" in that app compiles but then I get dlopen error at runtime because libSomething.so has not been packaged with application. The libs directory is under "app" if that changes anything and I've started with default JNI project generated by Android Studio 2.2...
[Update]
I've tried putting libSomething.so under app/src/main/jniLibs/armeabi-v7a but now the "main" (native-lib) library is not packaged.
[Update2]
I've added source set config that includes cmake output dir and this works but is ugly as hell and is not really a permanent solution...
sourceSet
{
main
{
jniLibs.srcDirs = [ "libs", ".externalNativeBuild/cmake/debug/obj"]
}
}
For now I ended up copying libSomething.so to cmake library output directory in a post build step. This works because it turns out that Android Studio copies into apk EVERYTHING that is in that directory.
Command in cmake is the following:
add_custom_command(TARGET native-lib POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
${CMAKE_CURRENT_SOURCE_DIR}/libs/${ANDROID_ABI}/libSomething.so
${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/libSomething.so
)
For now, you could also put your shared libs into directory, and configure jniLibs to point to that directory, that will pack it. One sample is here: https://github.com/googlesamples/android-ndk/tree/master/hello-libs, follow gperf see if that helps. This way app not depending on the generated build folders. When android studio added packing libs, the jniLibs workaround is not necessary anymore

android native lib not installed with APK

I've made an android library project that uses some native libraries.
I've made the jni wrapper and put the native libs (.so) in the libs// folders. The native libs are compiled using cmake, for armeabi, armeabi-v7a, x86 and mips.
I export this project to a jar and put this jar into a "normal" android project. I then export this project to an apk and can see that my libs are bundles into it.
However, when i install the apk, the libs corresponding to the device are not copied into /data/data/com.my.app/lib and obviously, running the app complains about not finding the libs (UnsatisfiedLinkError).
I've search through SO and everywhere i can but found no answer that solved my case.
i'm using Eclipse, btw
Thanks for your help
UPDATE
OK, i've read the doc in the ndk and seen the examples, and unfortunately, i can't see the solution.
The ndk build the c code into shared libs and places them into the appropriated location in the project. But it doesn't generate anything that says that the libs must be installed with the apk.
My goal is to provide an android library (so a jar), that can be included within an android application. I don't see the real difference between what i'm doing (compile the c libs using cmake and package the jni and java compiled classes into a jar) and what is done with android.mk
If you see what i'm missing, feel free to tell me (even if its obvious).
thanks
UPDATE
i've made a dirty hack: in the libs folder of my application, i've put the jar file containing my classes and my native libs and a copy of the .so files for each arch. Suprise, the libs are no installed in /data/data/com.me.myapp/lib
It seems to confirm that it's a packaging problem.
I export this project to a jar and put this jar into a "normal"
android project. I then export this project to an apk and can see that
my libs are bundles into it.
The issue is that the Android packaging system doesn't handle with binary assets in JARs. For your application project to find and include the generated .so files, you need it to reference the library project as an 'Android library project':
Did you call ndk-build command?
See description below for details.
http://developer.android.com/tools/sdk/ndk/index.html
You can build the shared libraries for the sample apps by going into /samples// then calling the ndk-build command. The generated shared libraries will be located under /samples//libs/armeabi/ for (ARMv5TE machine code) and/or /samples//libs/armeabi-v7a/ for (ARMv7 machine code).

Questions on how the build .c files

I have been trying to implement the API for the serial port found the the below web page. I am a beginner in all this and I am sure about what I am looking at:
http://code.google.com/p/android-serialport-api/source/browse/#svn%2Ftrunk%2Fandroid-serialport-api%2Fproject%2Fjni
Questions:
1) The .c files are built how? Do I need to download the NDK? I assume the .c file is run directly by the virtual machine, or what? Or is the executable for the .c the file in the libs directory? If so, how do I utilize the libserial_por.so file?
Thanks!
The .c files are built into a library by running ndk-build in the project directory. You need the NDK.
The .c files are not run directly by the virtual machine, but rather a library is created in the libs directory, which is then loaded along with the SerialPort class.
To use the library, just use the SerialPort class which already has bindings to the library.
C files will be compiled to an ARM binary library with the extension .so by the NDK. Take a look at the NDK Documentation, section "Getting Started with the NDK", to find out how to use it.
Basically, you place your .c files in the jni directory, change Android.mk to specify how to compile them, then run ndk-build to build the library. The resulting lib<name>.so will be placed in the lib directory. You then use your library in the Java project with System.loadLibrary('<name>').
This of course means the library must have a JNI interface for you to be able to use with the Java application, since Android doesn't support JNA yet.
I see though that the code you pointed out is an Android project. To run it, simply run ndk-build in the project directory to build the library, then run the project in an emulator.

Categories

Resources