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?
Related
I'm using ndk-build to build a set of shared library(.so) for my android project. I configured and made the source code of C++ library(gdal-2.2.2).
everything was ok.("./configure & make & make install" was successful).
So i created my jni folder like this documentation.
but when I'm trying to use ndk-build on windows, I get a lot of error like "Undefined refrence to somthing".
I've spent a lot of time on this project. Is there someone to help me?
Thanks.
Update
I used configure like this on ubuntu 16.04:
./configure --prefix=/home/mahdi/Desktop/build/ --with-spatialite=yes --with-spatialite-soname=libspatialite.so --host=i686-linux-android CXXFLAGS="-D_GLIBCXX_USE_CXX11_ABI=0" LIBS="-lsupc++ -lstdc++"
After make & make install step I created JNI. this is my directory.
jniwrap
jni
gdal
Android.mk
Application.mk
gdal_wrap.cpp
gdalconst_wrap.c
gnm_wrap.cpp
libgdal.a
ogr_wrap.cpp
osr_wrap.cpp
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/gdal/include
LOCAL_SRC_FILES := libgdal.a
LOCAL_EXPORT_LDLIBS := -lz
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := gdaljni
LOCAL_SRC_FILES := gdal_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := gdalconstjni
LOCAL_SRC_FILES := gdalconst_wrap.c
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := ogrjni
LOCAL_SRC_FILES := ogr_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := osrjni
LOCAL_SRC_FILES := osr_wrap.cpp
LOCAL_STATIC_LIBRARIES := gdal
include $(BUILD_SHARED_LIBRARY)
Aplication.mk
APP_STL := gnustl_shared
APP_CFLAGS := Android.mk
APP_ABI := x86
APP_PLATFORM := android-14
Then I used android-ndk-r16b in windows-x86_64 but I faced with these errors like this picture:
There was a lot of "undefined reference error" that i can't show here.
Note: for making gdal Java Binding I used swig and jdk7 on my ubuntu 16.04.
When you build libgdal.a on your ubuntu machine, you must have sqlite3, which resolves #include "sqlite3.h".
These include files are enough for a static library, but to create libgdaljni.so you also need libsqlite3.a. You can cross-compile it for Android it yourself on the same ubuntu machine, but it is probably OK to get prebuilt library e.g. from https://github.com/couchbase/couchbase-lite-java-native/tree/master/vendor/sqlite/libs/android.
Copy this file (for appropriate ABI) to the same directory, and modify your Android.mk accordingly:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sqlite3
LOCAL_SRC_FILES := libsqlite3.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/gdal/include
LOCAL_SRC_FILES := libgdal.a
LOCAL_EXPORT_LDLIBS := -lz
LOCAL_STATIC_LIBRARIES := sqlite3
include $(PREBUILT_STATIC_LIBRARY)
*continued without changes*
If you still have "undefined reference error", this could mean that some other libraries should be added.
I simply want use a static library for an NDK project. The library works with build systems like automake, but in Android.mk I have a problem with the source files. For some reason, directives don't work over different header files and I get the following error:
error: 'myname' does not name a type
It seems that the directive defined in file1.hpp is not visible in file2.hpp which includes file1.hpp (as I said, this problem does not occur with other build systems like automake.
What am I doing wrong here?
Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE := libstat
LOCAL_LDLIBS := -lm
LOCAL_SRC_FILES := /home/dir/libstat.a
LOCAL_EXPORT_C_INCLUDES := /home/dir/src
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_CFLAGS := -lm -ldl /home/dir/src
LOCAL_LDFLAGS := -L/home/dir/
LOCAL_C_INCLUDES += /home/dir/src
LOCAL_SRC_FILES := hello-jni.cpp
LOCAL_LDLIBS := -ggdb
LOCAL_STATIC_LIBRARIES := libstat
include $(BUILD_SHARED_LIBRARY)
src includes all header and source files. Any hint would be appreciated.
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
As specified in this answer, I downloaded libjpeg 8d from github and placed it in a folder {ANDROID_APP}/jni/libjpeg. This library has it's own Android.mk, so I tried to include it at the end of my {ANDROID_APP}/jni/Android.mk this way :
include $(LOCAL_PATH)/libjpeg/Android.mk
Note : I'm using the latest version of android NDK (r8c)
After running ndk-build, I still get this error :
ANDROID_APP/jni/libfoo/foo_analysis.c:36:21: fatal error: jpeglib.h: No such file or directory
This is the structure of my global Android.mk :
LOCAL_PATH := $(call my-dir)
# libFoo
include $(CLEAR_VARS)
LOCAL_MODULE := libfoo
LOCAL_MODULE_FILENAME := libfoo
LOCAL_SRC_FILES := libfoo/foo.c libfoo/foo_analysis.c libfoo/foo_extract.c
LOCAL_STATIC_LIBRARIES := libbmp # declared previously but not shown in this example
LOCAL_CFLAGS = ${FLAGS}
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libfoo
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
# libBar
include $(CLEAR_VARS)
LOCAL_MODULE := libbar
LOCAL_MODULE_FILENAME := libbar
LOCAL_SRC_FILES := bar/bar.c
LOCAL_STATIC_LIBRARIES := libfoo
LOCAL_CFLAGS = ${FLAGS}
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/bar
LOCAL_EXPORT_LDLIBS := -llog
include $(BUILD_STATIC_LIBRARY)
# callbar
LOCAL_MODULE := libcallbar
LOCAL_MODULE_FILENAME := libcallbar
LOCAL_SRC_FILES := com_androidapp_nativeC_callbar.c
LOCAL_STATIC_LIBRARIES := libbar
LOCAL_CFLAGS = ${FLAGS}
include $(BUILD_SHARED_LIBRARY)
#libjpeg
include $(LOCAL_PATH)/libjpeg/Android.mk
I tried to use LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/libfoo $(LOCAL_PATH)/libjpeg
and LOCAL_C_INCLUDES := $(LOCAL_PATH)/libjpeg in the libFoo module, but I still get the same error.
Just looked at Android.mk in jpeg8d-master folder and seems to be it has nothing.
I was trying to compile library directly according to STANDALONE-TOOLCHAIN.HTML
I do next: $export NDKROOT=/home/alex/tools/android-ndk-r8c (where is your NDK) $export SYSROOT=$NDKROOT/platforms/android-9/arch-arm (or any other android platform)
but files from jpeg8d-master have windows \r symbols and I deleted config.guess, config.sub, depcomp than use $automake -a command. And replace ltmain.sh from glib-2.34.0
than $./configure CC="$NDKROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-gcc --sysroot=$SYSROOT" --host=arm-linux-androideabi $make
next try prebuilts feature NDK - How to use a generated .so library in another project and NDK/PREBUILTS.HTML
I use this AES library (in C language),
http://gladman.plushost.co.uk/oldsite/AES/aes-src-16-04-07.zip
I follow this page and use the commds mentioned to compile libaes.a,
http://forums.devshed.com/c-programming-42/aes-encrypt-decrypt-in-c-687368.html
gcc -c -O2 -fomit-frame-pointer aescrypt.c aeskey.c aestab.c aes_modes.c
ar rcs libaes.a *.o
I can compile and run my program using the libaes.a without problem.
However if use ndk-build to compile my program (indeed modified a little),
I always get this error message and fail to compile...
"...member aes_modes.o in archive is not an object..."
what's wrong with that file?
Or what's wrong with the process?
my Android.mk :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libaes
LOCAL_SRC_FILES := libaes.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
LOCAL_C_INCLUDES :=\
/android-ndk-r6b/platforms/android-8/arch-arm/usr/include\
/android-ndk-r6b/samples/hello-jni/jni/libaes
LOCAL_WHOLE_STATIC_LIBRARIES := libaes
include $(BUILD_SHARED_LIBRARY)
I've a feeling that you compiled aes with native tools (x86) and using it as prebuild library. I've tried this Android.mk and it works:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := aes
LOCAL_SRC_FILES := aes/aescrypt.c aes/aeskey.c aes/aestab.c aes/aes_modes.c
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := jni/aes
LOCAL_STATIC_LIBRARIES := aes
LOCAL_MODULE := hello-jni
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
Just unzip aes-src-16-04-07.zip into jni/aes. I think it is better to use ndk build system because it can set all necesary options.
And hello-jni.c (just in case):
#include <aes.h>
void test() {
aes_init();
}