Android NDK project adding another "lib" to my .so file - android

I'm trying to use a C++ function (that is referenced using a .so file) from my C JNI code. To illustrate my project architecture, it looks like this:
Java -> C (JNI) -> C++ function
This is what my Android.mk file looks like:
LOCAL_PATH := $(call my-dir)
# My C++ library
include $(CLEAR_VARS)
LOCAL_MODULE := my_module
LOCAL_SRC_FILES := ../../../../../../module_service/lib/system/lib/libmodule_service.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
# JNI C code
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
LOCAL_C_INCLUDES += ../../../../../module_service/src
LOCAL_SHARED_LIBRARIES := my_module
include $(BUILD_SHARED_LIBRARY)
Using "ndk-build" command, my project builds. But when I try to run it on my device, it crashes with the following message:
java.lang.UnsatisfiedLinkError: ... nativeLibraryDirectories=[/data/app/com.sampleapp.mysampleapplication-1/lib/arm, /vendor/lib, /system/lib]]] couldn't find "liblibmodule_service.so"
What is adding another "lib" to my .so file? If I create another .so file named "liblibmodule_service.so" (in addition to "libmodule_service.so") in my /system/lib directory, it works. But that feels like a hacky solution, and I would prefer to not have both "libmodule_service.so" AND "liblibmodule_service.so" which are the same exact files.
Any help would be greatly appreciated. Thanks!

I guess you may be using System.loadLibrary("libmodule_service"); to load the library, change it to System.loadLibrary("module_service");

Related

Develop/Compile a lib for an android project

I have some cpp files in a directory (10 cpp files and ten relatives .h files corresponding to 10 classes).
I would like to create my own library with them (let call it libTest.a) in order to put it on an android project using NDK.
I have already :
C++ code
Create and configure the android project with NDK, so basically I have an Android.mk and a cpp file into the jni folder
I spent about 10 hours on the web trying to find someone who can explain how to compile its own lib.a
I found a lot about "how can I put a .a in an android project using ndk" but nothing about how to create a makefile, a config file or whatever in order to correctly compile and make a lib compatible with android.
The one who will give me some advices will made my day!
Thanks a lot
(My Android.mk below)
LOCAL_PATH := $(call my-dir)
# static library info
LOCAL_MODULE := libTest
LOCAL_SRC_FILES := ../prebuild/libTest.a
LOCAL_EXPORT_C_INCLUDES := ../prebuild/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES += ../prebuild/include
LOCAL_MODULE := mlaudioengine
LOCAL_SRC_FILES := mlaudioengine.cpp
LOCAL_STATIC_LIBRARIES := libTest
include $(BUILD_SHARED_LIBRARY)

Native method not found while using .so File in libs Android

I have .so file which i have created using a c library .Now i want to use .so file in another project for call those functions. I don't know how to use that library. How to call any function from that library? I have tried but found error as Native method not found.I am a beginner with Ndk in android.any help on this..??
I have referred many links those were not solved my problem as
Native method not found,
java.lang.UnsatisfiedLinkError: Native method not found,
I am facing the same as this link but it is unanswered. link is
Need NDK Help: How to call a C++ function for shared library from another project that uses C++
My Android.mk file is as follows from which i have created .so file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ShairportSample
LOCAL_SRC_FILES := ShairportSample.cpp
APP_PLATFORM := android-19
include $(BUILD_SHARED_LIBRARY)
you can call functions implemented inside the .so you want to reuse from another NDK lib, by setting it as prebuilt shared library your're depending on inside your Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := ShairportSample
LOCAL_SRC_FILES := libs/$(TARGET_CPU_ABI)/libShairportSample.so
LOCAL_EXPORT_C_INCLUDES := PATH_TO_ShairportSample.h
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := yournewlib
LOCAL_SRC_FILES := yournewlib.cpp
LOCAL_SHARED_LIBRARIES += ShairportSample
include $(BUILD_SHARED_LIBRARY)
From your Java code you'll have to load ShairportSample lib from your Java before loading yournewlib.
Otherwise, if you want to reuse your .so directly from Java, you should export a Java library (.jar) that uses it. Then you can reuse this Java lib from your other project.
first of all, you should make sure the .so file being compiled correctly, named it like this libYourLibName.so, put it into libs/armeabi folder.
secondly, make sure you have loaded the .so file from static block
static {
System.loadLibrary("YourLibName");
}
may this will help you

NDK: using user created .so when trying to build another .so

I'm trying to link some .so that I generated using the NDK to a new .so I'm trying to create,
as the old .so contains definitions of functions that I want to use in the new .so.
I've tried this Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := prog_test
LOCAL_SRC_FILES := main.c
LOCAL_MODULE_TAGS := optional
LOCAL_SHARED_LIBRARIES += mylib
include $(BUILD_SHARED_LIBRARY)
$(call import-module,<tag>) # with and without
I've also tried this method I found in stackoverflow NDK - How to use a generated .so library in another project but still no succes as I get always :
prebuilt/linux-x86/toolchain/arm-linux-androideabi-4.4.x/bin/../lib/gcc/arm-linux-androideabi/4.4.3/../../../../arm-linux-androideabi/bin/ld: error: cannot find -lmylib.so
I really appreciate any help to solve this issue.
B.R
you have to use include $(PREBUILD_SHARED_LIBRARY) instead of include $(BUILD_SHARED_LIBRARY)

Android NDK: Problems building library depending on a prebuilt one

I have a c library called B and it depends on other c library called A. I am sure I can successfully compile and use A through Android NDK.
Now I am trying to compile the B library using Android NDK. I have and Android project with a jni folder. My jni folder contains the A and B folders, they have the libraries c code. The jni folder also has a prebuilt folder, and it contains the a.so file (the prebuilt A library file).
My problem is that I can not build the B. I can compile it, but I cannot link it. Please, could anyone help me pointing what would be my mistake? A and B are generic names I am using to my projects, not the real ones I am using. I will list below the Android.mk files.
The Android.mk file used to build A (this file is not visible to the Android Project I described, although it has also the A code):
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libA
LOCAL_SRC_FILES := helloworld.c A/src/fileA.c
LOCAL_C_INCLUDES := $(LOCAL_PATH)/A/src
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/A/src
include $(BUILD_SHARED_LIBRARY)
The Android.mk file in the prebuilt folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libA
LOCAL_SRC_FILES := prebuilt/libA.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/A/src
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libB
LOCAL_SRC_FILES := B/src/fileB.c
LOCAL_SHARED_LIBRARIES := libA
include $(BUILD_SHARED_LIBRARY)
When I execute the ndk-build command I have the final output to be something like:
B/src/fileB.c:15: error: undefined reference to
'A_function' collect2: ld returned 1 exit status make: *
[obj/local/armeabi/libB.so] Error 1
Your B library files can actually access the A library, but they just don't know how. Add the required headers (.h files from the A library) to a subfolder of JNI and specify the following :
LOCAL_C_INCLUDES := $(LOCAL_PATH)/your/sub/folder
in your B project declaration.
I still had a problem, and had to declare the A library as PREBUILT_STATIC and then add to the B library
LOCAL_WHOLE_STATIC_LIBRARIES := libA

Undefined reference error while compiling C files with android ndk

I have a bunch of C files with extensions .c and .h. I want to compile these files with Android NDK. When I tried with only one file, NDK worked perfectly, but when I tried including other files inside this main C files with includes, I get an error. What an I missing? This is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := ndksetupdemo
LOCAL_SRC_FILES := mymain_c_file.c
LOCAL_C_INCLUDES := includes
include $(BUILD_SHARED_LIBRARY)
Should I include anything more?
It appears that you only link against the log library (LOCAL_LDLIBS := -llog). If you are referencing functions that are not defined in any of the included headers and in your mymain_c_file.c, you will get the undefined reference error. You will need to find out what other libraries you need to link against and list them in LOCAL_LDLIBS.
If the functions are defined in the other .c files, you need to add them to the LOCAL_SRC_FILES variable.

Categories

Resources