use prebuild libcurl.a with my android project - android

I have libcurl.a compiled for armeabi, armeabi-v7a and x86. I want to include it in my android project and successfully execute this simple code, The problem is I'm not quite sure what to write in android.mk and application.mk to make it happen!
I have written this so far: Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_Hazem_test_Hazem.c
LOCAL_MODULE:= com_Hazem_test_Hazem
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_ABI := armeabi armeabi-v7a x86
kindly, help me figure out the contents of those two files.

You have to include these lines after LOCAL_PATH := $(call my-dir) and before the part that uses the libcurl
include $(CLEAR_VARS)
LOCAL_MODULE := curl_static
LOCAL_SRC_FILES := <path>/libcurl.a
LOCAL_EXPORT_C_INCLUDES := <path>/curl/include
include $(PREBUILT_STATIC_LIBRARY)
and to use:
LOCAL_STATIC_LIBRARIES += curl_static

Related

ndk-build undefined reference errror

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.

Android NDK: Is it possible to switch APP_STL for multiple libraries?

I have a Application.mk and a Android.mk file.
Application.mk looks like
NDK_TOOLCHAIN_VERSION := 4.8
APP_PLATFORM := android-9
APP_STL := c++_shared
APP_ABI := armeabi-v7a
and my Android.mk looks like
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo1
LOCAL_SRC_FILES := foo1.cpp
# ... some other stuff ...
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := foo2
LOCAL_SRC_FILES := foo2.cpp
# ... some other stuff ...
include $(BUILD_SHARED_LIBRARY)
Now I want that library libfoo1 use c++_shared for APP_STL and libfoo2 use c++_static for APP_STL. (I know, normally that should not be made relation between app_stl values with static and shared build android). Is there a easy way without building a extra project and import the library to the other project?
Yes, it is possible. Do the following changes:
# Application.mk
APP_STL := none
This way you disable internal logic of choosing STL implementation of NDK build system. Now, specify manually which C++ stdlib implementation you want link with:
# Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := foo1
LOCAL_SRC_FILES := foo1.cpp
LOCAL_SHARED_LIBRARIES := c++_shared
# ... some other stuff ...
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := foo2
LOCAL_SRC_FILES := foo2.cpp
LOCAL_STATIC_LIBRARIES := c++_static
# ... some other stuff ...
include $(BUILD_SHARED_LIBRARY)
$(call import-module,cxx-stl/llvm-libc++)

How to build an shared library and call it in other ndk program

I want to build an shared library. To build it, I need to call another shared library. Here is what I did:
1.Create one Android project,named "BuildLib",and add a new folder "jni" under the project directory. Contents of jni folder:
jni-->Android.mk
-->Application.mk
-->add.cpp
-->add.h add.cpp just do two numbers addition:
add.h:
int add(int a,int b);
add.cpp:
#include "add.h"
int add(int a,int b){
return a+b;}
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := add.cpp
LOCAL_MODULE := add
include $(BUILD_SHARED_LIBRARY)
After build the project,I got libadd.so under directory $(BUILDLIB)/libs/armeabi/.
Create another Android project, named "CallLib". Copy libadd.so and add.h to jni folder, create Android.mk, Application.mk, and call_add.cpp.
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
include $(PREBUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)
LOCAL_SRC_FILES := call_add.cpp
LOCAL_MODULE := native
LOCAL_SHARED_LIBRARIES := add_prebuilt
include $(BUILD_SHARED_LIBRARY)
call_add.cpp:
#include "add.h"
int call_add(){return add(1,2);}
After all above, I build the CallLib project, but got the error:
undefined reference to 'add(int, int)';
I think the libadd.so can not be found, but I don't know how to modify. Does anyone know how I can fix this? Any help will be appreciated.
In your second Android.mk, try replacing the first module with:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := libadd.so
LOCAL_MODULE := add_prebuilt
LOCAL_EXPORT_C_INCLUDES := add.h
include $(PREBUILD_SHARED_LIBRARY)
The LOCAL_EXPORT_C_INCLUDES flag should attach the header information to the add_prebuilt module, so it can be linked with your final library.
Just in case anyone needs it:
A bit hackish way to keep the linker happy:
LOCAL_LDLIBS := -llog
or
LOCAL_LDLIBS := -L$(LOCAL_PATH)/lib -lMyStuff
Less hackish:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := xyz
LOCAL_SRC_FILES += xyz/xyz.c
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY) # this builds libxyz.so
include $(CLEAR_VARS)
LOCAL_MODULE := abc
LOCAL_SHARED_LIBRARIES := xyz # <=== !!! this makes libabc.so dependent on libxyz.so
LOCAL_SRC_FILES := abc/abc.c
#LOCAL_LDLIBS := ...
include $(BUILD_SHARED_LIBRARY) # this builds libabc.so

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.

Unable to find header files - Android NDK

i'm wrapping a native API to Android by NDK.
But when building it don't find the header files.
I have the following structure.
project/jni
Android.mk
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
LOCAL_PATH :=/home/marcos/dev/workspace/rmsdk.native.wraper/jni
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := ndk1
LOCAL_SRC_FILES := native.c DelegateDRMProcessorClient.cpp
LOCAL_STATIC_LIBRARY := adept cryptopenssl dp expat fonts hobbes jpeg mschema png t3 xml zlib
include $(BUILD_SHARED_LIBRARY)
project/jni/prereqs/
Android.mk (Used to call all subdirs Android.mk files)
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
project/jni/prereqs/%lib%/
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE :=dp
LOCAL_SRC_FILES :=libdp.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_STATIC_LIBRARY)
And there's a include folder on each %lib% folder.
When using ndk-build I get a
"/home/marcos/dev/workspace/rmsdk.native.wraper/jni/DelegateDRMProcessorClient.h:18:20: error: dp_all.h: No such file or directory"
Anyone knows how to include these header to be available to the compiler?
I solve it, getting all the headers in a folder and including the following line in the Android.mk
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include-all
This works, but not looks like the best approach.
I'm a bit late to this party, but ran into the same issue and might have an answer for your comment:
"This works, but not looks like the best approach"
There;s a sample in the NDK called "module-exports"
It shows how to construct an Android.mk file which respects header files living in their proper directories and not all dumped into a single include directory.
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)
Years later...
To export the include directory instead of individual files, I use the following:
LOCAL_EXPORT_C_INCLUDE_DIRS := $(MY_DIRECTORY_PATH)
For example, for the above question the export for "foo" would look like:
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/foo
For new people's convenience, I just want to add that move all your header files in folder which is referred by LOCAL_C_INCLUDES := $(LOCAL_PATH) and then save android.mk and restart eclipse. After trying all the above solutions, that worked for me.

Categories

Resources