Moving native libraries from android app to system lib and accessing it from app
Asked Answered
D

1

0

I have compiled one native code with android ndk. Now instead of keeping that library as part of .apk i want install it in android system (system/lib64) and my application load from system. I pushed the library to the system/lib64 and deleted the libraries from the android application's lib folder. But when i try to run i am getting

"java.lang.UnsatisfiedLinkError: dlopen failed: library "something.so" not found"

Following is my makefile

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/Include
LOCAL_SRC_FILES := ABCProtocol/ABCProtocol.c ABCProtocol/DatalinkLayer.c ABCProtocol/PhysicalLayer.c
LOCAL_MODULE := libABCXYZProtocol
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := eng
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include $(LOCAL_PATH)/Include $(LOCAL_PATH)/Util $(LOCAL_PATH)/Include/Linux/XYZ $(LOCAL_PATH)/ABCProtocol
LOCAL_SRC_FILES := Linux/XYZ/ABCXYZLinux.c Util/Util.c Util/Logger.c
#LOCAL_CFLAGS += -Wno-error=format-security
LOCAL_CFLAGS += -w
LOCAL_MODULE := libABCXYZWrapper
LOCAL_SHARED_LIBRARIES :=libABCXYZProtocol
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS) 
LOCAL_MODULE_TAGS := eng
LOCAL_MODULE:=com_example_reader_ABCXYZappnote_NativeLibrary
LOCAL_SRC_FILES:=com_example_reader_ABCXYZappnote_NativeLibrary.c
LOCAL_C_INCLUDES += $(LOCAL_PATH) $(LOCAL_PATH)/$(KERNEL_DIR)/include $(LOCAL_PATH)/Include $(LOCAL_PATH)/Util $(LOCAL_PATH)/Include/Linux/XYZ
#LOCAL_CFLAGS += -DANDROID
LOCAL_SHARED_LIBRARIES:=libc libABCXYZWrapper libABCXYZProtocol
include $(BUILD_SHARED_LIBRARY)
Duvall answered 4/10, 2016 at 13:30 Comment(7)
Can you share the code of your makefile(Android.mk)? This problem comes when you did not push the dependant libraries. Linking fails in such cases.Artiste
Also re-build and install the application after removing the library. If there are no dependencies and still you see the error, may be you have to build the library again by putting it in the android source code and using mm command.Artiste
I pushed the libraries manually to lib64. I can see the libraries there. Should i give System.loadLibrary("system/lib64/smthng.so");?Duvall
can you share the makefile source code? The makefile you used to build libsomething.so. It will help me understand what is your library and whether it has any dependencies.. is libsomething.so the main library that your app is trying to laod or is it a dependant library of some other library..Artiste
libABCXYZProtocol, libABCXYZWrapper are the ones which i pushed to the lib64 folder under the system and removed from the android code. The y are simple NDK compilation. The last one is actually a JNI code.Duvall
I saw your makefile.... Now what is the error? BDW LOCAL_MODULE:=com_example_reader_ABCXYZappnote_NativeLibrary Thats a very big name for the library... recommend to choose a small name. And also BUILD_SHARED_LIBRARY is used 2 times which is unnecessary i guess.Artiste
@mk.. That was by mistake i wrote BUILD-SHARED-LIBRARY twice. The error is, if i build .so files using this makefile and move the first two .so files to system/lib64 and try to install the app. It crashes with the above mentioned error.java.lang.UnsatisfiedLinkError: dlopen failed: library "libABCXYZProtocol.so" not found"Duvall
W
0

Applications are not allowed to access non-public APIs from system library locations: https://developer.android.com/about/versions/nougat/android-7.0-changes.html#ndk. Libraries used by your app must be packaged with your app.

Why do you want to put them in /system/lib anyway?

Waterborne answered 4/10, 2016 at 17:37 Comment(4)
I am trying to do it with Android 6.0 SDK and my target device is also 6.0. I want to do it because I have some operations (accessing hardware) in the native library which won't work if i package with APK. I thought it will work if i use the library from the system/libDuvall
You won't be able to ship that app though. You can't modify your users' /system partition.Waterborne
I am not planning to ship the app.. it's for a reference board. We are using it only internally within our firmDuvall
The NDK isn't what you're looking for then. You'll want to take a look at the AOSP build process and actually write a system library and making it a part of the system image rather than using the NDK. Apps can load vendor specific extensions via dlopen (though I'm not certain how this works in a 7.0 and newer world).Waterborne

© 2022 - 2024 — McMap. All rights reserved.