Morning!
I've created a small NDK project which allows dynamic serialisation of objects between Java and C++ through JNI. The logic works like this:
Bean -> JavaCInterface.Java -> JavaCInterface.cpp -> JavaCInterface.java -> Bean
The problem is I want to use this functionality in other projects. I separated out the test code from the project and created a "Tester" project. The tester project sends a Java object through to C++ which then echo's it back to the Java layer.
I thought linking would be pretty simple - ("Simple" in terms of NDK/JNI is usually a day of frustration) I added the JNIBridge project as a source project and including the following lines to Android.mk:
NDK_MODULE_PATH=.../JNIBridge/jni/"
JNIBridge/jni/JavaCInterface/Android.mk:
...
include $(BUILD_STATIC_LIBRARY)
JNITester/jni/Android.mk:
...
include $(BUILD_SHARED_LIBRARY)
$(call import-module, JavaCInterface)
This all works fine. The C++ files which rely on headers from JavaCInterface module work fine. Also the Java classes can happily use interfaces from JNIBridge project. All the linking is happy.
Unfortunately JavaCInterface.java which contains the native method calls cannot see the JNI method located in the static library. (Logically they are in the same project but both are imported into the project where you wish to use them through the above mechanism).
My current solutions are are follows. I'm hoping someone can suggest something that will preserve the modular nature of what I'm trying to achieve:
My current solution would be to include the JavaCInterface cpp files in the calling project like so:
LOCAL_SRC_FILES := FunctionTable.cpp $(PATH_TO_SHARED_PROJECT)/JavaCInterface.cpp
But I'd rather not do this as it would lead to me needing to update each depending project if I changed the JavaCInterface architecture.
I could create a new set of JNI method signatures in each local project which then link to the imported modules. Again, this binds the implementations too tightly.
Overview. The Android.mk file resides in a subdirectory of your project's jni/ directory, and describes your sources and shared libraries to the build system. It is really a tiny GNU makefile fragment that the build system parses once or more.
NDK_PROJECT_PATH - the location of your project NDK_APPLICATION_MK - the path of the Application.mk file APP_BUILD_SCRIPT - the path to the Android.mk file. These are needed to override the default values of the build script, which expects things to be in the jni folder.
The Native Development Kit (NDK) is a set of tools that allows you to use C and C++ code with Android, and provides platform libraries you can use to manage native activities and access physical device components, such as sensors and touch input.
JNI is the Java Native Interface. It defines a way for the bytecode that Android compiles from managed code (written in the Java or Kotlin programming languages) to interact with native code (written in C/C++).
After much blood sweat and tears I've figured this out.
SHARED_LIBRARY
only. You can build your static library in your original project using the following code in your Andriod.xml:
include $(CLEAR_VARS)
LOCAL_CFLAGS := -O0
LOCAL_MODULE := LibraryToBeUsedInsideSharedLib
LOCAL_SRC_FILES := ...
include $(BUILD_STATIC_LIBRARY) // This builds a "Static Object" here:
// /Project/obj/local/armeabi/libLibraryToBeUsedInsideSharedLib.a
include $(CLEAR_VARS)
LOCAL_MODULE := LibraryCalledFromJava
LOCAL_SRC_FILES := ...
LOCAL_STATIC_LIBRARIES := LibraryToBeUsedInsideSharedLib
include $(BUILD_SHARED_LIBRARY)
LOCAL_STATIC_LIBRARIES
includes the Static Library in your Shared Library. In your Java code you can now call this:
System.loadLibrary("LibraryCalledFromJava");
You should be able to call any native methods located inside the LibraryToBeUsedInsideSharedLib
library from any point in your java code.
You can export the libLibraryToBeUsedInsideSharedLib.a
file and use it in other projects by adding this to the external project's Android.xml:
include $(CLEAR_VARS)
LOCAL_MODULE := LibraryToBeUsedInsideSharedLib
LOCAL_LDLIBS := -llog/
LOCAL_SRC_FILES := $(MY_PREBUILT_LIB_DIR)/libLibraryToBeUsedInsideSharedLib.a
include $(PREBUILT_STATIC_LIBRARY)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With