I trying to build android application with some precompiled native libraries:
liba.so and libb.so.1.2.3
Libraries are placed into jniLibs subdirectory.
After building APK file, only liba.so included into it, but not libb.so.1.2.3.
Result is predictable. Application crashes at start.
What to do with build scripts to include all files from jniLibs into APK?
Due to the native library regex ^.+\\.so$ being used in the Android Gradle plugin, it is impossible to include anything other than .so files using the jniLibs folder. And even if you were to somehow add the library to the APK, the dynamic loader on Android is very limited and will most likely not be able to load them.
Your best bet is to remove the version altogether by renaming the library and changing its internal soname before linking against it.
Unfortunately I don't develop for Android anymore, so I can't test this, but I know Gradle and this might work for you. Looking at the Android DSL docs, you might be able to change the filtering on the jniLibs folder:
android {
sourceSets {
main {
jniLibs.filter.include("**/*")
}
}
}
Let me know if this works!
Just add the jniLibs folder in app/src/main and it will include the .so file in the apk.
/app/src/main/armeabi/*.so files
Related
I'm new to android studio and am working on an existing project that is using OpenCV. I need to implement CMU's Pocketsphinx for Android, and in following the installation steps they give I have created a jniLibs folder in my /src directory of my project, and placed the appropriate files for pocketsphinx in there.
This is where the problem occurs- when I do this, I suddenly get the following error message:
java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.robodoot.dr.facetracktest-2/base.apk"],nativeLibraryDirectories=[/data/app/com.robodoot.dr.facetracktest-2/lib/arm, /data/app/com.robodoot.dr.facetracktest-2/base.apk!/lib/armeabi-v7a, /vendor/lib, /system/lib]]] couldn't find "libjniopencv_core.so"
I do NOT get this error if I don't have the jniLibs folder in the /src directory, though then of course I get an error from pocketSphinx saying that it can't find it's required .so files.
What's more, after looking through OpenCV's SDK, I noticed there is no such file "libjniopencv_core.so". It doesn't exist anywhere, there are other .so files but none of which with that name. Even if I install all of the armeabi and what not directories from openCV to my new jniLibs folder, I still get this error message.
How can I tell android studio to only load the pocketsphinx jniLibs files, and disregard searching that directory for openCV .so files?
you can do this by adding following code in gradle
android {
...
defaultConfig {
...
ndk {
abiFilters "armeabi-v7a", "x86","armeabi"
}
packagingOptions {
exclude "lib/arm64-v8a/mysofile.so"
}
}
}
replace arm64-v8a/mysofile.so with whatever you want to exclude.
How can I tell android studio to only load the pocketsphinx jniLibs files, and disregard searching that directory for openCV .so files?
You should not disregard an important file. libjniopencv_core.so is required by opencv.
What's more, after looking through OpenCV's SDK, I noticed there is no such file "libjniopencv_core.so".
It must be inside opencv-android-arm.jar together with other important so files.
It's not clear why file conflicts, it should be some issue with gradle. You need to check internals of APK what so files are actually packed inside. Most likely you try to pack different architectures like armeabi and armeabi-v7. You might need to pack .so files from the same architecture.
i want to add fresco to my project so according to official site i put
compile 'com.facebook.fresco:fresco:0.9.0+'
into my gradle file. but after running the apk it fails on UnsatisfiedLinkError. i looked into my .apk and there is no /lib folder that should contain its .so files. i found out on the internet that gradle doesn't support .so files but i have seen projects that got it actually working, so there must be a way.
thanks in advance
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).
I have built a dynamic library in android using android build system. This library provides jni interface for functions inside it. Now I want to include this library in an application (.apk). I am using eclipse for application development. Now, how can I use the prebuild dynamice library (.so) in my application ? I tried putting it in a lib folder in my application but it is not working.
Any pointers are appreciated.
I am not using ndk to build my .so.
Since you write 'so' I think you're using NDK. If you're using NDK I don't know the answer.
If you're using the "Java" SDK, then in your library project go to Properties -> Android, and Check "Is Library". In your "apk" project, go to Properties -> Android -> Add . And your Library project should be available.
Also, any Library added in the "Java Build Path" Menu (again, in project properties) should be available in the APK in the end.
I know it's slightly old, but have you checked in the built APK to see if your .so library is there? Should be in the libs/armeabi folder.
Also, your .so file should be in lib/armeabi folder in your eclipse solution. I'm guessing the armeabi bit depends on which processor your .so file is build for.
Also, I know that if your library isn't called lib[name].so, it won't get copied when the apk is installed on the device. So:
libfoo.so copies
foo.so doesn't copy
foo.so doesn't copy
Also, you can use DDMS (its a view in eclipse) and it's file explorer to see if it's been copied to your device. It should be under data/data/[packagename]/lib.
Hope this helps a bit!
Andy.
I hit this same problem while building Qiqqa for Android. Under your eclipse android project, make sure you have a libs directory (not that it is plural libS not singular lib). Inside that create the armeabi/etc subdirs with their respectibe .so files.
Then when you build, eclipse will automatically pick up this libs directory and create the corresponding lib in your apk. System.loadLibrary("XXX") will then resolve to libXXX.so on your correct architecture...
Cheers,
Jimme
I am creating a widget that we will provide to developer end users and it consists of a .jar and a native library (.so) built using the NDK. The JAR has a JNI interface to the dynamic library.
It's very clear on how to include an external .jar in a project but not how to include a dependent dynamic library.
How do I package up and build the .jar and .so? What are the best practices here?
I can create the JAR file using the JDK's jar command. Do I need to run dx.bat on the jar to convert to Dalvik bytecode?
I need to create a sample project showing the widget in action. How do I include this .jar and .so in a sample project that demonstrates how to use the widget?
I spent some time on this, and i just can't understand why isn't this written on wikitude documentation.... anyway follow this changes!
go to windows/preferences/android/build
uncheck the first and the second option
extract files from wikitudesdk.jar with winrar as if it is an archive, search libarchitect.so and copy it in /libs/libs/armeabi/
add wikitudesdk.jar to your build path
You should use the standard build tools included with the SDK for this. If you include the .jar files you need in the /lib directory of your project, the ant build process will convert the included class files to Dalvik bytecode format and include them in your classes.dex file for the app. Add a build.properties file to your project root as well, with one line:
external.libs.dir=lib
Depending on the version of your SDK and configuration of it, you may need to place the jar in libs rather than lib.
As for the .so, I presume that it's properly compiled using the Android NDK, or using a build script that uses the proper compiler and compiler flags that are required to successfully link the shared object on Android. If this is the case, you can include the .so file in libs/armeabi and they will be added in the jar as well. Furthermore, the dynamic library loader will know to look in this location in the .jar when you actually try to load the library from Java code.
Using ADT 12, I accomplished this by doing the following:
1) Export JAR from your library with the SO file using Eclipse. Make sure you exclude AndroidManifest.xml. This will include source code and other data, so if you are distributing, you'll want to strip these unnecessary bits out using any ZIP utility.
2) Create a directory in your App's source tree (I use "lib" directory) and copy your library JAR to it.
3) Right-click project in Eclipse and select "Configure Build Path". Add JAR and point it to JAR inside your App source tree.
4) In your Eclipse preferences, select Android/Build section and uncheck "Automatically refresh Resources and Assets folder on build". You will notice an option below that says "Force error when external jars contain native libraries." There is an ADT bug, which will supposedly be fixed in R17, which uses the wrong setting. Once it is fixed, you will use the "Force error" one (make sure it unchecked).
5) Once you build your app APK, you will have a libs/armeabi/libXXXX.so file. This will not be automatically unpacked on install. You will need to write code to extract it from your APK file into your data directory, then use System.load on the .so file in your data directory.
I have bidirectional JNI calls going from the dynamically loaded file, and even use dlopen() on it to do my custom plugin registration stuff.
Add the below lines to android.mk.
include $(BUILD_PACKAGE)
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := alias:libs/your.jar
include $(BUILD_MULTI_PREBUILT)