Searching on several projects, I found this line on their android.mk $(call all-proto-files-under, $(src_proto)), and I tried to use this like that:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := my_test
src_proto := $(LOCAL_PATH)/proto
LOCAL_CPP_EXTENSION := .cxx .cpp .cc
LOCAL_CPPFLAGS += -std=c++11
LOCAL_SRC_FILES := main.cc \
$(call all-proto-files-under, $(src_proto))
# print the source files
$(warning $(LOCAL_SRC_FILES))
# print only main.cc
$(warning $(LOCAL_SRC_FILES))
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include \
$(LOCAL_PATH)/proto
# for logging
LOCAL_LDLIBS += -llog
# for native asset manager
LOCAL_LDLIBS += -landroid
include $(BUILD_SHARED_LIBRARY)
But it doesn't work, the warning prints nothing, and the second warning prints only main.cc, the line $(call all-proto-files-under, $(src_proto)) does nothing. I would like to know how can I use protobuf with android ndk.
I don't know how to solve it with the all-proto-files-under function specifically, but if you want to add all source files in a directory you can do that in the following way:
PROTOBUF_FILES := $(wildcard $(LOCAL_PATH)/proto/*.cc)
LOCAL_SRC_FILES += $(PROTOBUF_FILES:$(LOCAL_PATH)/%=%)
I suppose you could simplify that into a oneliner if you wanted to. It's also possible to add all source files in all subdirectories under a given directory if you need that:
PROTOBUF_FILES := $(wildcard $(LOCAL_PATH)/proto/**/*.cc)
When I built protobuf myself, I just took the corresponding Android.mk file from the AOSP git and removed all the stuff I didn't need.
Related
I am trying to link my exe w/ a pre-built '.o' object file ( consisting of a binary resource ).
the object file is located under '$(LOCAL_PATH)/dependency'.
Using a FileSys monitor, I can see that during compilation the Build system deletes the pre-built '.o' file.
using 'LOCAL_OBJECTS += 'dependency/prebuilt.o' doesn't help.
How can I prevent Android build system from deleting my pre-built '.o' file ?
A snap of the makefile used
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := MyTestApp
LOCAL_SRC_FILES := main.cpp
LOCAL_C_INCLUDES :=
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS :=
LOCAL_CPPFLAGS :=
LOCAL_LDFLAGS := -Wl,--format=binary -Wl,Resources/MyBinRsrc -Wl,--output=dependency/MyBinRsrc.o -Wl,--format=elf,dependency/MyBinRsrc.o -Wl,--format=default
COMMON_SRC_FILES := $(LOCAL_SRC_FILES)
LOCAL_OBJECTS += dependency/MyBinRsrc.o
include $(BUILD_EXECUTABLE)
I try to set up an Android NDK build based on CMake scripts, which dynamically create the required Android make files. While I can't use the JNI folder structure I split the build process in several separated make scripts:
1st Create root Android.mk file located in project root:
#ANDROID ROOT MAKEFILE
LOCAL_PATH := D:/binrev/repository/bar
include $(CLEAR_VARS)
MY_LOCAL_CFLAGS := -DDEBUG
include D:/binrev/repository/bar/src/Android.mk
2nd Create source Android.mk file in project source folder and perform module build:
$(info "[INFO] Source Makefile invoked")
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_SRC_FILES := bar.cpp
ifeq (debug,"debug")
MY_LOCAL_CFLAGS := -DDEBUG
endif
ifeq (false,true)
LOCAL_ARM_MODE := arm
endif
LOCAL_EXPORT_C_INCLUDES := D:/binrev/repository/bar/include
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_STATIC_LIBRARIES += foo
ifeq (OFF, ON)
include $(BUILD_SHARED_LIBRARY)
else
include $(BUILD_STATIC_LIBRARY)
endif
Basicly this mechanism works and I could compile my sources, but I fail if I try to include a Prebuild of a library. I tried the following ways to include a pre-build
of a static library (with modified source/include definitions):
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_SRC_FILES := lib/android/$(TARGET_ARCH_ABI)/libfoo.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
1st Prebuild definition in source Android.mk file
2nd Call import-module mechanism and add Prebuild Android.mk file to prebuild-lib
3rd Prebuild definition in root Android.mk file
[Edit:] Here is the snipped of the call-import test which also fail:
$(info "[INFO] Source Makefile invoked")
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_SRC_FILES := bar.cpp
ifeq (debug,"debug")
MY_LOCAL_CFLAGS := -DDEBUG
endif
ifeq (false,true)
LOCAL_ARM_MODE := arm
endif
LOCAL_EXPORT_C_INCLUDES := D:/binrev/repository/bar/include
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_STATIC_LIBRARIES += foo
ifeq (ON, ON)
include $(BUILD_SHARED_LIBRARY)
else
include $(BUILD_STATIC_LIBRARY)
endif
$(call import-module, external-deps/foo)
In each case the Script with the prebuild-definition is invoked, but the prebuild
is not performed. When my NDK build has been compleded, the prebuild library and
objects are not copied to my obj folder. It seems to me that the prebuild is
completely ignored. But the path to prebuild sources are correct, otherwise the
compile fails with missing file error.
You could get the complete source of this test implementation here:
[Test projects][1]https://sourceforge.net/projects/binrevengine/files/publications/
Hint: The bar project is the project which tries to prebuild the foo project.
The foo project contains the prebuild sources.
The added tests projects could be build by your own using MinGW64 with GCC 4.7/4.8 in handshake with CMake and pre installed NDK (using r8e).
I completly get lost and running out of ideas ...
Thanks for any help.
The Android build system will not build a static library without it being used by a shared library. Just create a dummy shared library that has your static library as a dependency and voila:
include $(CLEAR_VARS)
LOCAL_MODULE := dummy
LOCAL_PATH := $(LOCAL_PATH)
LOCAL_SRC_FILES := dummy.c
LOCAL_STATIC_LIBRARIES := foo
include $(BUILD_SHARED_LIBRARY)
To exclude possible sources of defects I've reduced the Android make file to simplest case without using CMake generator of those files:
LOCAL_PATH := D:/binrev/repository/bar
include $(CLEAR_VARS)
LOCAL_MODULE := foo-prebuilt
LOCAL_SRC_FILES := external-deps/foo/lib/android/$(TARGET_ARCH_ABI)/libfoo.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := bar
LOCAL_C_INCLUDES:= D:/binrev/repository/bar/include
LOCAL_C_INCLUDES+= D:/binrev/repository/bar/external-deps/foo/include
LOCAL_SRC_FILES := src/bar.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
LOCAL_SHARED_LIBRARIES := foo-prebuilt
include $(BUILD_SHARED_LIBRARY)
and:
LOCAL_PATH := D:/binrev/repository/foo
include $(CLEAR_VARS)
LOCAL_MODULE := foo
LOCAL_C_INCLUDES:= D:/binrev/repository/foo/include
LOCAL_SRC_FILES := src/foo.cpp
LOCAL_LDLIBS := -llog
LOCAL_LDLIBS += -landroid
include $(BUILD_STATIC_LIBRARY)
The failure still exists. The prebuild of the foo library is not executed. I also excluded MinGW64 as possible source of defect, if I try to build the project with Windows command line it results in same issue. The shared library is build, but the prebuild is not executed.
I checked my sources and scripts multiple times, but can't find any failure.
Any ideas what could be wrong or missing?
Trying to build a static NDK library using Android's ADT Eclipse tool chain. However, whenever I build with BUILD_STATIC_LIBRARY, no output is produced: I get the message
make: Nothing to be done for `all'."
Any recommendations?
LOCAL_PATH := $(call my-dir)
STL_PATH := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/include"
PLATFORM_INCLUDE := "C:/Android/ndk/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include"
APP_STL := gnustl_static
include $(CLEAR_VARS)
LOCAL_MODULE := libCore
LOCAL_CPPFLAGS += -std=c++11 -fexceptions -D_OS_ANDROID
LOCAL_LDLIBS := -lGLESv2 -lEGL -lstdc++
LOCAL_C_INCLUDES += $(LOCAL_PATH)/Headers
...
LOCAL_SRC_FILES += Source/Engine/Game.cpp
...
include $(BUILD_STATIC_LIBRARY)
Here is the content of Android.mk file of two-libs sample project from Android NDK.
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-first
LOCAL_SRC_FILES := first.c
include $(BUILD_STATIC_LIBRARY)
# second lib, which will depend on and include the first one
#
include $(CLEAR_VARS)
LOCAL_MODULE := libtwolib-second
LOCAL_SRC_FILES := second.c
LOCAL_STATIC_LIBRARIES := libtwolib-first
include $(BUILD_SHARED_LIBRARY)
You may try building the static library as part of another shared library as shown in the example.
I just did a ndk-build on the two-libs sample project and i could see the .a file along with .so in obj\local\armeabi directory.
Edit:
By default, ndk-build will only build shared libraries and executables, and the modules they depend on. To force a build specify libCore in APP_MODULES as follows.
APP_MODULES := libCore
or in command line as
ndk-build APP_MODULES=libCore
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!
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.
I'm trying to build an Android project using the ndk, but I have run into some troubles.
Here's the Android.mk file that works:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_CFLAGS := -Werror
LOCAL_SRC_FILES := main.cpp, Screen.cpp, ScreenManager.cpp
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Is there a way that allows me to specify all the *.cpp files in the directory, without listing them manually under LOCAL_SRC_FILES?
So far I tried using LOCAL_SRC_FILES = $(wildcard *.cpp), but it did now work, it seems that no files get selected.
You could try something like this...
FILE_LIST := $(wildcard $(LOCAL_PATH)/[DIRECTORY]/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
... Change [DIRECTORY] to the actual directory of the files. If they are in the same directory as your .mk file then remove that part. Create the FILE_LIST variable to find all of the .cpp files under the [DIRECTORY] directory. Then use it in the file listing. The LOCAL_SRC_FILES line will then remove the LOCAL_PATH from the listing.
I've been using this script for my Android.mk saved me so much time!
#traverse all the directory and subdirectory
define walk
$(wildcard $(1)) $(foreach e, $(wildcard $(1)/*), $(call walk, $(e)))
endef
#find all the file recursively under jni/
ALLFILES = $(call walk, $(LOCAL_PATH))
FILE_LIST := $(filter %.cpp, $(ALLFILES))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
Here is the gist
How about like this:
LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/,,$(wildcard $(LOCAL_PATH)/*.cpp))
If you'd be afraid that expansion of * contains $(LOCAL_PATH)/, it might be OK:
LOCAL_SRC_FILES := $(subst $(LOCAL_PATH)/./,,$(wildcard $(LOCAL_PATH)/./*.cpp))
Using this:
LOCAL_SRC_FILES += $($(wildcard $(LOCAL_PATH)/*.cpp):$(LOCAL_PATH)/%=%)