Add pre-built NDK library (.so file) to Android project - android

I inherited an Android project which includes a sub-project to build a JNI library from C source. I want to remove the C source and include pre-built library instead. Based on tips I found here and there, I copied the .so files from the .apk to paths like:
app/src/main/jniLibs/armeabi-v7a/libxcore.so
But, when I tried to build the app I get "cannot find symbol" errors for classes defined in the library. I'm guessing I need to add something to build.gradle that points to the .so files, but am very new to Java/Android/Gradle and can't figure out what. Tried a variety of things suggested here, with no luck. Thanks for any help.

The error had nothing to do with the location of the .so files, but with the fact that the library sub-project I deleted also contained some Java sources. Once I added back the original library project, and deleted only the C sources, everything worked fine.

Related

Add .so file to android solution

I don't know how to use .so files in my project. So I downloaded the ffmpeg.so file and now I need to add it to my solution so that I can convert video formats on my device but I do not know how. I tried finding tutorials on the internet on how to add this file but nothing seems to work for me.
Do I still need the Android NDK to compile my C code as I have the .so file.
Any good tutorials how I can add this library to my solution?
Put the file named libffmpeg.so into libs/armeabi subdirectory of your project. You don't need NDK if you don't have C/C++ sources of your own.
A mistake that happen sometimes is that the Android name expectations are not met. The name must start with lib and end with .so.

Exporting .jar to use in Unity with 3rd-party libs included: NoClassDefFoundError

This seems to be a common issue when exporting jars with Eclipse. Now, my context.
I'm attempting to write Java plugins to use in Unity applications. When I did a simple plugin with no external libraries (except the classes.jar so I can call a UnityPlayerActivity), it worked pretty well. "Now, let's use some utils libraries". So I wrote a plugin which uses Jackson libraries to parse and process JSON data, so I can serialize it to a Java object, or just pass RAW data, then build a Unity (C#) object.
I add the libraries directly from the file system to the 'libs' folder (copying the .jars, not linking them), add to build path, check every one in the Order and Export, clean and build, and export to JAR file (not runnable, but simple JAR file). Then, I add my new .jar to Unity Assets/Plugins/Android folder, then build my .apk. As soon as the game starts, I get the classic NoClassDefFoundError because of a constructer using a Jackson class. Checking the .jar contents, I find that Jackson libraries are, indeed, exported and within the 'libs' folder, but still it won't "find" it. The error will be thrown by the main thread, thus the application will crash.
I'm using ADT with API 17, so the libs folder must be named 'libs' (even Eclipse does create this folder when creating a new Android project), JRE 1.6 and Jackson libraries ver. 2.2.0. Already followed a lot of questions here, but none of their suggestions has worked for me.
Any pointers here? Has the Android API version anything to do here? Is there something I'm not doing?
Thanks in advance.
I'm quite new to Java, but as far as I can tell when you use an external JAR file it is dynamically linked. In other words, it stays independent - it is not merged into your library.
This was obvious in my case: The external library I used was Google Analytics. Its JAR file, libGoogleAnalyticsV2.jar, is 126 KB. After I added it to my libs directory and built my library I got an output JAR file of only 2 KB...
My solution was to copy the external library JAR file to Assets/Plugins/Android, together with the JAR file I created.

Debugging external libs in Android

I have several .jar files I want to reference into my Android project, and I have the sources for them. The point is that in order to build .jar's from sources, I have to use Maven - the build process is pretty complicated, and as I am new to both Android and Maven, I am trying to reuse the script for building these libraries that was written before me, and do not add the sources directly.
So my intentions were pretty simple:
Build the .class files via Maven script
Compile .jar from these .class files via jar tool in the command line
Reference these libraries from my android project.
But as soon as I copy .jar files to the libs folder in the android project - I cannot add sources on them - and I cannot debug those.
Looks like this issue has been several times on SO here or here. Still nothing works for me. .properties file doesn't seem to be recognized by Eclipse, when I go to Java Build Path - the path to the sources is marked as (None) and it is not modifiable, and if I reference the libraries like it was before ADT17 (adding a custom directory like lib with no s in the end, and referencing these jars as external libraries) - this fixes the debugging, but the compiler doesn't seem to include all the source code due to this
I've read that should be fixed in ADT20, but I am using ADT21 and still no luck. Any usable workaround of this?
Finally did it! That still looks like a bug in ADT. The only solution I found is to reference the libs as it was before ADT17 (put them into the separate lib folder without 's', add them to build path, and mark the checkboxes in the Configure Build Path -> Order and Export).
The thing I was missing is actually marking the checkboxes - for some reason when you change anything in that dialog, the checkboxes are becoming unmarked. I guess I just didn't notice that...

Building and including an external module in an Android app

I would like to include this plist parser module in my Android application, ideally without just copying the entire module source into my source tree (if that would even work).
I have successfully added the module as a project into Eclipse (3.7.0) and resolved errors by fixing the build path to include Android 2.1, which is what I am using. However, now I'm stuck. All of the information on using external libraries with your Android project I can find expect you to have a JAR of the library, but I only have this source code. I can run the plist parser module as an Android application, which appears to compile an .apk, but that doesn't actually do anything because it's not a standalone application. Any options to just build the module without running it are greyed out in the Eclipse interface.
How can I either build this module into a .jar for inclusion, or include it in some other way?
Edit: In order to clear the errors in the module after I added it to Eclipse, I followed the instructions in this answer.
You can either convert the whole thing to a library project or simply include the source code in your app's project. To create a library project, you can import the project from github, and after you get it compile, remove any activities, go to the project's Properties->Android and check the 'Is a library' check box. Then add it as a dependency to your own project.

Android NDK library: findLibrary returned null

I'm trying to build a medium-size project in Eclipse. It consists of main app project and two Android libraries. Everything worked fine until I added a very simple JNI library to main project.
When I run the app on device I see this exception in LogCat:
01-11 17:19:01.421: E/AndroidRuntime(26569):at
java.lang.Runtime.loadLibrary(Runtime.java:429)
Couldn't load xxx: findLibrary returned null
I googled around for similar problems and I believe that I did everything correctly:
The only .c file and Android.mk file are placed in the 'jni' folder
I build the library with ndk-build tool
The .so library is correctly placed in the 'libs\armeabi' folder
I see that the resulting apk file does indeed contain my native library
When I create a test project and link to the very same native code - it works fine. The problem persists in the main project. I already tried to create Eclipse project from scratch but it didn't help
I use Android SDK Tools v.16
Can anyone please help me with a suggestion?
Thanks!
Ok, after two days fighting the Android SDK I managed to solve my problem. Here's some explanation just in case it may help anybody else.
My previous project structure:
Android Lib1 project (with it's own jni code)
Android Lib2 project (depends on Lib1)
App1 project (depends on Lib2)
App2 project (with it's own jni code; depends on Lib2)
The original problem has been seen when I tried to load jni library in App2. I still don't understand what was preventing the runtime from finding the jni in the App2.
However the solution was to remove the Lib1 project from the workspace and add it as JAR file to Lib2 and both Apps. Now I'm finally able to use load the jni lib in the App2.

Categories

Resources