Android NDK import-module / code reuse

2020-02-25 08:09发布

问题:

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.

回答1:

After much blood sweat and tears I've figured this out.

  • Android JNI loads its binary from a SHARED_LIBRARY only.
  • JNI will try and link native calls to the appropriate method signatures/stubs from the loaded Shared Library (it will not look inside linked shared libraries).
  • You can create a Static Library with these methods and build it into the Shared Library used by your application.

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)