androidmakefilejava-native-interfaceandroid-sourceandroid-11

How can I add native c++ code to an AOSP app using JNI? I am trying to modify EmbeddedKitchenSinkApp. I am working with Android 11


I am trying to add native C++ code to the app, but I get an linker error on execution. I am guessing there is a problem somewhere in my Android.mk files or maybe I should just manually move the shared library? I am building with AOSP on branch android11-release.

I added the following to the main Android.mk file for the kitchen sink app around the existing include $(BUILD_PACKAGE):

LOCAL_JNI_SHARED_LIBRARIES := libjni_mylib

include $(BUILD_PACKAGE)

include $(call all-makefiles-under, $(LOCAL_PATH))

I added a directory called jni with my c++ source inside as well as the following Android.mk:

LOCAL_PATH:= $(call my-dir)

# MyLib
include $(CLEAR_VARS)

LOCAL_CPP_EXTENSION := .cpp
LOCAL_SDK_VERSION := 17
LOCAL_MODULE    := libjni_mylib
LOCAL_SRC_FILES := MyJniClient.cpp MyCode.cpp
LOCAL_PRODUCT_MODULE := true

LOCAL_CFLAGS += -ffast-math -O3 -funroll-loops
LOCAL_CFLAGS += -Wall -Wextra -Werror
LOCAL_ARM_MODE := arm

include $(BUILD_SHARED_LIBRARY)

When I run the app and it executes the JNI function, it crashes with this error:

E AndroidRuntime: java.lang.UnsatisfiedLinkError: dlopen failed: library "libjni_mylib.so" not found

However, I see on the target (which is an emulator) that /system/priv-app/MyKitchenSinkApp contains MyKitchenSinkApp.apk and lib/x86_64/libjni_mylib.so. Why is the linker unable to find the shared object file? Is there any more information I should share to help?

Edit: I found that libjni_mylib.so is actually just a symbolic link to /system/lib64/libjni_mylib.so which doesn't exist. Now I'm not sure how to fix that. Seems like when I do make it doesn't actually build the .so file even though the makefile is reached.


Solution

  • Turns out I had a typo in the file name in the makefile.