NDK adding prebuilt library.Correct method - android

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

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?.

Android NDK : Aborting. Stop

I'm trying to call C++ method in Java coding.
I received the Android NDK : Aborting. Stop when defined the Android.mk file as below :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := libs/ffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := ffmpeg.cpp
LOCAL_SHARED_LIBRARIES := ffmpeg
include $(BUILD_SHARED_LIBRARY)
But if I defined like this, it run successfully :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := libs/ffmpeg.so
include $(BUILD_SHARED_LIBRARY)
Questions :
1 - How to fix this error?
2 - In Android.mk file, Can not call libs/ffmpeg.so and ffmpeg.cpp in the same time?
People who know this,
Please tell me,
Thanks,
p/s : Project structure :
When seeing the Android.mk structure.
I think I need change Android.mk file like this :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ffmpeg
LOCAL_SRC_FILES := libs/ffmpeg.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := final_ffmpeg
LOCAL_SRC_FILES := ffmpeg.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_PREBUILTS := libs/ffmpeg.so
#libavformat libavcodec libswscale libavutil
include $(BUILD_SHARED_LIBRARY)
The result will help me :
It still be exactly when you need call the C++ function in C++ file from Java file.

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

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

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

how to generate two .so file in a single android.mk file

I'd like to create two .so file within a single .mk file. But the following codes fails because there should not be a space in LOCAL_MODULE. So is there a way out?
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio hello-jni
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := gpio.c hello-jni.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
I just got it works using the following codes.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gpio
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := gpio.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := h ello-jni
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := hello-jni.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)

Categories

Resources