I created a static C++ library for testing. It only defines a class, MyLibrary, and the constructor MyLibrary::MyLibrary(). I built it in qtcreator, and got a libMyLibrary.a file, which is a prebuilt static library.
I would like to use this library in an Android project (using the NDK). In a working NDK test project, I therefore added a folder called inc at the same level as jni, in which I put libMyLibrary.a and its corresponding header mylibrary.h.
My Android.mk is as follows:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyLibrary
LOCAL_SRC_FILES := ../inc/libMyLibrary.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp
LOCAL_C_INCLUDES += inc
LOCAL_STATIC_LIBRARIES := MyLibrary
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
The ndk-build command compiles without any error. But my static library is apparently not linked (i.e. it does not appear in obj/local/armeabi/objs).
I have tried to include it in my mainActivity.cpp file, but even though the header (mylibrary.h) is found, the library is not and I cannot create an object as in:
MyLibrary test = MyLibrary();
What am I doing wrong? I have read tens of similar questions on StackOverflow, but I still don't get it.
try to use this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := helloJNI
LOCAL_SRC_FILES := mainActivity.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/inc/
LOCAL_LDLIBS := -llog -L$(LOCAL_PATH)/inc/ -lMyLibrary
include $(BUILD_SHARED_LIBRARY)
move libMyLibrary.a & mylibrary.h to jni/inc/libMyLibrary.a
Related
Eclipse NDK, NativeActivity project.
I ma trying to add a liquidfun(Box2D) library to my existing project. Unfortunately, no one in internet explain how to precisely do.
I'am stuck after building library using ndk (following this https://google.github.io/liquidfun/Building/html/md__building_android.html), and run sample project. I have totally no idea how to use it in my own project.
My Android.mk, i already using sfml.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sfml-example
PROJECT_FILES := $(wildcard $(LOCAL_PATH)/CPP/*.cpp)
PROJECT_FILES := $(PROJECT_FILES:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES := main.cpp
LOCAL_SRC_FILES += $(PROJECT_FILES)
FILE_LIST := $(wildcard $(LOCAL_PATH)*.cpp)
LOCAL_SHARED_LIBRARIES := sfml-system
LOCAL_SHARED_LIBRARIES += sfml-window
LOCAL_SHARED_LIBRARIES += sfml-graphics
LOCAL_SHARED_LIBRARIES += sfml-audio
LOCAL_SHARED_LIBRARIES += sfml-network
LOCAL_WHOLE_STATIC_LIBRARIES := sfml-main
include $(BUILD_SHARED_LIBRARY)
$(call import-module,sfml)
Thanks in advance.
I have some prebuild libraries in my own project, and i include them like:
include $(CLEAR_VARS)
LOCAL_MODULE := libcurl
LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libcurl.a
LOCAL_STATIC_LIBRARIES := libcares libssl
LOCAL_EXPORT_LDLIBS := -lz
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/curl/include
include $(PREBUILT_STATIC_LIBRARY)
this is for prebuilt static library (with .a extension). If you would like to include shared, just change it to BUILD_SHARED_LIBRARY.
This file to include is inside MyProject/jni/lib/{arch} folder (with other libraries) and header files of library are put inside MyProject/jni/curl/include (just like it is visible in LOCAL_EXPORT_C_INCLUDES variable)
the names you pass to LOCAL_STATIC_LIBRARIES must be same as one that are declared in LOCAL_MODULE of other libraries/modules.
also everything you will probably need you can find in NDK docs that are in NDK folder. For prebuilt libraries there is separate section.
I am trying to compile a ndk module for my android app with a Shared Library provided with these drivers:
http://www.extendo-printer.de/fileadmin/user_upload/extendo/Drivers/X-56-Hengstler-Extendo-LinuxDrivers-S684018-R2-V1_01-FINAL-2009JUL15.zip
I can compile fine with the defaults (using Hello JNI example). But when I add my code the my C file which references a .so library I need to work with I get compile errors stating it can not find the methods defined in the library. When I try to include it in Android.mk in LOCAL_SRC_FILES i get warnings when it builds (unsupported file extension) but it builds, but than my native method call fails (java.lang linkage error). Is there a proper way to include a .so and than reference it's functions in your native C code?
current Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := x56-jni
LOCAL_LDLIBS := libExoApi.so
LOCAL_SRC_FILES := x56-jni.c
include $(BUILD_SHARED_LIBRARY)
I tried with LOCAL_SHARED_LIBRARIES as well with same build error. I've also tried adding to LOCAL_SRC_FILES as stated above (LOCAL_SRC_FILES := x56-jni.c libExoApi.so) with a "," and as well as \ syntax.
Update
I've updated Android.mk to:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libExoApi
LOCAL_SRC_FILES := libExoApi.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := x56-jni
LOCAL_LDLIBS := libExoApi.so
LOCAL_SRC_FILES := x56-jni.c
LOCAL_SHARED_LIBRARIES := libExoApi
include $(BUILD_SHARED_LIBRARY)
This fails at:
[exec] Unable to recognise the format of the input file `/var/folders/_6/6jjydzjs457fnpsc9_5cgl980000gn/T//michael/usbserial-generated/libs/armeabi/libExoApi.so'
I want to build an shared library. To build it, I need to call another shared library. Here is what I did:
1.Create one Android project,named "BuildLib",and add a new folder "jni" under the project directory. Contents of jni folder:
jni-->Android.mk
-->Application.mk
-->add.cpp
-->add.h add.cpp just do two numbers addition:
add.h:
int add(int a,int b);
add.cpp:
#include "add.h"
int add(int a,int b){
return a+b;}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := add.cpp
LOCAL_MODULE := add
include $(BUILD_SHARED_LIBRARY)
After build the project,I got libadd.so under directory $(BUILDLIB)/libs/armeabi/.
Create another Android project, named "CallLib". Copy libadd.so and add.h to jni folder, create Android.mk, Application.mk, and call_add.cpp.
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
include $(PREBUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := call_add.cpp
LOCAL_MODULE := native
LOCAL_SHARED_LIBRARIES := add_prebuilt
include $(BUILD_SHARED_LIBRARY)
call_add.cpp:
#include "add.h"
int call_add(){return add(1,2);}
After all above, I build the CallLib project, but got the error:
undefined reference to 'add(int, int)';
I think the libadd.so can not be found, but I don't know how to modify. Does anyone know how I can fix this? Any help will be appreciated.
In your second Android.mk, try replacing the first module with:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
LOCAL_EXPORT_C_INCLUDES := add.h
include $(PREBUILD_SHARED_LIBRARY)
The LOCAL_EXPORT_C_INCLUDES flag should attach the header information to the add_prebuilt module, so it can be linked with your final library.
Just in case anyone needs it:
A bit hackish way to keep the linker happy:
LOCAL_LDLIBS := -llog
or
LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -lMyStuff
Less hackish:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := xyz
LOCAL_SRC_FILES += xyz/xyz.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY) # this builds libxyz.so
include $(CLEAR_VARS)
LOCAL_MODULE := abc
LOCAL_SHARED_LIBRARIES := xyz # <=== !!! this makes libabc.so dependent on libxyz.so
LOCAL_SRC_FILES := abc/abc.c
#LOCAL_LDLIBS := ...
include $(BUILD_SHARED_LIBRARY) # this builds libabc.so
I have created a .so file that exposes a native C call to Java via JNI. This works well and I can deploy the app onto my Android system if I just use system libraries in my C code. However, if I want to make calls to functions in other .so files, I cannot get my project to link correctly.
For example, say I have the "libotherso.so" file which contains APIs defined in C that I can call from the "MyJNILibrary.c" code I'm using to generate "libMyJNILibrary.so".
I tried to change my Android.mk file as follows:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyJNILibrary
LOCAL_SRC_FILES := MyJNILibrary.c
LOCAL_LDLIBS += -lotherso
include $(BUILD_SHARED_LIBRARY)
But when I call ndk-build on this, I get errors finding -lotherso. Where do I put the "libotherso.so" file so that ndk-build can pick it up?
Thanks for any help you can provide.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := otherso
LOCAL_SRC_FILES := ../lib/libotherso.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := MyJNILibrary
LOCAL_SRC_FILES := MyJNILibrary.c
LOCAL_SHARED_LIBRARIES := otherso
include $(BUILD_SHARED_LIBRARY)
Note that LOCAL_SRC_FILES is relative to your LOCAL_PATH.
Don't forget to load your dependency before your own JNI library:
static {
System.loadLibrary("otherso");
System.loadLibrary("MyJNILibrary");
}
I am trying to include static cpp library in android. This library is already compiled(on mac os) and i have its include files.
Here is my Android.mk file
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_MODULE:= utils
LOCAL_SRC_FILES:= libUtils.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/utils
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := sample
LOCAL_SRC_FILES := sample_cpp.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_STATIC_LIBRARIES := utils
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
and here is Application.mk file
APP_STL := stlport_static
APP_CPPFLAGS = -fexceptions
but whenever it try to compile it using NDK i get the error
(Path of file)/libUtils.a: file not recognized: File format not recognized
collect2: ld returned 1 exit status
From the comments and so on it sounds like you trying to use a non arm version of the library. You should build the library with the ndk. The documentation has even documentation on how to do that.
For example building sigc++ could be like (from a project of mine, where sigc++ resides in the sigc++ subdirectory)
# SIGC++ Library built as static library
LOCAL_MODULE := sigc
LOCAL_PATH = $(CURRENT_DIR)
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := sigc++/signal.cc sigc++/signal_base.cc sigc++/trackable.cc
LOCAL_SRC_FILES += sigc++/functors/slot_base.cc sigc++/adaptors/lambda/lambda.cc
LOCAL_SRC_FILES += sigc++/connection.cc sigc++/functors/slot.cc
LOCAL_C_INCLUDES := sigc++
include $(BUILD_STATIC_LIBRARY)
But you should really read how the compiling linking works. I am afraid building for android with ndk is more low level than using Xcode or Msvc.