Need to understand Andorid.mk better - java-native-interface

I have read all the Android.mk intro, and reviewed a number of posts on it here on this forum. And still am not sure how to apply it to what I am trying to do. I am just trying to have access to native library files some of which are a couple of levels below my JNI directory. What I did was to put a symlink in my JNI directory to my source, called tbd, and put the following lines in my Android.mk
LOCAL_PATH := $(call my-dir)
include $(call all-subdir-makefiles)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJNI
LOCAL_C_INCLUDES += $(LOCAL_PATH)/tbd
LOCAL_SRC_FILES := HelloJNI.c manifest.c
LOCAL_STATIC_LIBRARIES := \
libtbd_service \
libtbd_common \
libc
I don't know how to get visibility into some of the files in tbd directory. I am trying to call a function in a file in libc direcoty, which is a couple of levels below my jni folder, and I get the message "file not found". I saw a post, in which someone suggested the following:
FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/**/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/**/**/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
Does this mean I have to do this for every directory in /tbd? There has to be a more efficient way to do this.
Someone else suggested...
LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard folder/**/*.cpp))
I can not even follow this:-( What does % do in this line? Would someone explain what this command is doing please?
#alijandro; thanks. So all I have to find out is how many levels of directory structure I have. Hey do you know about the static or shared libraries? Android.mk documents say "The BUILD_SHARED_LIBRARY is a variable provided by the build system that points to a GNU Makefile script that is in charge of collecting all the
information you defined in LOCAL_XXX variables since the latest 'include $(CLEAR_VARS)' and determine what to build, and how to do it
exactly. " So I don't know the details of how it would do all of this or what it means. Would it perhaps do a recursive search of the mentioned source files??

FILE_LIST := $(wildcard $(LOCAL_PATH)/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/**/*.cpp)
FILE_LIST += $(wildcard $(LOCAL_PATH)/**/**/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
The statements above will get a list of .cpp files in a maximize of three subdirectory. It uses the makefile built-in function for file name operation wildcard.
LOCAL_SRC_FILES += $(patsubst $(LOCAL_PATH)/%, %, $(wildcard folder/**/*.cpp))
Similarly, the statement above uses the built-in function pattern substitute $(patsubst pattern,replacement,text), which means finding the pattern in text and replace them with replacement. The pattern % denotes any characters in makefile.
You could refer here for more information about makefile.

Related

How to link libjingle_peerconnection_so.so to a project and call classes from it?

I try to build my jni class with a single line that tries to call webrtc audio processing.
webrtc::AudioProcessing* apm = webrtc::AudioProcessingBuilder().Create();
I also try to link the libjingle_peerconnection_so.so to my project so that I could use implementation of the audio processing from this common library. My Android.mk looks as follows:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libwebrtc
PATH_TO_WEBRTC := /home/artemy/dev/webrtc_src/src
LOCAL_SRC_FILES := $(LOCAL_PATH)/../../../../libs/$(TARGET_ARCH_ABI)/libjingle_peerconnection_so.so
LOCAL_EXPORT_C_INCLUDES := $(PATH_TO_WEBRTC) $(PATH_TO_WEBRTC)/third_party/abseil-cpp
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := audio_processing_jni
LOCAL_SRC_FILES := audio_proc_jni.cpp
LOCAL_C_INCLUDES := audio_proc_jni.h
LOCAL_LDLIBS := -llog
LOCAL_STATIC_LIBRARIES := libwebrtc
include $(BUILD_SHARED_LIBRARY)
However the project fails to link:
[armeabi-v7a] Compile++ thumb: audio_processing_jni <= audio_proc_jni.cpp
[armeabi-v7a] SharedLibrary : libaudio_processing_jni.so
ap/audio_proc_jni.cpp:11: error: undefined reference to 'webrtc::AudioProcessingBuilder::AudioProcessingBuilder()'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
Is that possible or I need to find another way to webrtc directly from my native code?
Please check how your library is built. I see that the prebuilt libraries e.g. https://github.com/signalapp/libwebrtc-android/tree/master/debug/libs only export the JNI functions:
% nm --dynamic libwebrtc-android/debug/libs/armeabi-v7a/libjingle_peerconnection_so.so | grep \ T\ | grep -v \ Java_
0017a680 T JNI_OnLoad
You must rebuild the library such that the functions that you need are not hidden by the linker.

Why does NDK compiler not recognize a LOCAL_CFLAGS definition?

I have some pre-existing code I'm trying to compile into an NDK library. There's a simply #ifndef I need to execute correctly, but in my Android.mk the var I've defined with LOCAL_CFLAGS is not recognized. It thinks it's a command line option
When I run ndk-build with the NDK_LOG option it all compiles fine until I see this:
[armeabi-v7a] Compile++ thumb: NDKImageProcessor <= NDKImageProcessor.cpp
arm-linux-androideabi-g++: error: unrecognized command line option '-WINONLY=1'
make: *** [obj/local/armeabi-v7a/objs/NDKImageProcessor/NDKImageProcessor.o] Error 1
I'm simply trying to get the following from being included in compilation:
#ifndef WINONLY
#import <CoreGraphics/CGGeometry.h>
#endif
Android.mk is pretty straightforward:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := NDKImageProcessor
LOCAL_SRC_FILES := NDKImageProcessor.cpp
LOCAL_SRC_FILES += ../../../../SharedSrc/Image.cpp
LOCAL_LDLIBS := -llog
LOCAL_CFLAGS := -WINONLY=1
include $(BUILD_SHARED_LIBRARY)
If I simply do not add that LOCAL_CFLAGS line the compiler tries to compile that iOS code, which is a no-go obviously.
From the GCC documentation (it should be the same if you use Clang):
-D name
Predefine name as a macro, with definition 1.
-D name=definition
The contents of definition are tokenized and processed as if they appeared during translation phase three in a ‘#define’ directive. In particular, the definition will be truncated by embedded newline characters.
So to define WINONLY with the value 1 you'd use:
LOCAL_CFLAGS := -DWINONLY=1
or simply
LOCAL_CFLAGS := -DWINONLY

Std in ndk with cpp - vector problems

I am trying to compile a large c++ code for ndk.
I get a bunch of errors. A lot of them related to vectors:
vector<int> myvector --> the '<' gives an error
::iterator cannot be declared
Any ideas on how to get full STL support? I use a lot of libraries, like vector, algorithm, iostream etc
Here is my Android.mk file
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := native
LOCAL_SRC_FILES := vns.cpp note.cpp cscore.cpp hscorecf.cpp hscorecp.cpp scoreinfo.cpp cscore.cpp score.cpp randMusic.cpp input.cpp main.cpp
APP_STL := stlport_shared
#but I have tried system, stlport_static, stlport_shared, or gnustl_static.
LOCAL_LDLIBS := -llog
include $(BUILD_SHARED_LIBRARY)
Got it.
I needed to put APP_STL := stlport_shared in a separate Application.mk file!
Got it. I needed to put APP_STL := stlport_shared in a separate Application.mk file.
Seems like you forgot to #include <vector> header or using namespace std;

can't find libstdc++ when building external modual using gnustl_static

I am adding gnustl_static to a external Android module and when I build it I get the following error.
make: *** No rule to make target `prebuilt/ndk/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/libstdc++.a', needed by `out/target/product/msm8960/obj/SHARED_LIBRARIES/liballjoyn_intermediates/LINKED/liballjoyn.so'. Stop.
when I look under "prebuilt/ndk/android-ndk-r7/sources/cxx-stl/gnu-libstdc++/libs/armeabi-v7a/" I can not find the libstdc++.a there is a fild libsupc++.a
the build system is getting this from the binary.mk file found in the "build/core" folder it states
# LOCAL_NDK_STL_VARIANT is gnustl_static
my_ndk_stl_include_path := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/libs/$(TARGET_CPU_ABI)/include \
$(my_ndk_source_root)/cxx-stl/gnu-libstdc++/include
my_ndk_stl_static_lib := $(my_ndk_source_root)/cxx-stl/gnu-libstdc++/libs/$(TARGET_CPU_ABI)/libstdc++.a
problem is I don't know what to get past this error.
There are many copies of libstdc++ in the prebuilt folder but they are not found in the location listed. The are found in
prebuild/ndk/$(ANDRIOD_NDK_VERSION)/platforms/$(ANDROID_SDK_VERSION)/$(ARCHETECTURE_TYPE)/usr/lib/libstdc++.a
here is a shortened form of my Android.mk file:
LOCAL_PATH := $(call my-dir)
# Rules to build libabc.so
include $(CLEAR_VARS)
LOCAL_CPP_EXTENSION := .cc
LOCAL_SDK_VERSION := 8
LOCAL_NDK_VERSION := 7
LOCAL_NDK_STL_VARIANT := gnustl_static
LOCAL_CFLAGS += \
-std=gnu++0x
LOCAL_C_INCLUDES := \
external/abc/inc \
external/openssl/include
LOCAL_SRC_FILES := \
file1.cc \
file2.cc
LOCAL_SHARED_LIBRARIES := \
libcrypto \
libssl \
liblog
LOCAL_PRELINK_MODULE := false
LOCAL_REQUIRED_MODULES := \
external/openssl/crypto/libcrypto \
external/openssl/ssl/libssl
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libabc
include $(BUILD_SHARED_LIBRARY)
Anyone have any ideas how I can solve the "No rule to make target" build issue?
Solution found:
The simple answer is that I need to change the LOCAL_NDK_VERSION version from 7 to 5 or 6.
The complex answer is that the binary.mk file has not been updated to account for changes that were made when NDK-7 was released.
In NDK-5 and NDK-6 when using gnustl_static the library file that contained everything for STL was in libstdc++.a that was located in the location specified in the binary.mk file. When NDK-7 was released the library name was changed from libstdc++.a to libgnustl_static.a. It does not look like the binary.mk was updated to account for this change.
This is not that surprising since it appears that no projects use gnustl_static. I did a grep accross all of the code in the android repository and the only code I found that was using the LOCAL_NDK_VERSION set to gnustl_static was my own.
You shoudl not use LOCAL_NDK_STL_VARIANT to select STL variant.
You should use APP_STL := gnustl_static in Application.mk file.

Using stl_tree.h on Android NDK results in "No such file or directory"

I'm porting a native library to a NDK application.
When I try to use the stl_tree.h #include, I got the following error.
error: stl_tree.h: No such file or directory
This is my Android.mk file.
LOCAL_PATH := $(call my-dir)
MY_PATH := $(LOCAL_PATH)
include $(call all-subdir-makefiles)
LOCAL_PATH := $(MY_PATH)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog -la -lb -lc -lstdc++ -lgcc #I try to explicit declare the stdc++ and gcc but still doesn't works.
LOCAL_MODULE := rmsdk
LOCAL_SRC_FILES := curlnetprovider.cpp RMServices.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include-all \
$(LOCAL_PATH)/include-all/openssl
LOCAL_STATIC_LIBRARIES := adept cryptopenssl curl dp expat fonts hobbes jpeg mschema png t3 xml zlib
include $(BUILD_SHARED_LIBRARY)
And this is my Application.mk
APP_MODULES := adept cryptopenssl curl dp expat fonts hobbes jpeg mschema png t3 xml zlib rmsdk
APP_STL := stlport_static
My actual problem is, when compiling I get a: '_Rb_tree_increment' is not a member of 'std'
This is why I try to include the stl_tree myself. It appears this lib don't get included.
I can only speak for C++, not for android, but why are you trying to include stl_tree.h directly? You should include either <map> or <set> to get the actual container you want to use. In g++ anyway stl_tree.h is just a private implementation used by map and set.
I solved it using the stlport from NDK 5.
The Android NDK doesn't have the STL (or at least, it didn't the last time I looked at the SDK, which was on Froyo).
The reason why it doesn't have the STL is that by default everything is compiled with exceptions disabled. So be very, very careful when using third-party STL ports; there's a good chance that if anything goes wrong it'll just crash.