I'm using the android ADT bundle for Linux with the CDT 8.0.2.+NDK support.
I need to add an external library. I checked on the Eclipse forum for example here:
linking-external-lib
The problem is that it looks like the Android CDT does not have the same settings. Neither of the three solutions can be applied.
Did anyone find the way to add an external .so library in eclipse with CDT?
Before I added the Native spport I used to have the .so file in the libs folder. Now the libs folder is cleared everytime I build the project. So I need to put it somewhere else.
I managed to do it by adding the following lines to the Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE:=libname
LOCAL_SRC_FILES:= path-to-lib/libname.so
include $(PREBUILT_SHARED_LIBRARY)
Related
im stuck with this problem and dont know what im doing wrong. Please help me out.
Im working on an android project where I have to use 2 native library(.so) file. These 2 files are not generated by me, i just have to use them in my project.
I have placed these .so files under libs->armeabi folder. In the code i have used System.loadLibrary("name") inside a static block to load these library files. After doing this, i clean the project and run the application. I get the following error
java.lang.UnsatisfiedLinkError:
What am i missing. I have unchecked the first two options in build settings as well.
Note: I am not developing the native library. I am just using it. It was being used in a previous version, so I am pretty sure there is nothing wrong in the way the .so files have been generated.
PS: My ultimate requirement is like this,
My android application will have a jar which I am developing. The methods in the jar will make use of these native functions. The main application will not directly access the native functions. Should i place the .so files in the jar or should it be in the main application. How should i package the jar and .so files.
Please help me out, this is my first time using native functions and im totally lost. Links to any materials would be great.
Set PATH to your NDK
Eclipse -> Window -> Preferences -> Android -> NDK -> set path to the NDK
Right click on an Android project and select Android Tools -> Add native support.
Make changes to your Android.mk file to include this .so file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := <module_name>
LOCAL_SRC_FILES := <.so file name>
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../jni/include
include $(PREBUILT_SHARED_LIBRARY)
Clean project and build again
I have an Android Eclipse library project "Lib" that contains both Java and C code built with the NDK. I have another project "App", which is dependant on Lib. App also contains both Java and C. My goal is to have multiple Eclipse App-like projects which each use the Lib project.
A Java routine in App calls a Java routine in Lib which calls C code in Lib. This compiles, links, and runs perfectly on the device.
Now I want to add a call from the C code in App to the C code in Lib. My First problem is header files. I need to include a header file, jni/lib-jni,h, from the Lib project:
#include "lib-jni.h"
in a .c file in App. For this #include to work I believe I need to add Lib's jni/ directory to LOCAL_C_INCLUDES in App's jni/Android.mk. I do not want to hard-code this path, I would like to get it from Eclipse if possible. After all, I've told Eclipse that the App project depends on the Lib project and Eclipse knows how to connect Java calls between the two.
I temporarily kludged around the first problem by copying lib-jni.h from Lib's jni/ directory to App's jni/ directory. Now App's C code compiles, but it won't link; it gets an undefined. In App's Android.mk I need to tell the C linker to link against libLib.so. I tried putting -lLib on LOCAL_LDLIBS, but that didn't work. libLib.so is in the Lib project in Lib/libs/armeabi/libLib.so. Eclipse knows to incorporate this .so into the .apk file it builds for App.
Is there a way to cleanly solve these two problems?
Add Lib's jni/ directory to App's LOCAL_C_INCLUDES
Add a reference to libLIb.so to App's LOCAL_LDLIBS
I phrased these problems in terms of possible solutions. I'm open to any solution. For example, if using LOCAL_LDLIBS the wrong way to go, what is the right way?
Note: Because App and Lib are two separate Eclipse projects built at separate times I am pretty sure I can not use LOCAL_EXPORT_C_INCLUDES in Lib's Android.mk to make Lib's jni/ directory available to App's Anroid.mk - I tried it didn't work. But I'm prepared to be educated.
I always set the LOCAL_C_INCLUDES to the original lib, so I think that is not that bad idea. An other solution is to include the external library as a static library in your libs folder, but I'm not sure if it will work.
I'm making a Unity3d plugin for this c++ library for Android. Ive gotten it to work in windows by opening the visual studio project the library makers provided, adding a new .cpp file to it with my API code and building to a dll.
I'm wondering now how I can add this new .cpp file to the library and build it for Android.
So far I have installed the Android sdk and ndk on a machine with ubuntu and successfully built the original library using ndk-build (as per these instructions)
I'm guessing it is not as simple as copying my .cpp file into the folder and building as there are AndroidManifest.xml files and so on.
The AndroidManifest.xml file is not pertinent for an NDK build.
You will need to find the appropriate Android.mk file. You may need to add the file name into the LOCAL_SRC_FILES in that make file, although they may have it set up to compile all the .cpp files in the directory or something more intelligent along those lines. It is, after all, a regular make file.
See here for info on the Android.mk file specifics.
Edit:
I suppose I should add that it would be the LOCAL_SRC_FILES preceding the relevant include $(BUILD_SHARED_LIBRARY) for the library you intend to build. Each make file may contain an arbitrary number of targets, although in the simplest case it's usually one make file to build a library.
I have an android test project that includes JNI code from elsewhere in my repository. For instance, my Android.mk file resembles the following:
LOCAL_PATH := $(call my-dir)
include $(LOCAL_PATH)/../../../Android.mk
I have opened my project in eclipse, enabled native support, and switched to the Android Native perspective. The project builds and runs correctly.
My problem is that my native c++ code is not present in the eclipse workspace. I can't see it under the jni folder or under any of the project's sub folders. My question is, how do I include the native code in my workspace so that I can edit it, and set breakpoints (via Sequoyah)?
You can add a linked folder to your eclipse project, see http://www.russellbeattie.com/blog/1002305
I have used ndk successfully to build & use a .so file in one project. I need to use this library in another project. I would rather not copy the source there, but just use the library.
Trying to copy & paste the whole libs/armeabi/libcommon.so to the project root does not work, I think because libs/armeabi is an android generated path.
So what would be the best way to do it?
I am using Eclipse-Galileo & ndk5.
There is a much simpler way to do all of this.
Let's say that your prebuilt library is called "libprebuilt.so"
In the project folder of the new project you want to only include the prebuilt library, do something like:
mkdir -p jni/libprebuilt
cp libprebuilt.so jni/libprebuilt
Then, just create a jni/libprebuilt/Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libprebuilt
LOCAL_SRC_FILES := libprebuilt.so
include $(PREBUILT_SHARED_LIBRARY)
Then when you do ndk-build, it will copy this library in to libs/armeabi/ ... that's all!
I figured out a way to do this, so posting it here in case someone else runs into it.
The code to be shared (including the Java JNI wrapper, native code, .so library), should be in a separate project. Convert this to a Library project by right-click on project name --> properties --> Android properties --> check mark "Is Library". This project cannot be executed now but can be referenced by other projects.
In the project which will use the shared object, add a reference to the Libarray project by right-click on project name --> properties --> Android properties --> Library/"Add". This should show up the Library project created in the previous step. Select it.
Now the .so can be referred to easily between different projects.
There is a package name inside all JNI (Java Native Interface) functions names (like JNIEXPORT void JNICALL Java_com_myapp_myclass_funct). So i guess you should rename these funkcions in library a recompile it. Or maybe use it as external package in your app.