How to include third-party *.a into Android NDK using android studio.? - android-ndk

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)

Related

Adding two prebuilt static and shared libraries using Android.mk

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)

ndk-build for jsoncpp always giving one error

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.

Android NDK thrid party static library - ndk build giving error : jni/libAT_int.a:1:1: invalid character

I'm trying to integrate static library libAT_int.a
I have successfully integrated it in iOS app.
lipo -info libAT_int.a
Architectures in the fat file: libAT_int.a are: armv6 armv7
But when i'm integrating it with Android NDK. I'm getting error during ndk build
error: jni/libAT_int.a:1:1: invalid character
Android.mk has following content :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := AT
ifeq ($(APP_ABI), armeabi-v7a)
LOCAL_SRC_FILES := libAT_int.a
else
LOCAL_SRC_FILES := libAT_int.a
endif
LOCAL_CPP_FEATURES += rtti exceptions
LOCAL_CPP_EXTENSION := .cpp .h
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := MusicSDK
LOCAL_SRC_FILES := MusicSDK.cpp
LOCAL_CPP_FEATURES += rtti exceptions
LOCAL_CPP_EXTENSION := .cpp .h
LOCAL_STATIC_LIBRARIES := AT
include $(BUILD_SHARED_LIBRARY)
You can't use a static library built for iOS when building for Android, you need a static library built specifically for Android.

Prevent manually added libraries from being deleted by ndk-build

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.

How to build third party libraries with Android NDK

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.

Resources