0

In Microsoft Visual Studio 2010, it is possible to create a Solution with multiple projects and set dependencies between projects. I'm trying to figure out if the same thing can be done using Eclipse via the NDK. More specifically, I want to know if it is possible to create C source files in an ordinary Android project that can reference C header files in an Android library project.

For example:

Android library project: Sockets
Ordinary Android project: Socket_Server 

Sockets contains all the C header/source files that are needed to do socket I/O.
Socket_Server contains test code that makes calls to the functions that are defined in Sockets library project.  This test code requires a header file that contains the function declaration of all API calls.

I already set the library dependencies between the projects via:

Properties > Android > Library > Add

In Socket_Server, there's a file called SocketTestServer.cpp. It contains test code, but makes API calls to the library project, Sockets, and does so by #include "Nv_Socket.h", which is not part of Socket_Server:

jni/SocketTestServer.cpp:1:23: fatal error: Nv_Socket.h: No such file or directory
compilation terminated.
Android Noob
  • 3,271
  • 4
  • 34
  • 60
  • It's still not clear: "`Sockets` contains all the C header/source files that are needed to do socket I/O." ... "In `Socket_Server`, there's a file called SocketTestServer.cpp. ". SO where is only java code and where is only C? – Maxim Shoustin Nov 15 '12 at 23:28
  • I actually have 3 projects, Socket_Client, Socket_Server, and Sockets. In Socket_Client and Socket_Server, I have a duplicate copy of all the C source code from the Sockets JNI folder. I can run Socket_Server and Socket_Client apps on my Nexus 7 just fine. The issue is that I don't want to have a copy of the same C source files in the Client/Server projects when the only difference between them is a test.cpp file that makes API calls to the same source code base. I'm trying to create project dependencies like its being done in Visual Studio 2010. – Android Noob Nov 15 '12 at 23:37
  • So..if I have a test.cpp file that #include some header files from Sockets, how do I set up that project dependency in Eclipse? – Android Noob Nov 15 '12 at 23:41
  • I fixed my response. Why do you want to implement it as NDK? you have good java API and use jar files to prevent duplicates. I use `NDK` only for performance reasons (read/write to/from files, GET/POST from url....) – Maxim Shoustin Nov 15 '12 at 23:41
  • `So..if I have a test.cpp file that #include some header files from Sockets, how do I set up that project dependency in Eclipse?` From my exp. you cant integrate 2 Android projects by the same header – Maxim Shoustin Nov 15 '12 at 23:42
  • I'm actually porting my co-workers code (Crypto-engine related) from Windows to Linux/Android and his code is heavily reliant on his own socket code written in C. We're just doing incremental work for now starting with sockets and then multi-threading. – Android Noob Nov 15 '12 at 23:42
  • Since you use sockets its not the problem that one side will be `C` and other one - Java – Maxim Shoustin Nov 15 '12 at 23:44
  • So you're saying that I can't #include a header file from another project? – Android Noob Nov 15 '12 at 23:48

2 Answers2

0

I think I solved it. If you make a .so file, put it in your /jni folder and then reference it in your .mk file, you should have no problems compiling.

.mk file:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Turn on C++ Exception handling
LOCAL_CPP_FEATURES += exceptions
LOCAL_CPPFLAGS += -fexceptions

# Load LogCat utility
LOCAL_LDLIBS := -llog

# C flags
LOCAL_CFLAGS    += -Wno-psabi -fpermissive

# Module name                  
LOCAL_MODULE    := ServerSockets

# C Header includes
LOCAL_C_INCLUDES := $(LOCAL_PATH)

# Source files
LOCAL_SRC_FILES := libSockets.so 

include $(PREBUILT_SHARED_LIBRARY)
Android Noob
  • 3,271
  • 4
  • 34
  • 60
0

See http://www.kandroid.org/ndk/docs/IMPORT-MODULE.html and http://www.kandroid.org/ndk/docs/PREBUILTS.html. The trick is not to have to copy the binary from one project to another! Also, using LOCAL_EXPORT_C_INCLUDES you can guarantee that the up-to-date version is used with

#include "Nv_Socket.h"
Alex Cohn
  • 56,089
  • 9
  • 113
  • 307