0

I've been trying to perform some native library builds for my Android App. More specifically performing ndk-build, with the help of some prebuilt shared libraries. One of the modules (let's call it pyjni) depends on those prebuilt shared libraries. Therefore my Android.mk consists of two modules for the prebuilt and to-be-built libraries respectively, as recommended for android ndk development.

This is my Android.mk file for the App:

LOCAL_PATH := $(call my-dir)
TARGET_ARCH_ABI := armeabi-v7a

include $(CLEAR_VARS)
LOCAL_SRC_FILES :=  $(LOCAL_DIR)/libs/$(TARGET_ARCH_ABI)/libprebuilt.so
LOCAL_MODULE := prebuilt
include $(PREBUILT_SHARED_LIBRARY)


# Build libpyjni.so
include $(CLEAR_VARS)
LOCAL_MODULE    := pyjni
LOCAL_SRC_FILES := pyjni.c
LOCAL_C_INCLUDES:= $(LOCAL_DIR)/include/$(TARGET_ARCH_ABI)/
LOCAL_SHARED_LIBRARIES := prebuilt
LOCAL_LDLIBS += -llog
include $(BUILD_SHARED_LIBRARY)

Build turns out fine. In fact, when examining the .APK both the pre-built and built shared libraries are packaged inside @ lib/armeabi-v7a. So far so good.

However, when trying to load the built shared library as System.loadLibrary("pyjni") during the App execution, it crashes notifying the following error: java.lang.UnsatisfiedLinkError: dlopen failed: library ".../app/build/intermediates/ndkBuild/debug/obj/local/armeabi-v7a/libprebuilt.so" not found.

I can succesfully load the prebuilt library using System.loadLibrary("prebuilt"). So at this point I suspect it is an issue on how the prebuilt shared library is being packaged or rather how LOCAL_SHARED_LIBRARIES is working. In the app's build.gradle file the sourceSets.main.jniLibs.srcDirs points to the directory where libprebuilt.so is placed. To avoid packaging issues with multiple instances of the prebuilt library file, the gradle file specifies

packagingOptions{
        pickFirst 'lib/armeabi-v7a/libprebuilt.so'
    }

At this point I'm confused with how the link created by LOCAL_SHARED_LIBRARIES is working on execution.

  • This used to be a problem at some older NDK release. Not during the build of libpyjni, but rather the build of libprebuilt – Alex Cohn Apr 18 '19 at 17:25
  • @AlexCohn Thanks for the reply. NDK is up to date (version r19c) on Android Studio, so it shouldn't be about that. I agree there must be some issue on how libprebuilt.so is being built with ndk-build for the APK... – Miguel Viana Apr 24 '19 at 12:11
  • If you cannot rebuild the prebuilt library, you can edit its ELF header using [patchelf utility](https://stackoverflow.com/a/20333550/192373). – Alex Cohn Apr 24 '19 at 12:36

0 Answers0