Add shared lib.so to android project - android

I have an Android project using some native functions. Native functions are using some functions from external shared library "external_lib.so"
I've added to Android.mk: "-lexternal_lib -L/path/to/external/lib" line and myjni.so have been linked successfully. But during runtime myjni.so loading error occurred. The size of myjni.so reflects that external_lib.so is not included. How to fix the problem?
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := myjni
LOCAL_SRC_FILES := myjni.c
LOCAL_LDLIBS := -ldl -llog -lz -lexternal_lib -L/path/to/external/lib
include $(BUILD_SHARED_LIBRARY)

is the .so a third party prebuilt shared library?
if so, in your Android.mk, you need to have:
...
LOCAL_SHARED_LIBRARY := curl
...
LOCAL_LDLIBS += -L$(ANDROID_LIBCURL_LIB_PATH) -lcurl
LOCAL_SHARED_LIBRARIES := curl
include $(BUILD_SHARED_LIBRARY)

Related

Adding library to NDK project - eclipse

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.

Non-system libraries in linker flags: -lopencv_java3

In the android.mk file I want to include the OpenCV3 library. so the file looks like this:
android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_EXPORT_C_INCLUDES := D:\path to\OpenCV-android-sdk\sdk\native\jni\include
include D:\path to\OpenCV-android-sdk\sdk\native\jni\OpenCV.mk
LOCAL_MODULE := MyLib
LOCAL_SRC_FILES := file1.cpp
LOCAL_SRC_FILES += file2.cpp
OPENGLES_LIB := -lGLESv1_CM
OPENGLES_DEF := -DUSE_OPENGL_ES_1_1
LOCAL_LDLIBS += -lGLESv1_CM -ldl -llog
include $(BUILD_SHARED_LIBRARY)
But when I build the project, I got this warning message:
Android NDK: WARNING:jni/Android.mk:MyLib: non-system libraries in linker flags: -lopencv_java3
Android NDK: This is likely to result in incorrect builds. Try using LOCAL_STATIC_LIBRARIES
Android NDK: or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the
Android NDK: current module
How can I solve this issue ?
If you only use OpenCV from your C++ code, you don't need this libopencv_java.so. To indicate this, add
OPENCV_LIB_TYPE=STATIC
Before including OpenCV.mk.
Some explanations can be found at answers.opencv.org.
Try using LOCAL_STATIC_LIBRARIES or LOCAL_SHARED_LIBRARIES instead to list the library dependencies of the current module
Depending on which version you have (static/dylibs), change your LOCAL_LDLIBS to either LOCAL_STATIC_LIBRARIES or LOCAL_SHARED_LIBRARIES
Android.mk is a good reference to variables in the Android makefile

Using JsonCpp on X-Cross platform library

I'm making a library in C++ with OpenCV and JsonCpp towards building a library for Android and iOS.
On testing my library for Android, I'm making the JNI files but when I try to load the library I'm getting
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol
"_ZN4Json6WriterD2Ev" referenced by "libXYZ.so"...
and that's because I think I'm not building my Json library very well.
The library that I use is this one: https://github.com/open-source-parsers/jsoncpp
My Android.mk is:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=off
OPENCV_INSTALL_MODULES:=on
include $(LOCAL_PATH)/jsoncpp/Android.mk
include /Users/localmac/Desktop/AndroidDevelopment/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk
OPENCV_LIB_TYPE:=SHARED
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_C_INCLUDES += /Users/localmac/mylibrary/OpenCVtry/
LOCAL_C_INCLUDES += /Users/localmac/Desktop/RD/OpenCVtry/Libraries/jsoncpp-master/include
LOCAL_ALLOW_UNDEFINED_SYMBOLS := true
LOCAL_MODULE := libXYZ
LOCAL_SRC_FILES := androidClass.cpp main.cpp utils.cpp
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
I have no idea of how to do this.
Thank you in advance.
EDIT it's not the NDK Compiling's fault.
Even if I compile the JsonCpp, I get the
java.lang.UnsatisfiedLinkError: dlopen failed: cannot locate symbol "_ZN4Json6WriterD2Ev" referenced by "libXYZ.so"...
EDIT My jsoncpp/Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cpp LOCAL_MODULE := libJsoncpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsoncpp/include
LOCAL_SRC_FILES := src/lib_json/json_reader.cpp \
src/lib_json/json_value.cpp \
src/lib_json/json_writer.cpp
include $(BUILD_SHARED_LIBRARY)
You're not linking against Jsoncpp in your makefile. You should add the following line:
LOCAL_SHARED_LIBRARIES := libJsoncpp
before the last include $(BUILD_SHARED_LIBRARY).
You must specify module names for this variable (and its sister LOCAL_STATIC_LIBRARIES), that is, what you specified for the LOCAL_MODULE variable.
Also, that spares you from specifiying the includes in the LOCAL_C_INCLUDE variable (as the makefile will include them directly when specifying the library in the variable I mentioned at the top of my post).
EDIT: For the sake of completeness, I'll add that you can specify multiple libraries like that:
LOCAL_SHARED_LIBRARIES = libJsoncpp \
libOpenCV \
...
and the same goes for LOCAL_STATIC_LIBRARIES.

ADT tool chain not producing output for static libraries

Trying to build a static NDK library using Android's ADT Eclipse tool chain. However, whenever I build with BUILD_STATIC_LIBRARY, no output is produced: I get the message
make: Nothing to be done for `all'."
Any recommendations?
LOCAL_PATH := $(call my-dir)
STL_PATH := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include"
PLATFORM_INCLUDE := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include"
APP_STL := gnustl_static
include $(CLEAR_VARS)
LOCAL_MODULE := libCore
LOCAL_CPPFLAGS += -std=c++11 -fexceptions -D_OS_ANDROID
LOCAL_LDLIBS := -lGLESv2 -lEGL -lstdc++
LOCAL_C_INCLUDES += $(LOCAL_PATH)/Headers
...
LOCAL_SRC_FILES += Source/Engine/Game.cpp
...
include $(BUILD_STATIC_LIBRARY)
Here is the content of Android.mk file of two-libs sample project from Android NDK.
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-first
LOCAL_SRC_FILES := first.c
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_STATIC_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)
You may try building the static library as part of another shared library as shown in the example.
I just did a ndk-build on the two-libs sample project and i could see the .a file along with .so in obj\local\armeabi directory.
Edit:
By default, ndk-build will only build shared libraries and executables, and the modules they depend on. To force a build specify libCore in APP_MODULES as follows.
APP_MODULES := libCore
or in command line as
ndk-build APP_MODULES=libCore

Unsatisfied Link Error - Could not find Library

I'm attempting to load the libjnigraphics.so prebuilt library to my project, however when I deploy the application, I am receiving this error:
06-17 22:35:28.741: INFO/dalvikvm(298): Unable to dlopen(/data/data/com.foo/lib/libndkfoo.so): Cannot load library: link_image[1721]: 29 could not load needed library 'libjnigraphics.so' for 'libndkfoo.so' (load_library[1051]: Library 'libjnigraphics.so' not found)
It compiles perfectly, fine.
Here's my Android.mk as well:
LOCAL_PATH := $(call my-dir)
# Add prebuilt libjnigraphics
include $(CLEAR_VARS)
LOCAL_MODULE := libjnigraphics
LOCAL_SRC_FILES := libjnigraphics.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c
LOCAL_SHARED_LIBRARY := libjnigraphics
#LOCAL_LDLIBS += -libjnigraphics
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)
Is there a special place I need to put the libjnigraphics.so? Right now I have it in the jni folder, however I've tried it in multiple other places and it still fails. Any help?
The device has to be running Foyo (Android 2.2, target platform android-8) to be able to use libjnigraphics.so. It doesn't matter what is in your SDK or project directories, this library is part of the device's system libraries.
You can compile your code using the NDK with android-8, but then it will give link-time errors when you side-load the apk and your device happens to be running 2.1 or earlier.
There's this SO question with a workaround, but it shouldn't be relied upon and will crash on some devices. Here's a post on the android-ndk group explaining the issues.
just try these changes:
LOCAL_PATH := $(call my-dir)
# Add prebuilt libjnigraphics
include $(CLEAR_VARS)
LOCAL_MODULE := graphics-prebuilt
LOCAL_SRC_FILES := libjnigraphics.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
# Here we give our module name and source file(s)
LOCAL_MODULE := ndkfoo
LOCAL_SRC_FILES := ndkfoo.c
LOCAL_SHARED_LIBRARY := graphics-prebuilt
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)

Categories

Resources