Android NDK module that is dependant on another module

Issue

I have an android project written using the Android NDK. Within the project, I am using two prebuilt shared libraries(GpkgSDK and spatialite) and building two more shared libraries(WFSHelpers and com_example_gpkgviewer_jni_WKTConverter). The only library that interacts with the Java level of the application is the com_example_gpkgviewer_jni_WKTConverter library.

The dependencies between the libraries are shown below:

  • WFSHelpers is dependent on GpkgSDK and spatialite
  • com_example_gpkgviewer_jni_WKTConverter is dependent on WFSHelpers

The issue that I am having is that when I try to run ndk-build, I am getting a lot of undefined references when attempting to build the com_example_gpkgviewer_jni_WKTConverter library. The other libraries are successfully built. The way I would normally resolve these undefined references is by including the following in my com_example_gpkgviewer_jni_WKTConverter module definition :

LOCAL_SHARED_LIBRARY : WFSHelpers

I am unsure as to whether I would also need to include the libraries that the WFSHelpers is dependent on like so:

LOCAL_SHARED_LIBRARY : WFSHelpers GpkgSDK spatialite

I have also tried them in a different order like so but it does not seem to resolve my issue:

  • LOCAL_SHARED_LIBRARY : GpkgSDK spatialite WFSHelpers

My Application.mk is included below:

NDK_TOOLCHAIN_VERSION : 4.8
# APP_STL : stlport_shared  --> does not seem to contain C++11 features
APP_STL : gnustl_shared

# Enable c++11 extentions in source code
APP_CPPFLAGS + -stdc++11
APP_CPPFLAGS + -frtti 
APP_CPPFLAGS + -fexceptions

APP_MODULES : GpkgSDK spatialite WFSHelpers com_example_gpkgviewer_jni_WKTConverter

APP_ABI : armeabi armeabi-v7a

My Android.mk is shown below:

LOCAL_PATH : $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE : GpkgSDK
LOCAL_SRC_FILES : libMP.so
LOCAL_EXPORT_C_INCLUDES : \
$(LOCAL_PATH)/include \
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE : spatialite
LOCAL_SRC_FILES : spatialamal/prebuilt/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_EXPORT_C_INCLUDES :     spatialamal/headers/spatialite \
                            spatialamal/headers
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    : WFSHelpers
LOCAL_LDLIBS : -L$(SYSROOT)/usr/lib -llog
LOCAL_SRC_FILES : \
GPKGReader/Debug.h \
GPKGReader/DLLExport.h \
GPKGReader/DBQueryResult.cpp \
GPKGReader/GeoPackageDB.cpp \
GPKGReader/GPKGReader.cpp \
GPKGReader/order32.h \
GPKGReader/SpecDefinitions.h \
GPKGReader/WKBGenericGeometry.cpp \
GPKGReader/WKBLineString.cpp \
GPKGReader/WKBMultiLineString.cpp \
GPKGReader/WKBMultiPolygon.cpp \
GPKGReader/WKBPoint.cpp \
GPKGReader/WKBPolygon.cpp \
GPKGDataLayer/GPKGDataLayer.cpp
LOCAL_SHARED_LIBRARIES : GpkgSDK spatialite
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_LDLIBS : -L$(SYSROOT)/usr/lib -llog
LOCAL_MODULE    : com_example_gpkgviewer_jni_WKTConverter
LOCAL_SRC_FILES : com_example_gpkgviewer_jni_WKTConverter.cpp
LOCAL_SHARED_LIBRARY : WFSHelpers GpkgSDK spatialite
include $(BUILD_SHARED_LIBRARY)

An example of the undefined reference errors that I am getting is shown below:

[armeabi] SharedLibrary  : libcom_example_gpkgviewer_jni_WKTConverter.so
jni/com_example_gpkgviewer_jni_WKTConverter.cpp:59: error: undefined reference
o 'WKBGenericGeometry::readInt32(unsigned char, unsigned char*, unsigned int)'
collect2.exe: error: ld returned 1 exit status
make.exe: *** [obj/local/armeabi/libcom_example_gpkgviewer_jni_WKTConverter.so]
Error 1

The line of code within com_example_gpkgviewer_jni_WKTConverter.cpp that causes this error is shown below:

*id_arg  WKBGenericGeometry::readInt32(byte_order, &(bytes[4]), length - 4);

Any suggestions on how I can resolve the dependencies.

Solution

After attempting to get this to build for ages, I have finally been able to get it to work. Although, I am not quite sure what the differences are, so anyone who can shed a light on this is welcome to do so. My issue was that I had dependencies on other libraries. The documentation states the following:

LOCAL_SHARED_LIBRARIES
The list of shared libraries modules this module depends on at runtime. This is necessary at link time and to embed the corresponding information in the generated file.

LOCAL_LDLIBS
The list of additional linker flags to be used when building your shared library or executable. This is useful to pass the name of specific system libraries with the ‘-l’ prefix. For example, the following will tell the linker to generate a module that links to /system/lib/libz.so at load time:

LOCAL_LDLIBS : -lz

See STABLE-APIS for the list of exposed system libraries you can linked against with this NDK release.
NOTE: This is ignored for static libraries, and ndk-build will print a warning if you define it in such a module.

Thus in my Android.mk file, I had to use LOCAL_LDLIBS instead of LOCAL_SHARED_LIBRARIES to indicate the dependencies.

My new Android.mk is as shown below:

LOCAL_PATH : $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE : GpkgSDK
LOCAL_SRC_FILES : libMP.so
LOCAL_EXPORT_C_INCLUDES : \
$(LOCAL_PATH)/include \
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE : spatialite
LOCAL_SRC_FILES : spatialamal/prebuilt/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_EXPORT_C_INCLUDES :     spatialamal/headers/spatialite \
                            spatialamal/headers
include $(PREBUILT_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    : WFSHelpers
LOCAL_SRC_FILES : \
GPKGReader/Debug.h \
GPKGReader/DLLExport.h \
GPKGReader/DBQueryResult.cpp \
GPKGReader/GeoPackageDB.cpp \
GPKGReader/GPKGReader.cpp \
GPKGReader/order32.h \
GPKGReader/SpecDefinitions.h \
GPKGReader/WKBGenericGeometry.cpp \
GPKGReader/WKBLineString.cpp \
GPKGReader/WKBMultiLineString.cpp \
GPKGReader/WKBMultiPolygon.cpp \
GPKGReader/WKBPoint.cpp \
GPKGReader/WKBPolygon.cpp \
GPKGDataLayer/GPKGDataLayer.cpp
LOCAL_LDLIBS : libs/$(TARGET_ARCH_ABI)/libGpkgSDK.so
LOCAL_LDLIBS + libs/$(TARGET_ARCH_ABI)/libspatialite.so
LOCAL_LDLIBS + -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    : com_example_gpkgviewer_jni_WKTConverter
LOCAL_SRC_FILES : com_example_gpkgviewer_jni_WKTConverter.cpp
LOCAL_LDLIBS : libs/$(TARGET_ARCH_ABI)/libWFSHelpers.so
LOCAL_LDLIBS + libs/$(TARGET_ARCH_ABI)/libGpkgSDK.so
include $(BUILD_SHARED_LIBRARY)

I shall leave this answer open for a while as I am not entirely sure what the difference between LOCAL_LDLIBS and LOCAL_SHARED_LIBRARIES is. If anyone can provide me with an explanation, please do. If not, I shall mark this answer as accepted after giving it some time. Thanks !

Answered By – kushaldsouza

Leave a Comment