Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include a prebuilt static library in Android NDK's Android.mk file correctly?

I'm stuck getting my libraries included inside the Android NDK build.

The libraries are correctly compiled and work fine when creating a dummy cpp file and building everything with a direct g++ command in the shell.

The current Android.mk file doesn't work and throws an error that the corresponding header files (that are part of the .a files) can't be found.

How do I include prebuilt static libraries correctly?

My Android.mk file looks like this:

LOCAL_PATH := $(call my-dir)


# V8 Base

include $(CLEAR_VARS)

LOCAL_MODULE          := v8_base
LOCAL_MODULE_FILENAME := v8_base_static
LOCAL_SRC_FILES := ../lib/libv8_base.a

include $(PREBUILT_STATIC_LIBRARY)


# V8 Nosnapshot

include $(CLEAR_VARS)

LOCAL_MODULE          := v8_nosnapshot
LOCAL_MODULE_FILENAME := v8_nosnapshot_static
LOCAL_SRC_FILES := ../lib/libv8_nosnapshot.a

include $(PREBUILT_STATIC_LIBRARY)


# V8GL Runtime

include $(CLEAR_VARS)

LOCAL_MODULE    := v8gl-runtime
LOCAL_SRC_FILES := main.c ../src/v8gl/v8gl.cpp
LOCAL_CPPFLAGS  := -D__ANDROID__
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue v8_base v8_nosnapshot

# LOCAL_EXPORT_CPPFLAGS := -D__ANDROID__

include $(BUILD_SHARED_LIBRARY)

$(call import-module,android/native_app_glue)

The compiler output is the following, which makes sense, but only shows me that there is no single .a file included and I don't know why:

Compile++ thumb  : v8gl-runtime <= v8gl.cpp
(... g++ call)
In file included from jni/../src/v8gl/v8gl.cpp:6:
jni/../src/v8gl/v8gl.h:5:16: error: v8.h: No such file or directory

SOLUTION with absolute path

Thanks to the hint of @alex-cohn I found out that the includes were falsely pointed out. So I decided to use an environment variable that is set before calling ndk-build that contains the absolute path. That fixes the problem with the includes.

So the last Module, where the actual inclusion is done, is now looking like:

ADK_PATH=/var/whatever/to/your/project/root_not_jni

include $(CLEAR_VARS)
LOCAL_MODULE    := v8gl-runtime
LOCAL_SRC_FILES := main.c ../src/v8gl/v8gl.cpp

LOCAL_C_INCLUDES:= $(ADK_PATH)/external/v8/include

LOCAL_CPPFLAGS  := -D__ANDROID__
LOCAL_LDLIBS    := -llog -landroid -lEGL -lGLESv1_CM
LOCAL_STATIC_LIBRARIES := android_native_app_glue v8_base v8_nosnapshot

Now it also shows that the libraries are included, because they are compiled afterwards - for whatever reason.

SOLUTION with relative path

All include paths are relative to the project root folder and not the jni folder. That means it will land as a compiler -I flag as something like this:

LOCAL_C_INCLUDES := $(LOCAL_PATH)/../file_in_project.root

# resulting g++ flag:
-Ijni/../file_in_project.root

So there's a difference between the relative include paths and the LOCAL_SRC_FILES, which are relative to the jni folder!

like image 818
Christoph Martens Avatar asked Sep 11 '12 21:09

Christoph Martens


1 Answers

You probably have file v8.h in directory ../include or somewhere else...

You should add line

LOCAL_C_INCLUDES = $(LOCAL_PATH)/../include

Note that unlike LOCAL_SRC_FILES where you don't need $(LOCAL_PATH), here you must put the full paths of all directories where the necessary .h files are.

like image 179
Alex Cohn Avatar answered Sep 28 '22 17:09

Alex Cohn