Linking with updated library on Android - android

I have developed an application and a native library for Android. The native library uses openSL ES for audio processing.
In my Android.mk file I have the following statement:
LOCAL_LDLIBS := -lOpenSLES
So I'm guessing that this means that the application will dynamically link in the openSLES library from the device's system/lib folder at the time when the application is loaded/executed on the device?
The problem I'm facing is that the libraries on the device are buggy and I have 3 updated libraries which contain the bug fix. If possible, how do I make sure that my native library is using the 3 libraries I have:
Libwilhelm.so
libOpenMAXAL.so
libOpenSLES.so
Do I just replace
LOCAL_LDLIBS := -lOpenSLES
with
LOCAL_SHARED_LIBRARIES := -lOpenSLES -lOpenMAXAL -lwilhelm

As long as you target a specific device or a very limited set of devices, the proposed solution is good enough. But if your aim is a public app, which will be installed on different platforms, including future 'N' version of Android, and customized ROMs, including e.g. Samsung, you should be careful with the system dependencies of these libraries.
While OpenSLES and OpenMAXAL are innocent (they only depend on liblog and libwilhelm), the latter requires more care.
Looking at its Android.mk, libwilhelm depends on liblog libutils libmedia libbinder libstagefright libstagefright_foundation libcutils libgui libdl libeffects and libstagefright_http_support.
Only liblog and libdl are "official" (i.e., part of NDK). The others depend on platform, and their exported functions may be incompatible for different devices running same platform level.
To be on the safe side, I would only introduce the fixes, and keep using the system version of libwilhelm when possible. I hope you can reduce your system dependencies this way.

Related

std::chrono with NDK r10e could not be resolved

Do not think I did not search, my Android project (on Eclipse) refuses to recognize std::chrono library. The include is OK in my header file :
#include <chrono>
But when I want use it :
using namespace std::chrono;
I have a : Symbol 'chrono' could not be resolved, and all the functions of chrono are unavailables.
So I use NDK r10e, I add some lines in my Application.mk, which now looks like this :
APP_PLATFORM := android-22
APP_STL := gnustl_static
APP_CPPFLAGS := -std=gnu++11
NDK_TOOLCHAIN_VERSION := 4.8
And in my Android.mk, I add :
LOCAL_CFLAGS += -std=gnu++11
It did not solve my problem. Any ideas ? Bad Eclipse configuration ?
After modifications in mk files, I have build and re-build my project.
This is known problem of GNU libstdc++ in Android NDK. It's built on top of very limited libc (Google's Bionic) and thus can't provide full C++ Standard Library functionality. In particular, std::chrono is almost completely disabled at build time, but not only std::chrono. There are many other classes and functions being disabled, so Google's NDK just don't support C++ fully.
You can switch to LLVM libc++ (APP_STL := c++_static), but it has experimental status in Google's Android NDK and is actually unstable (i.e. it cause crashes in the application even for completely standard C++ code). This instability is caused by the same reason as for GNU libstdc++ - i.e. because it's built on top of very limited libc.
I'd recommend switch to CrystaX NDK - alternative fork of Google's Android NDK, which I've started mainly to solve Google's NDK problems such as non-standard implementations of libc, libc++, etc. CrystaX NDK is developed to work as a drop-in replacement for Google's NDK (except for the fact that it provides fully standard-conforming low-level libraries). In CrystaX NDK, both GNU libstdc++ and LLVM libc++ are much more stable and fully conforming to C++ standard, at least at the same level as they conform to it on GNU/Linux. In particular, std::chrono is fully implemented there and works just fine. Also, in CrystaX NDK, you can use more recent compilers such as gcc-5.3 and clang-3.7, with better support of C++11 and C++14. I'd be happy if it helps you.

Android NDK and pthread

I'm compiling Qt/C++ project with android NDK standalone toolchain. I’ve created standalone toolchain with make-standalone-toolchain.sh --arch=arm --toolchain=arm-linux-androideabi-4.9 --platform=android-21 command. NDK version is android-ndk-r10e. Target project uses some functions from pthread library. At compile time, I get the following error:
error: 'pthread_getaffinity_np' was not declared in this scope
const int err = pthread_getaffinity_np(_pthreadId, sizeof(cpu_set_t), &cpuSetMask);
compilation terminated due to -Wfatal-errors.
I've checked the header of pthread included in ndk toolchain and I did not find the declaration of pthread_getaffinity_np function.
Is pthread functionality for Android limited? How to use pthread with Android NDK properly?
Is pthread functionality for Android limited?
AFAIK, Yes.
http://mobilepearls.com/labs/native-android-api/#pthreads
https://web.archive.org/web/20180602101341/http://mobilepearls.com/labs/native-android-api/#pthreads
POSIX threads (pthreads)
The android libc, bionic, provides built-in support for pthreads, so no
additional linking (-lpthreads) is necessary. It does not implement full
POSIX threads functionality and leaves out support for read/write locks,
pthread_cancel(), process-shared mutexes and condition variables as well as
other more advanced features. Read the bionic OVERVIEW.txt for more
information.
TLS, thread-local storage, is limited to 59 pthread_key_t slots available
to applications, lower than the posix minimum of 128.
See https://android.googlesource.com/platform/bionic/+/master/docs/status.md for our official docs about what is in which Android version.
you can also look at the <pthread.h> header in the NDK (current version here) and see for example entries like:
pid_t pthread_gettid_np(pthread_t __pthread) __INTRODUCED_IN(21);
this shows that we do have the non-POSIX/non-portable (_np) function pthread_gettid_np, but that it was introduced in API level 21, so if your code needs to run on older releases you can't use it.
basically the headers are the canonical source of truth for "which functions are available in which API levels?".
for the specific case of pthread_getaffinity_np, no, we don't support that. you can combine pthread_gettid_np from <pthread.h> and sched_getaffinity from <sched.h> though.
POSIX threads (pthreads) seems to be not provided for -host build modules.
at least here is the error for the libcrypto-host module build:
out/host/linux-x86/obj/SHARED_LIBRARIES/libcrypto-host_intermediates/src/crypto/thread_pthread.o:
In function `thread_local_init':
/media/compilation/projects/android/beagle2/external/boringssl/src/crypto/thread_pthread.c:112:
undefined reference to `pthread_key_create'
and the only way to fix it so far is to add -lpthread inside
external/boringssl/Android.mk before directive:
include $(BUILD_HOST_SHARED_LIBRARY)
example:
# Host shared library
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libcrypto-host
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/include
LOCAL_MULTILIB := both
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk
LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -Wno-unused-parameter
LOCAL_CFLAGS += -DOPENSSL_NO_ASM
LOCAL_LDLIBS += -lpthread
include $(LOCAL_PATH)/crypto-sources.mk
include $(BUILD_HOST_SHARED_LIBRARY)

Android: How to build and replace modified AOSP code

I am starting to work with stage fright frame work to implement hardware decoder in android prior to Jelly bean in my video conferencing application.
I have downloaded and built the android source code in Mac system. I am not clear with the whole idea of working with AOSP. And my questions are (with respect to stagefright framework)
Where can I find the libstagefright.so after AOSP build ?.
If I use the OMX codec in my class for decode, how should I link the libstagefright.so to native code of my application ? If I build my native code by copying the libstagefright.so and linking it via make file is that the way ?
How can I use it in my application ? If I load it via System.loadLibrary(" "), will it work ??
UPDATE:
I have tried the suggestion from Ganesh. But when I tried to build the project with NDK it is not taking the headers included as LOCAL_C_INCLUDES.
Here is my android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := custom_decoder
LOCAL_C_INCLUDES := $(LOCAL_PATH)\includes \
frameworks/av/media/libstagefright/include \
frameworks/native/include/media/openmax \
frameworks/base/include/media/stagefright
LOCAL_SHARED_LIBRARIES := \
libstagefright libstagefright_omx libstagefright_foundation libutils liblog
LOCAL_SRC_FILES := custom_decoder_jni.cpp custom_decoder.cpp
include $(BUILD_SHARED_LIBRARY)
the error is shown from custom_decoder.h when it is reading the includes of AOSP.
fatal error: DataSource.h: No such file or directory.
I haven"t included any AOSP .so in my project(as per Ganesh's suggestion in comment 2). Should I do that?
What else should I do to get it built......
You don't need to rebuild libstagefright.so to use it in your app. You can adb pull the library from your device, or even from an emulator.
Please note that libstagefright communicates with many components in /system/lib, some of which are specific to SoC vendor (e.g. Qualcomm) or ODM (e.g. Samsung). Some uses of Stagefright may require quirks specific to device. This is why OpenMAX IL has not been officially exposed on Android. On the otehr hand, OpneMAX AL has been officially supported since November 2011.
At any rate, libstagefright cannot be directly accessed from Java. If you are looking for a Java solution for video communication, you should first look at the excellent libstreaming library.
The advantage of using libstagefright comes usually when you have you your native (C/C++) library that only has to be connected to an efficient codec.
Here is a nice answer on a related question about Android hardware decoder.
To answer your specific queries,
libstagefright.so is built and installed at /system/lib
I presume you are employing the libstagefright.so in native code. With this assumption, you don't have to copy. I presume you are building your module as a loadable library i.e. .so file. Hence, if you can identify the dependency on libstagefright.so through LOCAL_SHARED_LIBRARIES as well as including the header files, it should be more than sufficient for building your module. Please refer to this example of building a FLAC encoder where similar dependencies have been handled.
By application, if you are referring to a Java application which interacts with a JNI layer, then point 2 should be more than sufficient. However, if you are creating a native layer application, I would recommend you to follow stagefright command line utility's makefile.

Linking cross-platform library to native android application

The problem:
I have a native Android application that is compiled for x86 and arm, armv7a.
The app also links to a pre-shared library.
That pre-shared library is also compiled for x86, arm, and armv7a. So there are 3 lib.so files.
What must I do in the Android.mk/Application.mk to link to the appropriate *.so file given the architecture it is being compiled for?
That is, when the build system is compiling for arm, it should link to the libs/armeabi/lib.so.
Likewise, when the Build system is compiling for x86, it should use the libs/x86/lib.so file.
I believe the alternative might be a more complex build script but I'm shooting for the simple solution first, if it exists.
Thanks!
The answer
Unfortunately my query skills were not very good and shortly after posting, I found the question and answer already on SO:
How can i Link prebuilt shared Library to Android NDK project?
To summarize:
Prebuilt shared libraries, compiled for different platforms, should all be named the same and go under the jni/${ARCH}/ directory.
That is, the structure should appear as so:
jni/x86/libtest.so
jni/armeabi/libtest.so
jni/armeabi-v7a/libtest.so
You should use the $(TARGET_ARCH_ABI) flag, for example:
include $(CLEAR_VARS)
LOCAL_MODULE := mylib-prebuilt
LOCAL_SRC_FILES := ../path_to_prebuilt_folder/libs/$(TARGET_ARCH_ABI)/libmylib.so
include $(PREBUILT_SHARED_LIBRARY)

How to link to the libmedia.so system library in an Android NDK app using android.mk?

I want to create a Player which uses Android system/lib/libmedia.so.
Directly use JNI to play a video.
In Android.mk, i add "-lmedia" for including the library, but i can't link this directly.
This is my process.
write a Cpp file which includes some header file in libmedia.so
add "-lmedia" in Android.mk of LOCAL_LDLIBS
such as..
LOCAL_LDLIBS -lmedia -lstagefright
use ndk-build to build .so
error occured!!
Does anybody have some answer to help???
libmedia.so and libsinstructionght.so are not part of the public API. This means that in theory, you should not rely on them. In practice, though, these libraries are present on all devices, but they are different.
You can extract this binary file from your device, or even from an emulator using command
adb pull /system/lib/libmedia.so C:/android-ndk/platforms/android-14/arch-arm/usr/lib
This will put ths file together with the public API so that using it with ndk-build is easier. On the other hand, you should be aware of fragmentation not lnly between different levels of Android, but also chipsets, manufacturers, and even models.
To handle this, I pull .so files from different devices into separate directories, and add one of them to the linker path, e.g.
LOCAL_LDLIBS += -Lc:/android/galaxys.4.1.2.system.lib
This instruction above can not resolve the big problem you are facing with your approach. libmedia.so is not intended to be linked to user apps. It assumes the context of a privileged user with access to protected devices, such as camera, codecs, and screen.
You can make full use of this library if you target a rooted device, or prepare a custom ROM. And know what you are doing, and how to avoid stealing essential resources from the system.
Otherwise there is very little gain in linking the media lib.
PREBUILT_SHARED_LIBRARY
Points to a build script used to specify a prebuilt shared library.
Unlike BUILD_SHARED_LIBRARY and BUILD_STATIC_LIBRARY, the value
of LOCAL_SRC_FILES must be a single path to a prebuilt shared
library (e.g. foo/libfoo.so), instead of a source file.
You can reference the prebuilt library in another module using
the LOCAL_PREBUILTS variable (see docs/PREBUILTS.html for more
information).
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := libfoo.so
include $(PREBUILT_SHARED_LIBRARY)
Refrenced from NDK documentation.
PREBUILT_STATIC_LIBRARY
This is the same as PREBUILT_SHARED_LIBRARY, but for a static library
file instead. See docs/PREBUILTS.html for more.
Please read the NDK documentation for more details.

Categories

Resources