How to use Asio (standalone from Boost) in Android NDK? - android

Asio (without Boost) is supposed to be usable with just the headers only right?
By default, Asio is a header-only library. (http://think-async.com)
I understand that internally Asio still depends on Boost.
This is my setup.
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := gatelib
LOCAL_SRC_FILES := gatelib.cpp
LOCAL_C_INCLUDES += /cygdrive/l/asio-1.5.3/include
LOCAL_C_INCLUDES += /cygdrive/l/boost/boost_1_49_0
include $(BUILD_SHARED_LIBRARY)
Application.mk
APP_STL := stlport_static
APP_CFLAGS += -DBOOST_DATE_TIME_NO_LIB -DBOOST_REGEX_NO_LIB
gatelib.cpp
#include <jni.h>
#include <asio.hpp>
#ifdef __cplusplus
extern "C"
{
#endif
// rest of code ...
#ifdef __cplusplus
}
#endif
Build script
#echo on
#set BASHPATH="L:\cygwin\bin\bash"
#set PROJECTDIR="/cygdrive/l/AsioAndroid/AsioDemo"
#set NDKDIR="/cygdrive/l/android-ndk-r8c/ndk-build"
%BASHPATH% --login -c "cd %PROJECTDIR% && %NDKDIR%
#pause:
Output
http://pastebin.com/XiJjvNmp
So basically, what I am trying to achieve is, get Asio working on Android (via NDK). Yes, just Asio itself, not the Asio included with Boost. The problems are presented in the output. I have no clue at all..
PS. There's no Asio tag, so I guess Boost-Asio tag will have to do for now.

Here's what I did.
Application.mk
APP_STL := gnustl_static
APP_CFLAGS += -DBOOST_DATE_TIME_NO_LIB -DBOOST_REGEX_NO_LIB
APP_CPPFLAGS += -fexceptions
The clues and hints were all over Stack Overflow. Once pieced together, it worked!

Related

Android.mk adding LOCAL_CPPFLAGS doesn't work

My source file can not include some header, because of local flag is not defined.
SSS.cpp:
#include <jni.h>
//This code is not defined:
#ifdef WORD
#include "Word.h"
#endif
//...rest of code
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := word
LOCAL_CPPFLAGS := -DWORD
LOCAL_SRC_FILES := SSS.cpp
include $(BUILD_SHARED_LIBRARY)
And one more moment, project build was successful, but I can not run it because of lots of errors in source file (Eclipse c++ editor still can not see my header).
Probably not -WORD but -DWORD ?
-D defines a macro to be used by the preprocessor.
And what this
-std=c++11
belongs to?

Android NDK - No such file or directory compilation terminated - Header files export issue

I am trying to import the TagLib C++ library (http://taglib.github.io/) to Android using the NDK.
I was able to successfully compile the library by putting all the source code under /jni/taglibroot/ as per picture bellow:
and running ndk-build on the /jni folder.
However, I have also created the tag_lib_personal.cpp file bellow (to use the TagLib API):
#include <jni.h>
/*#include <iostream>
#include <iomanip>*/
#include <stdio.h>
#include <taglibroot/taglib/fileref.h>
#include <taglibroot/taglib/tag.h>
#include <taglibroot/taglib/tpropertymap.h>
#ifdef __cplusplus
extern "C"
{
#endif
jstring Java_com_mindtherobot_samples_ndkfoo_NdkFooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
TagLib::FileRef f(argv[i]);
if(!f.isNull() && f.audioProperties()) {
TagLib::AudioProperties *properties = f.audioProperties();
int seconds = properties->length() % 60;
int minutes = (properties->length() - seconds) / 60;
/*cout << "-- AUDIO --" << endl;
cout << "bitrate - " << properties->bitrate() << endl;
cout << "sample rate - " << properties->sampleRate() << endl;
cout << "channels - " << properties->channels() << endl;
cout << "length - " << minutes << ":" << formatSeconds(seconds) << endl;*/
}
return "hello world!";
//return (*env)->NewStringUTF(env, "Hello from native code!");
}
#ifdef __cplusplus
}
#endif
But I cannot get it to compile. When I try to run ndk-build again I get the following error:
[armeabi] Compile++ arm : my_own_source_files <= tag_lib_personal.cpp
In file included from D:/blabla/eclipse-jee-indigo-SR2-win32-x86_64/workspace/Project//jni/tag_lib_personal.cpp:7:0:
D:/blabla/eclipse-jee-indigo-SR2-win32-x86_64/workspace/Project//jni/taglibroot/taglib/fileref.h:29:19: fatal error: tfile.h: No such file or directory
compilation terminated.
make.exe: *** [D:/blabla/eclipse-jee-indigo-SR2-win32-x86_64/workspace/Project//obj/local/armeabi/objs/my_own_source_files/tag_lib_personal.o] Error 1
The error shows it cannot find some .h files from the TagLib library. Here is my Android.mk file though:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := taglib_module
FILE_LIST := $(wildcard $(LOCAL_PATH)/taglibroot/*.cpp) #Based on: http://stackoverflow.com/a/8980441
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/foo
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
# If I run ndk-build only on the above code, it compiles the TagLib library successfully and generates the .so files as expected
include $(CLEAR_VARS)
LOCAL_MODULE := my_own_source_files
LOCAL_SHARED_LIBRARIES := taglib_module
LOCAL_SRC_FILES := tag_lib_personal.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/taglibroot/taglib,\
$(LOCAL_PATH)/taglibroot/taglib/ape,\
$(LOCAL_PATH)/taglibroot/taglib/asf,\
$(LOCAL_PATH)/taglibroot/taglib/flac,\
$(LOCAL_PATH)/taglibroot/taglib/it,\
$(LOCAL_PATH)/taglibroot/taglib/mod,\
$(LOCAL_PATH)/taglibroot/taglib/mp4,\
$(LOCAL_PATH)/taglibroot/taglib/mpc,\
$(LOCAL_PATH)/taglibroot/taglib/mpeg,\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v1,\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v2,\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v2/frames,\
$(LOCAL_PATH)/taglibroot/taglib/ogg,\
$(LOCAL_PATH)/taglibroot/taglib/ogg/flac,\
$(LOCAL_PATH)/taglibroot/taglib/ogg/opus,\
$(LOCAL_PATH)/taglibroot/taglib/ogg/speex,\
$(LOCAL_PATH)/taglibroot/taglib/ogg/vorbis,\
$(LOCAL_PATH)/taglibroot/taglib/riff,\
$(LOCAL_PATH)/taglibroot/taglib/riff/aiff,\
$(LOCAL_PATH)/taglibroot/taglib/riff/wav,\
$(LOCAL_PATH)/taglibroot/taglib/s3m,\
$(LOCAL_PATH)/taglibroot/taglib/toolkit,\
$(LOCAL_PATH)/taglibroot/taglib/trueaudio,\
$(LOCAL_PATH)/taglibroot/taglib/wavpack,\
$(LOCAL_PATH)/taglibroot/taglib/xm
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
As you can see I put every single directory of the TagLib library that has at least one .h file in the LOCAL_EXPORT_C_INCLUDES directive but it for some reason still cannot find tfile.h (which is inside $(LOCAL_PATH)/taglibroot/taglib/toolkit). What am I missing?
You are not missing, you have an extra ;-)
Syntax for LOCAL_C_INCLUDES does not expect these commas. You simply list all directories. Actually, you can use LOCAL_EXPORT_C_INCLUDES instead (your post hints that you probably tried):
LOCAL_C_INCLUDES := $(LOCAL_PATH)/taglibroot/taglib\
$(LOCAL_PATH)/taglibroot/taglib/ape\
$(LOCAL_PATH)/taglibroot/taglib/asf\
$(LOCAL_PATH)/taglibroot/taglib/flac\
$(LOCAL_PATH)/taglibroot/taglib/it\
$(LOCAL_PATH)/taglibroot/taglib/mod\
$(LOCAL_PATH)/taglibroot/taglib/mp4\
$(LOCAL_PATH)/taglibroot/taglib/mpc\
$(LOCAL_PATH)/taglibroot/taglib/mpeg\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v1\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v2\
$(LOCAL_PATH)/taglibroot/taglib/mpeg/id3v2/frames\
$(LOCAL_PATH)/taglibroot/taglib/ogg\
$(LOCAL_PATH)/taglibroot/taglib/ogg/flac\
$(LOCAL_PATH)/taglibroot/taglib/ogg/opus\
$(LOCAL_PATH)/taglibroot/taglib/ogg/speex\
$(LOCAL_PATH)/taglibroot/taglib/ogg/vorbis\
$(LOCAL_PATH)/taglibroot/taglib/riff\
$(LOCAL_PATH)/taglibroot/taglib/riff/aiff\
$(LOCAL_PATH)/taglibroot/taglib/riff/wav\
$(LOCAL_PATH)/taglibroot/taglib/s3m\
$(LOCAL_PATH)/taglibroot/taglib/toolkit\
$(LOCAL_PATH)/taglibroot/taglib/trueaudio\
$(LOCAL_PATH)/taglibroot/taglib/wavpack\
$(LOCAL_PATH)/taglibroot/taglib/xm
Note If you encounter strange issues with ndk-build in the future, first advice is to run ndk-build V=1: it will log all actual compilation commands used by ndk-build, which may often expose the errors or typos in various Android.mk files.
PS Your build will finally fail. When you build taglib, you need also to compile all .cpp files in taglibroot subdirectories! I suggest that you try to build the library the way it was designed to be built, with CMake. The Android patches can be found here: https://code.google.com/p/android-cmake.
You can also try a straightforward approach, but I am not sure it will work:
MY_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := taglib_module
LOCAL_PATH := $(MY_PATH)/taglibroot/taglib
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)\
$(LOCAL_PATH)/ape\
$(LOCAL_PATH)/asf\
$(LOCAL_PATH)/flac\
$(LOCAL_PATH)/it\
$(LOCAL_PATH)/mod\
$(LOCAL_PATH)/mp4\
$(LOCAL_PATH)/mpc\
$(LOCAL_PATH)/mpeg\
$(LOCAL_PATH)/mpeg/id3v1\
$(LOCAL_PATH)/mpeg/id3v2\
$(LOCAL_PATH)/mpeg/id3v2/frames\
$(LOCAL_PATH)/ogg\
$(LOCAL_PATH)/ogg/flac\
$(LOCAL_PATH)/ogg/opus\
$(LOCAL_PATH)/ogg/speex\
$(LOCAL_PATH)/ogg/vorbis\
$(LOCAL_PATH)/riff\
$(LOCAL_PATH)/riff/aiff\
$(LOCAL_PATH)/riff/wav\
$(LOCAL_PATH)/s3m\
$(LOCAL_PATH)/toolkit\
$(LOCAL_PATH)/trueaudio\
$(LOCAL_PATH)/wavpack\
$(LOCAL_PATH)/xm
FILE_LIST := $(foreach dir, $(LOCAL_EXPORT_C_INCLUDES), $(wildcard $(dir)/*.cpp))
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_EXPORT_CFLAGS := -DFOO=1
LOCAL_EXPORT_LDLIBS := -llog
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_PATH := $(MY_PATH)
LOCAL_MODULE := my_own_shared_lib
LOCAL_SHARED_LIBRARIES := taglib_module
LOCAL_SRC_FILES := tag_lib_personal.cpp
LOCAL_ARM_MODE := arm
include $(BUILD_SHARED_LIBRARY)
Please note that in your Java, you will load the two libraries, and order is important:
Runtime.loadLibrary("taglib_module");
Runtime.loadLibrary("my_own_shared_lib");

Type 'AndroidBitmapInfo' could not be resolved

I'm a newbie of Android NDK. I want to try create fast blur effect to bitmap and I found a NDK solution from here: Fast Bitmap Blur For Android SDK answered by #zeh
after I did my configuration, I am not able to run the project and It said "Type 'AndroidBitmapInfo' could not be resolved" in the *.c file.
Could you guys tell me how to fix this problem?
Here is my Android.mk
LOCAL_PATH := $(call my-dir)
# Create BitmapUtils library
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog -ljnigraphics -landroid
LOCAL_MODULE := bitmaputils
LOCAL_SRC_FILES := bitmaputils.c
LOCAL_CFLAGS = -ffast-math -O3 -funroll-loops
include $(BUILD_SHARED_LIBRARY)
Thank you
Just in case: have you correctly included the bitmap header?
#include <android/bitmap.h>
Add the following line to your Application.mk
APP_PLATFORM := android-8
In case you don't use Application.mk, run ndk-build as follows:
ndk-build APP_PLATFORM=android-8
according to ndk samples\bitmap-plasma\jni, you'd better double check mk file, and header file.
Application.mk
# The ARMv7 is significanly faster due to the use of the hardware FPU
APP_ABI := armeabi armeabi-v7a
APP_PLATFORM := android-8
----------------------------------------------------------------
Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := plasma
LOCAL_SRC_FILES := plasma.c
LOCAL_LDLIBS := -lm -llog -ljnigraphics
include $(BUILD_SHARED_LIBRARY)
--------------------------------------------------------
plasma.c
#include <jni.h>
#include <time.h>
#include <android/log.h>
#include <android/bitmap.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
The configuration of CDT indexer needs to enable "Index unused headers ..."
To get there: Project->Properties->C/C++ General->Indexer.
Than rebuild the project
If still this appears than repeat this again.It will be gone :)

Link shared library under Android NDK

I with success compile library LibXtract to shared object libxtract.so and want to use is in second project.
In mention project I try to compile it on simple function:
#include <com_androidnative1_NativeClass.h>
#include <android/log.h>
#include "libxtract.h"
JNIEXPORT void JNICALL Java_com_androidnative1_NativeClass_showText
(JNIEnv *env, jclass clazz)
{
float mean = 0, vector[] = {.1, .2, .3, .4, -.5, -.4, -.3, -.2, -.1}, spectrum[10];
int n, N = 9;
float argf[4];
argf[0] = 8000.f;
argf[1] = XTRACT_MAGNITUDE_SPECTRUM;
argf[2] = 0.f;
argf[3] = 0.f;
xtract[XTRACT_MEAN]((void *)&vector, N, 0, (void *)&mean);
__android_log_print(ANDROID_LOG_DEBUG, "LIbXtract", "Button pushe2");
}
I have flat structure:
jni/com_androidnative1_NativeClass.c
jni/com_androidnative1_NativeClass.hjni/libxtract.h
jni/other *.h files from libxtract interface
jni/Android.mk
jni/Applicatoin.mk
library libxtract.so I put in mainproject/lib folder
my Android.mk file looks like:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/
LOCAL_LDLIBS += -llog
LOCAL_SHARE_LIBRARIES := libxtract
NDK_MODULE_PATH += $(LOCAL_PATH)/../lib/
include $(BUILD_SHARED_LIBRARY)
and I still got error:
Compile thumb : com_androidnative1_NativeClass <= com_androidnative1_NativeClass.c
SharedLibrary : libcom_androidnative1_NativeClass.so./obj/local/armeabi/objs/com_androidnative1_NativeClass/com_androidnative1_Nativ eClass.o: In function `Java_com_androidnative1_NativeClass_showText':
/home/jack/Projects/AndroidNative1/jni/com_androidnative1_NativeClass.c:20: undefined reference to `xtract'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libcom_androidnative1_NativeClass.so] Error 1
Code came form example of LibXtract and under C++ compile without problems, any ideas?
Your Android make file should be ...
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_SRC_FILES := com_androidnative1_NativeClass.c
LOCAL_MODULE := com_androidnative1_NativeClass
LOCAL_LDLIBS += -llog
LOCAL_LDLIBS += $(LIB_PATH) -lxtract
LOCAL_SHARE_LIBRARIES := libxtract
include $(BUILD_SHARED_LIBRARY)
Try this make file in your second project, and you can successfully build your code without having any error.
In the above answer all is right but exept one.
When we want to link lib we must add -L before LOCAL_LDLIBS dir as below.
LIB_PATH := $(LOCAL_PATH)/../lib
LOCAL_LDLIBS += **-L**$(LIB_PATH) -lxtract
Else it will give error as below
cannot open XXX/../lib: Permission denied
You need to tell Android NDK build scripts about your shared library. Check ${NDK}/doc/PREBUILTS.html for instructions how this can be done. They advise to add Android.mk in the same directory where you have your libXtract.so:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libXtract
LOCAL_SRC_FILES := libXtract.so
include $(PREBUILT_SHARED_LIBRARY)
Debugging tip: I guess you are using ndk-build to build your "second project". Try running ndk-build with V=99 (try V=99 ndk-build or ndk-build V=99 - my memory failing). This will show you the the exact failing linking command. You should likely have options -lXtract and -L/path/to/libXtract/library. (Sometimes it is convenient to just copy and paste the linking command to run it manually to find the right options for successful linking, before actually fixing the build settings.)
Update: I now see #codetiger's comment seems to point to a same sort of answer (without mentioning the NDK document which is good reading - so I am not deleting this answer).

android external/stlport include in Android.mk build not successfull

I m trying to build an app with android-froyo source in which I am using skia and stl templates,
I have included
MY_INCLUDES=external/zlib external/jpeg external/freetype/include \
frameworks/base/core/jni/android/graphics external/skia/include/core \
external/libpng external/expat/lib <b>external/stlport/stlport</b>
libstlport_cflags := -D_GNU_SOURCE
libstlport_cppflags := -fuse-cxa-atexit
LOCAL_CPPFLAGS := $(libstlport_cppflags)
include $(BUILD_STATIC_LIBRARY)
I get the following error when i try to build the android source with this app, which i kept at packages/apps:
external/stlport/stlport/stl/_new.h:47:50: error: libstdc++/include/new: No such file or directory
Please guide me to rectify this issue.
Thanks
Mohit
As I understand the file which cannot be found by preprocessor is located in bionic folder.
I had the same issue and I solved it by adding the following line:
LOCAL_C_INCLUDES += bionic
I haven't tried this with Android 2.2 but I'm using Android Kitkat (4.4).
To get the stlport library working with our project we included it in our project's Android.mk as so:
include external/stlport/libstlport.mk
This is assuming that on Froyo, there is a libstlport.mk file to include in your build process. In 4.4, there is also a Android.mk file but that builds other code as well and builds stlport as a static library (which is not what we wanted).
You may need to also add the include directory as well, something like: external/stlport/stlport.
cpp
#include <stdio.h>
// The code
// The set of definitions and includes for STLPort
// They used defined() instead of #ifdef.
#define _STLP_HAS_INCLUDE_NEXT 1
#define _STLP_USE_MALLOC 1
#define _STLP_USE_NO_IOSTREAMS 1
#include <stl/config/_android.h>
#include <map>
#include <string>
int main(void)
{
std::string a = "abc";
printf("%s",a.c_str());
return 0;
}
Android.mk
# A simple test for the minimal standard C++ library
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := test-libstl.cpp
LOCAL_C_INCLUDES += sources/cxx-stl/stlport/stlport
LOCAL_SHARED_LIBRARIES += libstlport
LOCAL_MODULE := test-libstl
include $(BUILD_EXECUTABLE)

Categories

Resources