Android Library unsatisfied link error with jar file - android

I have one project that includes a sample app, and a module that we'll call the sdk that I created that I will eventually package up as an .aar file when all is complete. The sample app tells the sdk to run a service. This service requires the use of external libraries that have been packaged as a .jar file that includes .so files. The issue is that every time I try to load the libraries, I get an unsatisfied link error saying that the .so file does not exist and therefore cannot open the library. I'm not quite sure if I'm placing everything in the right place as the error says. If I am missing a step, I was hoping someone could help point me towards the right direction. Below I'll show the file structure of my project as well as what I'm doing to try to open the library.
File Structure
`- MY PROJECT
- My Sample App (com.mine.mysampleapp)
- build
- src...etc
- SDK (separate module) (com.mine.mysamplesdk)
- build
- libs
- my-lib.jar
- assets
- arm64-v8a
- my-lib.so
- armeabi-v7a
- my-lib.so
Library Loading Code
System.load(context.getFilesDir() + "/my-lib.so");
What I'm not understanding is that the .so file lives inside the sdk but the error message:
dlopen failed: library "/data/user/0/com.mine.mysampleapp/files/my-lib.so" not found
shows that it's looking for the file in my sample application.

If you're trying to package as an AAR, those have built in support for JNI libraries: https://www.codepool.biz/build-so-aar-android-studio.html
Rather than putting the libraries in the assets directory and trying to assemble the System.load path yourself, put them in that directory and then you can just System.loadLibrary("my-lib") (note that System.loadLibrary won't actually let you open a library that isn't prefixed with "lib", so you need to rename your library for this to work).

Related

"Native method not found" in android studio

i have looked through all the other posts here about this subject but none of them helped me.
i am using a third party SDK (oovoo) and it contains a .jar file and .so file (armeabi-v7a)
as stated in allot of places, i put the .jar file in the libs folder and the armeabi-v7a folder (which contains the .so file) in a folder named jniLibs inside src/main
after building, when i unzip the .apk i see a folder named lib and inside it a folder named armeabi-v7a which contains the .so file
yet i still get this exception:
java.lang.UnsatisfiedLinkError: Native method not found: com.oovoo.core.ClientCore.VideoChannelPtr.nCreate:(Ljava/lang/String;)J
why cant it reach the implementation?
if it is in the lib folder in
the .apk does it mean it will for it at runtime?
thanks allot!
android studio version 0.8.6
gradle version 1.12
oovoo version 1.2.4
To answer your questions specifically :
It can't reach the implementation because at runtime, when the java environment tries to load a native method named java_com_oovoo_core_ClientCore_VideoChannelPtr_nCreate with the prototype int (string) it doesn't find such a method loaded. This can be caused by the following :
the .so library isn't loaded by the time you called that method , hence java doesn't know about it (so it's your mistake, you called the functions in wrong order)
the .so library doesn't export such a function, which means you have no chance of fixing this
the .so library loaded at runtime by the phone requires a different architecture besides armv7s. Be sure your device is armv7s.
the folder in which the Android system is looking for native libs is different than you expect. I know this was an old issue, so create in your apk, in your lib folder both "armeabi-v7a" and "armeabi" , and copy that .so file in both
Regarding your 2nd question, yes, if you have the lib inside the apk you will have it at runtime.

Import JAR that uses .so files into a new android project

I'm trying to build an app using a library packed in a JAR file.
When I call one of the functions I get this error:
Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing thelibrary.class
I've check the source code of the library and I think it crushes when loading some .so files ( System.loadLibrary("usb-1.0");
those files do exists in the JAR project under libs/armeabi (libusb-1.0.so)
I guess the .so files are not packed with the JAR, so how do I get them to be found in my project ?
(tried to copy them to libs/armeabi but the same exception is thrown....)
I do not get any link / build errors....
Thanks.
An ordinary JAR file will not work for distributing something that is dependent upon an Android NDK-compiled library. However, an Android library project can also distribute an NDK-compiled library.
but the real problem is that my app is going to be a library as well, and I don't know if I can make it a library while it is using other libraries
Assuming that "make it a library" == "make it an Android library project", this is fine, so long as projects attempting to use your library project also have access to the other library project.

I got the error on sample project "Hello Word" on android - cocos2d

I have eclipse 3.7, cocos2d-1.0.1-x-0.9.2, android-ndk-r7.
When I have create sample project but, it will get error "FORCE CLOSE"...
So is there any other thing i want to include on the project ?
Your main application has a load static library method that is pointing to a nonexistent library, which means it has not found it. Recheck your makefiles for the correct module name. Also make sure they have compiled correctly with NDK beforehand and the .a and .so files are in the lib folder.

android intellij add directory with class files

I am trying to include a library into my android project using IntelliJ IDEA v10.0.3. The .jar library is, however, corrupt, so I want to include the bin directory into the project.
What I am doing: I go to File - Project Structure - Modules - Dependencies - Add - Library - New Library. In the dialog that appears, I give the library a name, then click Attach Classes..., then choose the bin directory. I then click ok.
The library appears to be working fine. I get code completion and use the classes as desired. However, when I run the project, I get the following error:
Error! C:[Path-to-library-project]\bin is a directory. Directory libraries are not supported
What can I do to fix this problem?
Make a valid jar from the required classes and use standard jar library. It's Android specific limitation.

Creating a product SDK: How do I add a native lib (.so) and a jar with the SDK I am creating?

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)

Categories

Resources