Linking prebuilt static libraries ndk vs. Android source - android

I have found recently that linking prebuilt static libraries from the ndk-build is fundamentally different than from within the android source tree (mm). Why is this?
// main.cpp
#include <stdio.h>
#include "doubler.hpp"
int main()
{
printf("test a static lib \n");
// library function
doubler *p = new doubler();
delete p;
return 0;
}
Android mk:
LOCAL_PATH := $(call my-dir)
###################### static lib ##################
### prebuilt lib works in NDK but not Android src
#include $(CLEAR_VARS)
#LOCAL_MODULE := doubleIt_prebuilt
#LOCAL_SRC_FILES := $(LOCAL_PATH)/libDoubler.a
#LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
##LOCAL_SRC_FILES := libDoubler.a
#include $(PREBUILT_STATIC_LIBRARY)
###################### test app ##################
include $(CLEAR_VARS)
# binary name
LOCAL_MODULE:= testApp
# c++ file extension
LOCAL_CPP_EXTENSION := .cpp
# src files
LOCAL_SRC_FILES := main.cpp
# include dir
LOCAL_C_INCLUDES := $(LOCAL_PATH)
### this works in Android src, but not in NDK
LOCAL_LDLIBS := -L$(LOCAL_PATH) -lDoubler
### prebuilt lib works in NDK but not Android src
#LOCAL_STATIC_LIBRARIES := doubleIt_prebuilt
#what to build
include $(BUILD_EXECUTABLE)
Ultimately, what is the correct way to link a static lib in the src tree?

Why is this?
ndk-build and the AOSP build system are two entirely different build systems that unfortunately look very similar.
Ultimately, what is the correct way to link a static lib in the src tree?
In the AOSP tree? Prebuilt modules are defined differently. Here are a couple examples:
https://android.googlesource.com/platform/prebuilts/sdk/+/9c011b3a7784803b96dc0f0a840aa9033a0cd62a/tools/Android.mk#291
include $(CLEAR_VARS)
LOCAL_MODULE := libbcc
LOCAL_SRC_FILES := $(HOST_OS)/lib64/$(LOCAL_MODULE)$(HOST_SHLIB_SUFFIX)
LOCAL_MODULE_CLASS := SHARED_LIBRARIES
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_SUFFIX := $(HOST_SHLIB_SUFFIX)
LOCAL_IS_HOST_MODULE := true
LOCAL_MULTILIB := 64
include $(BUILD_PREBUILT)
https://android.googlesource.com/platform/development/+/518e6c3a28cc63fd094c8b255e268650b03fdab5/host/windows/prebuilt/usb/Android.mk
include $(CLEAR_VARS)
LOCAL_IS_HOST_MODULE := true
LOCAL_MODULE := AdbWinApi
LOCAL_MODULE_CLASS := STATIC_LIBRARIES
LOCAL_SRC_FILES_x86 := AdbWinApi.a
LOCAL_MODULE_SUFFIX := .a
LOCAL_MULTILIB := 32
LOCAL_MODULE_HOST_OS := windows
include $(BUILD_PREBUILT)
You then use them the same way you would any other library: LOCAL_STATIC_LIBRARIES := libmyprebuilt. Both of the above examples are for host modules. For a target module simply remove that line.
Note that AOSP's new (still in progress) build system, Soong, does not yet have support for prebuilt modules. These can only be defined in Android.mk files right now.

Related

Adding library to NDK project - eclipse

Eclipse NDK, NativeActivity project.
I ma trying to add a liquidfun(Box2D) library to my existing project. Unfortunately, no one in internet explain how to precisely do.
I'am stuck after building library using ndk (following this https://google.github.io/liquidfun/Building/html/md__building_android.html), and run sample project. I have totally no idea how to use it in my own project.
My Android.mk, i already using sfml.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := sfml-example
PROJECT_FILES := $(wildcard $(LOCAL_PATH)/CPP/*.cpp)
PROJECT_FILES := $(PROJECT_FILES:$(LOCAL_PATH)/%=%)
LOCAL_SRC_FILES := main.cpp
LOCAL_SRC_FILES += $(PROJECT_FILES)
FILE_LIST := $(wildcard $(LOCAL_PATH)*.cpp)
LOCAL_SHARED_LIBRARIES := sfml-system
LOCAL_SHARED_LIBRARIES += sfml-window
LOCAL_SHARED_LIBRARIES += sfml-graphics
LOCAL_SHARED_LIBRARIES += sfml-audio
LOCAL_SHARED_LIBRARIES += sfml-network
LOCAL_WHOLE_STATIC_LIBRARIES := sfml-main
include $(BUILD_SHARED_LIBRARY)
$(call import-module,sfml)
Thanks in advance.
I have some prebuild libraries in my own project, and i include them like:
include $(CLEAR_VARS)
LOCAL_MODULE := libcurl
LOCAL_SRC_FILES := lib/$(TARGET_ARCH_ABI)/libcurl.a
LOCAL_STATIC_LIBRARIES := libcares libssl
LOCAL_EXPORT_LDLIBS := -lz
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/curl/include
include $(PREBUILT_STATIC_LIBRARY)
this is for prebuilt static library (with .a extension). If you would like to include shared, just change it to BUILD_SHARED_LIBRARY.
This file to include is inside MyProject/jni/lib/{arch} folder (with other libraries) and header files of library are put inside MyProject/jni/curl/include (just like it is visible in LOCAL_EXPORT_C_INCLUDES variable)
the names you pass to LOCAL_STATIC_LIBRARIES must be same as one that are declared in LOCAL_MODULE of other libraries/modules.
also everything you will probably need you can find in NDK docs that are in NDK folder. For prebuilt libraries there is separate section.

No rule to make target error android ndk build sqlite

I am trying to build sqlite using the android NDK to use a sqlite3_create_function but am getting No rule to make target error. make: *** No rule to make target '/fts3-rank.c', needed by '.../obj/local/armeabi/objs/fts3-rank//fts3-rank.o'. Stop. This Android.mk file is based off of the one on this website: http://www.roman10.net/how-to-compile-sqlite-for-android-using-ndk/
#LOCAL_PATH is used to locate source files in the development tree.
#the macro my-dir provided by the build system, indicates the path of the current directory
LOCAL_PATH := $(call my_dir)
#####################################################################
# build sqlite3 #
#####################################################################
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/sqlite-amalgamation-3071700
LOCAL_MODULE := sqlite3
LOCAL_SRC_FILES := $(LOCAL_PATH)/sqlite-amalgamation-3071700/sqlite3.c
include $(BUILD_STATIC_LIBRARY)
#include $(BUILD_SHARED_LIBRARY)
#####################################################################
# build our code #
#####################################################################
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/sqlite-amalgamation-3071700
LOCAL_MODULE := fts3-rank
LOCAL_SRC_FILES := fts3-rank.c
LOCAL_STATIC_LIBRARIES := libsqlite3
#LOCAL_SHARED_LIBRARIES:=libsqlite3
LOCAL_LDLIBS := -llog -lm
#include $(BUILD_SHARED_LIBRARY)
include $(BUILD_EXECUTABLE)
May be There some different reason of this error.
It may be LOCAL_PATH value incorrect so check LOCAL_PATH initialization. Remove any extra spaces in that.
LOCAL_PATH := $(call my-dir)__
Your jni library should be loaded in memory before calling any jni function. Load jni library as follow.
static {
System.loadLibrary("libmy-jni-module");
}
You may refer this discussion on so
I fixed it somehow by trial and error. It was very strange. I guess it's because I was using LOCAL_PATH twice?
I finally got it to build using this Android.mk
#LOCAL_PATH is used to locate source files in the development tree.
#the macro my-dir provided by the build system, indicates the path of the current directory
LOCAL_PATH := $(call my-dir)
#####################################################################
# build sqlite3 #
#####################################################################
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := sqlite-amalgamation-3071700
LOCAL_MODULE := sqlite3
LOCAL_SRC_FILES := sqlite-amalgamation-3071700/sqlite3.c
include $(BUILD_STATIC_LIBRARY)
#include $(BUILD_SHARED_LIBRARY)
#####################################################################
# build our code #
#####################################################################
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/sqlite-amalgamation-3071700
LOCAL_MODULE := fts3-rank
LOCAL_SRC_FILES := fts3-rank.c
LOCAL_STATIC_LIBRARIES := libsqlite3
#LOCAL_SHARED_LIBRARIES:=libsqlite3
LOCAL_LDLIBS := -llog -lm
include $(BUILD_SHARED_LIBRARY)
#need main function to have executable
#include $(BUILD_EXECUTABLE)

Prebuild of static library failed

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?

ADT tool chain not producing output for static libraries

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

Error in linking C++ static library with android ndk(Error: file format not recognized)

I am trying to include static cpp library in android. This library is already compiled(on mac os) and i have its include files.
Here is my Android.mk file
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_MODULE:= utils
LOCAL_SRC_FILES:= libUtils.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/utils
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := sample
LOCAL_SRC_FILES := sample_cpp.cpp
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_STATIC_LIBRARIES := utils
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
and here is Application.mk file
APP_STL := stlport_static
APP_CPPFLAGS = -fexceptions
but whenever it try to compile it using NDK i get the error
(Path of file)/libUtils.a: file not recognized: File format not recognized
collect2: ld returned 1 exit status
From the comments and so on it sounds like you trying to use a non arm version of the library. You should build the library with the ndk. The documentation has even documentation on how to do that.
For example building sigc++ could be like (from a project of mine, where sigc++ resides in the sigc++ subdirectory)
# SIGC++ Library built as static library
LOCAL_MODULE := sigc
LOCAL_PATH = $(CURRENT_DIR)
LOCAL_CPP_EXTENSION := .cc
LOCAL_SRC_FILES := sigc++/signal.cc sigc++/signal_base.cc sigc++/trackable.cc
LOCAL_SRC_FILES += sigc++/functors/slot_base.cc sigc++/adaptors/lambda/lambda.cc
LOCAL_SRC_FILES += sigc++/connection.cc sigc++/functors/slot.cc
LOCAL_C_INCLUDES := sigc++
include $(BUILD_STATIC_LIBRARY)
But you should really read how the compiling linking works. I am afraid building for android with ndk is more low level than using Xcode or Msvc.

Categories

Resources