How to include many shared libraries .so to build android apk - android

I want to use tow extern library to build android application of tracking object with surf algorithm so I used this tutorial Using OpenCV Nonfree Module (SIFT, SURF) in Android NDK Projects
the first library is libobjtrack_opencv_jni.so (using for tracking) an the 2 others are libopencv_java.so and libobjtrack_opencv_jni.so
I finished To Get this result:
Android.mk:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sift_prebuilt
LOCAL_SRC_FILES := libnonfree.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_java_prebuilt
LOCAL_SRC_FILES := libopencv_java.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := objtrack_opencv_jni
LOCAL_SRC_FILES := libobjtrack_opencv_jni.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE := STATIC
OPENCV_INSTALL_MODULES:=on
OPENCV_CAMERA_MODULES:=off
include C:/OpenCV-2.4.5-android-sdk/sdk/native/jni/OpenCV.mk
LOCAL_C_INCLUDES:= C:/OpenCV-2.4.5-android-sdk/sdk/native/jni/include
LOCAL_MODULE := test_sift12
LOCAL_CFLAGS := -Werror -O3 -ffast-math
LOCAL_LDLIBS += -llog -ldl
LOCAL_SHARED_LIBRARIES := sift_prebuilt opencv_java_prebuilt objtrack_opencv_jni
LOCAL_SRC_FILES := test_sift.cpp
include $(BUILD_SHARED_LIBRARY)
build is succeeded and i got test_sift12.so but is not work when i run the .apk .. I think that the problem is with the use of PREBUILT_SHARED_LIBRARY in android.mk file .
i need your help

load library, where you want to use that libraries in the activity.
like in this way
public class YourClassName extends Activity {
.....
//add the following code at the end of the class
static
{
System.loadlibrary("opencv_java");
System.loadlibrary("objtrack_opencv_jni");
System.loadlibrary("nonfree");
System.loadlibrary("test_sift12");
}
}
I suggest you change you names of following
LOCAL_MODULE := sift_prebuilt
to
LOCAL_MODULE := nonfree
and
LOCAL_MODULE := opencv_java_prebuilt
to
LOCAL_MODULE := opencv_java
Remember don't forget to modify the names in LOCAL_SHARED_LIBRARIES

Related

Adding a compiler flag to only one file in Android.mk

I have an Android.mk file that has a number of files for which LOCAL_CFLAGS get applied to them. I would like to apply a different flag to only one of the files out of the many. How can this be accomplished?
I searched the internet from the Android perspective, but didn't find a whole lot. Considering the following example I would like to apply flag TEST3 to file test3.c only. I looked at Per-file CPPFLAGS in Android.mk, but I couldn't find anything as far as how to use PRIVATE_CPPFLAGS to one file. Any ideas?
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
include $(BUILD_SHARED_LIBRARY)
The supported way to achieve your goal is to use a separate static library for C/CPP files that need different parameters. In this particular case, the fix would be as easy as
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test3
LOCAL_SRC_FILES := test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3 -DTEST3
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
LOCAL_WHOLE_STATIC_LIBRARIES := test3
include $(BUILD_SHARED_LIBRARY)
There is another approach, similar to one I forged a while ago
LOCAL_PATH := $(call my-dir)
TARGET-process-src-files-tags += $(call add-src-files-target-cflags, $(LOCAL_TEST3_SRC_FILES), $(LOCAL_TEST3_CFLAGS))
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
LOCAL_TEST3_SRC_FILES := test3.c
LOCAL_TEST3_CFLAGS := -DTEST3
include $(BUILD_SHARED_LIBRARY)
If -Dtest3 would do, you can use another hack:
LOCAL_PATH := $(call my-dir)
get-src-file-target-cflags = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$1) -D$(basename $1)_DEFINE
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
include $(BUILD_SHARED_LIBRARY)
See more in How to dynamically get the current compiler target file name in Android.mk's LOCAL_CFLAGS?.

How do I use two different versions of OpenCV in one application?

To get it out of the way, here's my Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include B:/Android/OpenCV/Libraries/OpenCVModified/install/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := DetectionBasedTracker_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include B:/Android/OpenCV/Libraries/OpenCVOriginal/install/sdk/native/jni/OpenCV.mk
LOCAL_SRC_FILES := DetectionBasedTrackerOriginal_jni.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_LDLIBS += -llog -ldl
LOCAL_MODULE := detection_based_tracker_original
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := $(TARGET_ARCH_ABI)/libavcodec
LOCAL_SRC_FILES := libprebuilt/libavcodec.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := $(TARGET_ARCH_ABI)/libavformat
LOCAL_SRC_FILES := libprebuilt/libavformat.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := $(TARGET_ARCH_ABI)/libavutil
LOCAL_SRC_FILES := libprebuilt/libavutil.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := $(TARGET_ARCH_ABI)/libswscale
LOCAL_SRC_FILES := libprebuilt/libswscale.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := $(TARGET_ARCH_ABI)/libffmpeg_mediametadataretriever_jni
LOCAL_SRC_FILES := libprebuilt/libffmpeg_mediametadataretriever_jni.so
include $(PREBUILT_SHARED_LIBRARY)
I have an assingment, where I have to modify OpenCV slightly to speed it up. I want to demonstrate the effects of the changes I've made in one application that uses the original and the modified versions of OpenCV at the same time.
I've already made wrappers and implemented them successfully in the application and the application calls the correct underlying JNI code for each wrapper.
The problem is, that the modules seem to be using only one OpenCV source. It seems to be the one that's included first, if I compile the modified DBT first, it's the modified one and if I compile the original one (after cleaning project) it's the original OpenCV.
I have logcat logs that confirm that the JNI parts are separated correctly, but the OpenCV is uniform for both wrappers.
So what I need to do is get NDK to install both versions of OpenCV and make the modules to use the correct versions, except I have no idea how.

Multiple native Android libraries that are also dependent on libgnustl_shared.so

I have an Android application project that depends on two native libraries, libA and libB. libA depends on libB, and both libraries are built using APP_STL:=gnustl_shared. The problem then arises when trying to build the APK:
[2014-09-30 14:31:47 - Appname] Error generating final archive: Found duplicate file for APK: lib/armeabi/libgnustl_shared.so
Origin 1: /libA/libs/armeabi/libgnustl_shared.so
Origin 2: /libB/libs/armeabi/libgnustl_shared.so
How do I configure these libraries to build/link properly while using a common shared library such as libgnustl_shared.so?
EDIT:
I have tried many alternative makefile settings, so it's hard to know what to post here, but I'll try. Both Application.mk files in libA/jni and libB/jni contain:
APP_STL := gnustl_shared
APP_OPTIM := release
APP_ABI := armeabi-v7a armeabi
APP_PLATFORM := android-15
In libA/jni, the Android.mk file contains:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyBase
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libMyBase.so
LOCAL_PRELINK_MODULE := true
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Base
LOCAL_SRC_FILES := Base.cpp
LOCAL_C_INCLUDES := ../../../../LIBS/MyBase/pub/include
LOCAL_LDLIBS := -llog -ljnigraphics
LOCAL_SHARED_LIBRARIES := MyBase
LOCAL_CPP_FEATURES += rtti
LOCAL_CPP_FEATURES += exceptions
include $(BUILD_SHARED_LIBRARY)
In libB/jni, the Android.mk file contains:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyMedia
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libMyMedia.so
LOCAL_PRELINK_MODULE := true
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := Media
LOCAL_SRC_FILES := Media.cpp media_jni.cpp
LOCAL_C_INCLUDES := ../../../../LIBS/MyBase/pub/include \
../../../../LIBS/MyMedia/pub/include
LOCAL_LDLIBS := -llog -ljnigraphics
LOCAL_SHARED_LIBRARIES := MyBase MyMedia
LOCAL_CPP_FEATURES += rtti
LOCAL_CPP_FEATURES += exceptions
include $(BUILD_SHARED_LIBRARY)
$(call import-module,LIBS/MyBase/jni)
MyBase and MyMedia are two native libraries written in C++ and are not Java/JNI aware. MyMedia is dependent on MyBase. Both libraries are dependent on a robust C++ library such as gnustl_shared.

How to build an shared library and call it in other ndk program

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

NDK adding prebuilt library.Correct method

Is this the correct format to specify the Android.mk?Is there any syntax errors? I couldnt find any source for clearing this doubt.Thanking you in advance.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := imageprocessing
LOCAL_SRC_FILES := imageprocessing.c
LOCAL_LDLIBS := -lm -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libfreeimage
LOCAL_SRC_FILES := libfreeimage.so
include $(PREBUILT_SHARED_LIBRARY)
and in the C code have
#include <android/libfreeimage.h>
since u r creating a shared library from an existing shared library: So u need to first give the pre-built shared library part first then u can create ur own library. so ur code should look like this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libfreeimage
LOCAL_SRC_FILES := libfreeimage.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := imageprocessing
LOCAL_SRC_FILES := imageprocessing.c
LOCAL_LDLIBS := -lm -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
Well,
I got the solution from
http://www.kandroid.org/ndk/docs/PREBUILTS.html
Thank you for your valuable time Subrat nayak,thanks a lot

Categories

Resources