Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDK compiling multiple libraries

I am using native code in my android app. Firstly I was only using one library. So everything worked fine. But now I have to integrate one more library into it. I've no idea what should be the ideal structure of the jni folder of my project (as in where to place the entire code, etc.). I found a work around. I created two folders inside jni .i.e library1 and library2. Again created a jni folder inside both the folders and placed respective code in the folders.

I got it to compile. Both .so files are being generated, but I am unable to use it in my application. I cant load the library using System.loadLibrary("library1.so"); Also tried providing full path. But failed

Also I have no idea what to write inside the parent jni folder's Android.mk file.

Current structure: project_folder -> jni -> library1 -> jni -> "source code" an Android.mk is written here project_folder -> jni -> library2 -> jni -> "source code" an Android.mk is written here

UPDATE #1 :

Gdbserver      : [arm-linux-androideabi-4.6] libs/armeabi/gdbserver
Gdbsetup       : libs/armeabi/gdb.setup
make: *** No rule to make target `jni/zap/jni/zap/zap/error.c', needed by `obj/local/armeabi/objs-debug/zap/jni/zap/zap/error.o'.  Stop.

I am not using Application.mk. This is my Android.mk:

TOP_PATH := $(call my-dir)

# Build library 1
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/zap
LOCAL_MODULE := zap
LOCAL_C_INCLUDES := $(LOCAL_PATH)/zap
LOCAL_SRC_FILES := $(LOCAL_PATH)/zap/error.c \
$(LOCAL_PATH)/zap/hello-jni.c \
$(LOCAL_PATH)/zap/zap.c \
$(LOCAL_PATH)/zap/zapd.c \
$(LOCAL_PATH)/zap/zaplib.c 
include $(BUILD_SHARED_LIBRARY)
like image 411
Darshan Bidkar Avatar asked Jan 25 '13 06:01

Darshan Bidkar


1 Answers

The best structure I've found is to use the jni/ folder for ndk-build makefiles only, and keep the source outside in their own folders. This is easy to add to existing projects without restructuring your tree under jni.

However, you do have to be careful about how you handle the LOCAL_PATH variable and use of $(call my-dir). Here's a working example:

  • MyProject/
    • library1/
      • source1.cpp
    • library2/
      • source2.cpp
    • jni/
      • Android.mk
      • Application.mk

Android.mk:

# TOP_PATH refers to the project root dir (MyProject)
TOP_PATH := $(call my-dir)/..

# Build library 1
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/library1
LOCAL_MODULE := library1
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := source1.cpp
include $(BUILD_SHARED_LIBRARY)

# Build library 2
include $(CLEAR_VARS)
LOCAL_PATH := $(TOP_PATH)/library2
LOCAL_MODULE := library2
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := source2.cpp
include $(BUILD_SHARED_LIBRARY)

You can optionally split out the sections in Android.mk to their own makefiles.

like image 121
safety Avatar answered Oct 12 '22 23:10

safety