0

I'm having a similar problem to what this person was having:

Android NDK import-module / code reuse

But for some reason when I implement the solution that they listed, it doesn't work for me. The only difference I have in my project is that I am using 1 makefile for the entire project. So, the modules look like this: (... = more files or paths)

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := mod1
LOCAL_C_INCLUDES := $(LOCAL_PATH)/to/c/includes \
... \
LOCAL_SRC_FILES := stringTester.cpp \
... \
include$(BUILD_STATIC_LIBRARY)


include $(CLEAR_VARS)

LOCAL_MODULE := mod2
LOCAL_C_INCLUDES := $(LOCAL_PATH)/to/c/includes \
... \
LOCAL_SRC_FILES := localSourceFile.cpp \
... \
include$(BUILD_STATIC_LIBRARY)

and so on for 2 more modules, then in the final module:

include $(CLEAR_VARS)

LOCAL_MODULE := sharedMod
LOCAL_C_INCLUDES := $(LOCAL_PATH)/to/c/includes \
... \
LOCAL_STATIC_LIBRARIES := mod1 mod2 mod3 mod4
... \
include $(BUILD_SHARED_LIBRARY)

and in my java file:

package com.string.test

public class javaTest{

public native String returnAString();

static{
    System.LoadLibrary("sharedMod");
  }
}

The problem I'm running into is that I cannot call any methods from the static libraries, such as any method in stringTester.cpp, but if I move that file to be included in the module that creates the shared library, everything works as it should, the Java wrappers are correct and methods return the data I expect. The only reason why I don't move all the files into a shared library are they are separate and evolving projects, such as our physics engine and so forth.

I have also tried separating the modules with each module having its own makefile, and also having a cpp file in the shared library that called the methods in the static libraries but I would get a lot of unresolved reference failures. One other solution I have tried was to use LOCAL_WHOLE_STATIC_LIBRARIES to load the static libraries but that gave me unresolved reference errors as well.

Any help or suggestions are appreciated. I'm at my wits end, and at a roadblock.

Community
  • 1
  • 1
PirateDave
  • 163
  • 1
  • 3
  • 11
  • Are you trying to load a static library and use that from your Java code ? You can't do that - it needs to be a shared lib. (Though you can compile different parts as a static library which you use to produce a final shared library, though care must be taken with linking order and linker flags, LOCAL_WHOLE_STATIC_LIBRARIES is likely the way to go with that approach but I don't know the Android.mk system well enough to give specific advice) – nos Jan 23 '13 at 19:18
  • I'm loading the shared library in Java, but I was under the impression that I could use functions from a static library if they were included into the shared library (like I was trying to do in the last module). If I can't do that then I will try to compile all the sources into a shared library because I will need to access the methods from the other modules. – PirateDave Jan 23 '13 at 19:36
  • If your shared library links in your static libs, yes you can do that. However something in the shared library have to reference the functions in your static library otherwise they just get pruned away. If you want to include all the code of the static libs, you should to use LOCAL_WHOLE_STATIC_LIBRARIES, so it might help if you try that and post output/code/etc related to errors you get from that. (along with other relevant info, e.g. the NDK version) – nos Jan 23 '13 at 19:41
  • I'm currently using ndk-r8c. When I use LOCAL_WHOLE_STATIC_LIBRARIES I get errors like this: 'stringTester.cpp ln 355: undefined reference to unzReadCurrentFile' which is the same thing that happens if I try using a makefile with all the sources in one shared library. I know this relates to the fact that we use zlib in our module, which we include with 'LOCAL_LDLIBS := -lz' and it works when I keep the modules separate. I'm going to try a make that includes the -lz flag in each module with LOCAL_WHOLE_STATIC_LIBRARIES in the shared module and will let you know the result. *Failed* same output – PirateDave Jan 23 '13 at 20:06
  • unzReadCurrentFile is normally part of libminizip , not libz so, you might need to add -lminizip – nos Jan 23 '13 at 22:00
  • I might have to find another way to add that because it gave me an error saying it could not find -lminizip, so it may not be in this version of the ndk – PirateDave Jan 23 '13 at 22:28

1 Answers1

1

The answer is definitely to use:

LOCAL_WHOLE_STATIC_LIBRARIES := mod1 mod2 mod3 mod4

I was running into other problems with including other libraries but when reduced to a simple test case, using the above macro worked. Thanks to nos for the help!!

PirateDave
  • 163
  • 1
  • 3
  • 11