Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ndk build library outside main project source tree

Tags:

android-ndk

I wants to build a 3rd party library avahi using ndk. avahi has android port already (with valid Android.mk).

What I have done: I have successfully created a project and copied all the source code in to jni/ folder and run ndk-build. It builds libavahi.so

What I wants to do: Instead of copying all the source code into jni/ folder, I'd like to store it in a folder outside project source tree. What shall I do? I have looked into NDK DOCUMENTATION/Import Module but nothing is similar to my case.

Newbie in ndk, and any suggestion is welcomed.

like image 483
Lily Avatar asked Sep 23 '12 11:09

Lily


People also ask

How do I run ndk build from the command line?

The ndk-build file lives in the top level the NDK installation directory. To run it from the command line, invoke it while in or under your application project directory. For example: In this example, <project> points to your project’s root directory, and <ndk> is the directory where you installed the NDK.

What is ndk-build file in NDK?

The ndk-build file is a shell script introduced in Android NDK r4. Its purpose is to invoke the right NDK build script.

What do I need to use ndk-build?

You need GNU Make 4 to use ndk-build or the NDK in general. The NDK includes its own copy of GNU Make and will use that unless you've set the $GNUMAKE environment variable to point to an unsuitable make. In NDK r18 and newer, ndk-build can generate a JSON compilation database.

Can ndk-build generate a JSON compilation database?

In NDK r18 and newer, ndk-build can generate a JSON compilation database. You can either use ndk-build compile_commands.json to generate the database without building your code, or ndk-build GEN_COMPILE_COMMANDS_DB=true if you want to build and generate the database as a side-effect. Last updated 2020-08-17 UTC.


1 Answers

You are right, this is not the case of Import Module. The way you reference the avihi library from your native code will still be as LOCAL_SHARED_LIBRARIES (see NDK sample module-exports). But in your jni/Android.mk file, you may use include command to another file. This command is very similar to #include statement in C. This file needs not to be inside your project tree. Taking the same sample, here is how it can work:

Original Android.mk from samples/module-exports/jni:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_CFLAGS := -DFOO=2
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := zoo
LOCAL_SRC_FILES := zoo/zoo.c
LOCAL_SHARED_LIBRARIES := bar
include $(BUILD_SHARED_LIBRARY)

The changed file will look as follows:

ZOO_LOCAL_PATH := $(call my-dir)
include ~/projects/bar/jni/Android.mk
LOCAL_PATH := $(ZOO_LOCAL_PATH)

include $(CLEAR_VARS)
LOCAL_MODULE := zoo
LOCAL_SRC_FILES := zoo/zoo.c
LOCAL_SHARED_LIBRARIES := bar
include $(BUILD_SHARED_LIBRARY)

And the external bar/jni/Android.mk as follows:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo/foo.c
LOCAL_CFLAGS := -DFOO=2
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)

Now, both files bar.c and foo.c may be kept outside the tree of the zoo project!

like image 107
Alex Cohn Avatar answered Oct 06 '22 00:10

Alex Cohn