How can I compile third party libraries with the android NDK? I am compiling a wrapper which implements the JNI functions as a shared lib, which depends on another 3rd party lib (HTK). I don't know how to setup the makefile. The following does not work:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
include HTKLib/Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gaitfuncs
LOCAL_SRC_FILES := gaitfuncs.c
%LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
The second makefile should then build a static lib which my shared lib links to. How can I include this subdir makefile properly? Is this the correct way of doing it? And as a bonus: Are there wildcards for the LOCAL_SRC_FILES variable to take all files ending in .c for example.
Thanks!
I found a solution:
JNIPATH := $(call my-dir)
LOCAL_PATH := $(JNIPATH)
include $(call all-subdir-makefiles)
LOCAL_PATH := $(JNIPATH)
include $(CLEAR_VARS)
LOCAL_MODULE := gaitfuncs
LOCAL_SRC_FILES := gaitfuncs.c
LOCAL_STATIC_LIBRARIES := htk
include $(BUILD_SHARED_LIBRARY)
Calling the CLEAR_VARS function before calling the subdir-makefiles function wasn't exactly elegant ;)
In the documentation.html in Android NDK folder,
take a look at the macro function "my-dir"
Returns the path of the last included Makefile, which typically is the
current Android.mk's directory. This is useful to define LOCAL_PATH at
the start of your Android.mk as with:
LOCAL_PATH := $(call my-dir)
and the macro function "all-subdir-makefiles"
Returns a list of Android.mk located in all sub-directories of the
current 'my-dir' path.
Related
I am trying to add both Dlib and superpowers libraries to my android project using Android.mk. I included successfully prebuilt shared Dlib library (.so files) using Android.mk, but when I am trying to add the superpower as a static library (the package has .a files and CMakeLists.txt), I am facing issue regarding liking JNI functions with java. I cannot use the JNI functions in java. I would appreciate it if someone can help me. Here is my Android.mk file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libs
LOCAL_SRC_FILES := /[MyProjectAddress]/app/libs/$(TARGET_ARCH_ABI)/libdlib.so
LOCAL_EXPORT_C_INCLUDES := /[MyDLIBFileAddress]/dlib_v19.7/dlib
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := superpoweredLib
LOCAL_SRC_FILES := /[MyProjectAddress]/app/libs/$(TARGET_ARCH_ABI)/libSuperpoweredAndroid$(TARGET_ARCH_ABI).a
LOCAL_EXPORT_C_INCLUDES := /[SuperpoweredFileAddress]/SuperpoweredSDK/Superpowered/AndroidIO/SuperpoweredAndroidAudioIO.cpp \ /[SuperpoweredFileAddress]/SuperpoweredSDK/Superpowered \
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := FrequencyDomain
LOCAL_LDFLAGS := -Wl,--build-id
LOCAL_LDLIBS += -llog -ldl
LOCAL_SRC_FILES := \ /[MyProjectAddress]/app/src/main/jni/FrequencyDomain.cpp \
LOCAL_C_INCLUDES += /[MyProjectAddress]/app/src/debug/jni
LOCAL_C_INCLUDES += /[MyProjectAddress]/app/src/main/jni
LOCAL_SHARED_LIBRARIES := libs
include $(BUILD_SHARED_LIBRARY)
LOCAL_SHARED_LIBRARIES := superpoweredLib
include $(BUILD_SHARED_LIBRARY)
Pre-requisites : I am using Android Studio 2.1.2
I have downloaded source of jsoncpp from following link
https://github.com/open-source-parsers/jsoncpp
I have already checked following SO thread , not getting proper solutions :
LOCAL_MODULE_FILENAME should not include file extensions i get this error each time i run ndk-build in terminal
Using JsonCpp on X-Cross platform library
My common Android.mk is as follows:
LOCAL_PATH := $(call my-dir)
#JsonCpp lib
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
include $(LOCAL_PATH)/jsnlib/Android.mk
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
With code for building some other libraries too , which is working fine.
My jsoncpp's Android.mk is as follows
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsnlib/include/json/*.h
FILE_LIST += $(wildcard $(LOCAL_PATH)/jsnlib/src/lib_json/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_MODULE := jsnlib
LOCAL_MODULE_FILENAME:= libjsnlib
include $(BUILD_STATIC_LIBRARY)
When I do ndk-build always getting following error
Android NDK: jni/jsnlib/Android.mk:jsnlib: LOCAL_MODULE_FILENAME should not include file extensions
Android NDK: jni/jsnlib/Android.mk:jsnlib: LOCAL_MODULE_FILENAME must not contain a file extension
What happens here is that while you're in the middle of defining your jsoncpplib module you include another makefile, which contains its own module definition:
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
include $(LOCAL_PATH)/jsnlib/Android.mk
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
You should move the inclusion of the other makefile to above the where you're doing CLEAR_VARS:
include $(LOCAL_PATH)/jsnlib/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Also, LOCAL_MODULE_FILENAME:= libjsnlib seems redundant, since LOCAL_MODULE := jsnlib should result in the same library name.
I am having .a file from third-party. How can use it in android studio?
Please help me.
You can not use directly a .a library in your app. This libraries are static and Android app only allow you to load dynamic libraries.
But you can build a dynamic library linking with your static library. Si you just have to add a few line to you Android.mk to link with this static library. Put your prebuilt .a and his header in a prebuild jni alongside your jni folder. Then your Android.mk should look like something like this :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := YourStaticLib
LOCAL_SRC_FILES := ../prebuilt/your_static_lib_prebuild.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../prebuilt
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := DynamicLib
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/../prebuilt
LOCAL_SRC_FILES := your_src_file.cpp
LOCAL_LDLIBS := -llog
LOCAL_ARM_NEON := true
LOCAL_STATIC_LIBRARIES := YourStaticLib
include $(BUILD_SHARED_LIBRARY)
I've searched a lot of topics about linking libpng to my android ndk project but I've found right answer for my problem and I hope somebody will help me.
This is hierarchy of my project:
jni
different_cpp_files
different_hpp_files
Android.mk
libpng
different_cpp_files
different_hpp_files
Android.mk
Android.mk in libpng folder:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LS_C=$(subst $(1)/,,$(wildcard $(1)/*.c))
LOCAL_MODULE := png
LOCAL_SRC_FILES := \
$(filter-out example.c pngtest.c,$(call LS_C,$(LOCAL_PATH)))
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_LDLIBS := -lz
include $(BUILD_STATIC_LIBRARY)
I suppose that everything is right here..
Android.mk in jni folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LS_CPP=$(subst $(1)/,,$(wildcard $(1)/*.cpp))
LOCAL_MODULE := pacman
LOCAL_CFLAGS := -Wno-psabi
LOCAL_SRC_FILES := $(call LS_CPP,$(LOCAL_PATH))
LOCAL_LDLIBS := -landroid -llog -lEGL -lGLESv1_CM -lOpenSLES
LOCAL_STATIC_LIBRARIES := android_native_app_glue png
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
$(call import-module,libpng)
The last line shows that I got libpng like native_app_glue lib(in the directory of android-ndk sources) Now I want to compile libpng from my project. What I need to change in Android.mk file?
i've got another way for you:
Download all files from here and paste it into a new folder anywhere on your system:
https://github.com/julienr/libpng-android
go into the folder and run:
./build.sh
You will get an libpng.a file in [YOUR_FOLDER]/obj/local/armeabi/libpng.a
Copy this file into:
[YOUR_ANDROID_NDK_FOLDER]/platforms/[ALL_FOLDERS_IN_HERE]/arch-arm/usr/lib/
now you can use libpng in all your projects with the simple line:
LOCAL_LDLIBS += -lpng
you only have to include this in your cpp's:
#include <png.h>
Have fun!
I have a project which is reusing a native library (libocr.so) pre-compiled and for which I don't have source files.
I manually put the library on libs/armeabi of my project and everything works perfectly.
Then I needed to create a new native library to the same project. I put my source code as weel as the Android.mk file in my jni folder and I build it with ndk-buld command.
The library is build and placed in libs/armeabi folder, but libocr.so (the one manually added) is automatically deleted from there...
How can I prevent libocr.so from being deleted?
Here is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libyuv
LOCAL_SRC_FILES := ycrcbutils.c
include $(BUILD_SHARED_LIBRARY)
Thanks in advance for any help, Luca.
...ok I found the answer by myself...
according to ndk/docs/PREBUILTS.HTML I changed my Android.mk like this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := libyuv
LOCAL_SRC_FILES := ycrcbutils.c
include $(BUILD_SHARED_LIBRARY)
# Add prebuilt libocr
include $(CLEAR_VARS)
LOCAL_MODULE := libocr
LOCAL_SRC_FILES := libocr.so
include $(PREBUILT_SHARED_LIBRARY)
and placed a copy of my libocr.so under jni folder of my project.