building a prebuilt static library with ndk-build all - android

I have a problem with an ndk-build script that builds a static library.
The problem is that this script gets included by our application's larger build script, which gets called with ndk-build all
The build script for the static library looks like this:
# LoadBalancing-cpp
LOCAL_PATH := $(call my-dir)
all_static_libraries = common-cpp-static-prebuilt \
photon-cpp-static-prebuilt
lib_suffix := ${APP_OPTIM}_android_${APP_ABI}
lib_loadbalancing_cpp_static_name := loadbalancing-cpp-prebuilt-static_${lib_suffix}
include $(CLEAR_VARS)
LOCAL_MODULE := loadbalancing-cpp-static-prebuilt
LOCAL_SRC_FILES := lib$(lib_loadbalancing_cpp_static_name).a
LOCAL_STATIC_LIBRARIES := $(all_static_libraries)
include $(PREBUILT_STATIC_LIBRARY)
$(call import-module,common-cpp-prebuilt)
$(call import-module,photon-cpp-prebuilt)
The problem is, building a static library requires the LOCAL_SRC_FILES to point to a single value (the path to the library), however when called with ndk-build all in this case, it will contain multiple values (since lib_suffix will point to all available architectures).
Is there a way to build this file using ndk-build all ?

You can use TARGET_ARCH variable which is managed by ndk-build:
lib_suffix := $(APP_OPTIM)_android_$(TARGET_ARCH)
... and so on.
Essentially, ndk-build will "call" your Android.mk file multiple times, each time setting the TARGET_ARCH variable differently.

Related

Android - item in LOCAL_STATIC_LIBRARIES not being built

I'm having trouble getting the dependency module to build while building a static library in AOSP. Call this library A, it has a dependency on another static lib B.
A's Android.mk looks like this:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := <files...>
LOCAL_STATIC_LIBRARIES := B
include $(BUILD_STATIC_LIBRARY)
Individually B builds fine (mma).
Problem is when I build A, B is not being built. Instead I see this at output:
Export includes file: <...>/B/Android.mk -- out/<...>/STATIC_LIBRARIES/B_intermediates/export_includes
Can someone explain what does this line mean and, why is it not trying use B's Android.mk to properly build B?
I understand it's not ideal to package a static lib inside another, but here I'm more courious about why is it the build system not running through B's makefile, when it's clearly a dependency?
Thank you!
The build system ignores lots of irrelevant statements, LOCAL_STATIC_LIBRARIES being one of examples. They don't write every entry in LOCAL_STATIC_LIBRARIES as a dependency for libA.a. Instead, they interpret the Android.mk files to produce make rules for all targets, and if a dependency happens to show up, it will eventually get built, too.
Therefore, the easiest workaround for you would be to add a dummy shared library to your Android.mk, like
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := A
LOCAL_SRC_FILES := <files...>
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := dummyA
LOCAL_SRC_FILES := dummy.c
LOCAL_STATIC_LIBRARIES := B
include $(BUILD_SHARED_LIBRARY)
I am not sure if you can drop LOCAL_SRC_FILES completely. In terms of good old plain make files, the above is roughly equivalent to:
all: libA.a libdummyA.so
libdummyA.so: dummy.c libB.a
gcc -o $# dummy.c -lb
Alternatively, you can manually specify the dependency:
$(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libA_intermediates/libA.a: $(PRODUCT_OUT)/obj/STATIC_LIBRARIES/libB_intermediates/libB.a
Re: export_includes message, it is the result of processing LOCAL_EXPORT_C_INCLUDES statement for libB.

Android makefile can't find libs because of extra LOCAL_PATH in path

So I've been scratching my head for quite some time with this: basically, I have two Android makefile, one in my jni folder, and one in another folder that contains my native c++ code.
Thing is, for the following makefile,
LOCAL_PATH := $(call my-dir)
GENERATED_PATH := $(LOCAL_PATH)/../../generated/release/api/Android
############################ Includes ############################
#------------------------------------------------------------------ Assimp
LOCAL_MODULE := Assimp
LOCAL_EXPORT_C_INCLUDES := $(GENERATED_PATH)/assimp/include
LOCAL_SRC_FILES := $(GENERATED_PATH)/assimp/lib/libassimp.a
include $(PREBUILT_STATIC_LIBRARY)
# More Libraries included....
#....
I get the following error:
Android NDK: ERROR:jni/../../../appCommon/Android.mk:Assimp: LOCAL_SRC_FILES points to a missing file
Android NDK: Check that jni/../../../appCommon/jni/../../../appCommon/../../generated/release/api/Android/assimp/lib/libassimp.a exists or that its path is correct
What bugs me is that there's twice the LOCAL_PATH in the path where the ndk searches for the library. I've already read about a few cases like this one (like using the notdir macro) but couldn't find a satisfying solution.
How can I specify correctly (and not manually) the correct directory ?
So it turns out that the problem stems from the PREBUILT_STATIC_LIBRARY script, which looks for the lib in the following path: $(LOCAL_PATH)/$(LOCAL_SRC_FILES)
Thus, a simple workaround that worked is to have separate variables for the directories, like that:
GENERATED_PATH := ../../generated/release/api/Android
GENERATED_INCLUDE_PATH := $(LOCAL_PATH)/$(GENERATED_PATH)
and then use them like that:
LOCAL_MODULE := Assimp
LOCAL_EXPORT_C_INCLUDES := $(GENERATED_INCLUDE_PATH)/assimp/include
LOCAL_SRC_FILES := $(GENERATED_PATH)/assimp/lib/libassimp.a
include $(PREBUILT_STATIC_LIBRARY)

Use external static library in application (NDK)

I have recently started working with NDK and am stuck with a problem. I basically want to use an external static library (.so) into my application. Here's what I've tried till now.
Create a library
1) Add a method in the Java class with native keyword
public native static void FibNR ();
2) In the terminal navigate to the project folder and run the following command
mkdir jni
javah -jni -classpath bin/classes/ -d jni/ com.example.fibonaccinative.FibLib
3) Refresh project and add a C file corresponding to the generated H file.
4) Create a Android.mk file in the JNI folder and add the following code in it
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE :=STATIC
LOCAL_SRC_FILES := com_example_fibonaccinative_FibLib.c
LOCAL_MODULE := com_example_fibonaccinative_FibLib
include $(BUILD_SHARED_LIBRARY)
5) Build the code using the following command
/Developer/android-ndk-r9b/ndk-build all
The above steps execute successfully and I'm able to view the results of the c code also. Now I want to create a new application and use the generated .so file (com_example_fibonaccinative_FibLib.so) into it. For this I do the following
Use the library
follow steps 1, 2 and 3 from above for the new application
4) Create a Android.mk file in the JNI folder and add the following code in it
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
OPENCV_LIB_TYPE :=STATIC
LOCAL_SRC_FILES := com_example_usingstaticlibrary_LibraryTest.c
LOCAL_MODULE := com_example_usingstaticlibrary_LibraryTest
LOCAL_SHARED_LIBRARIES :=libcom_example_fibonaccinative_FibLib.so
LOCAL_LDLIBS := -L$(SYSROOT)/usr -llog
include $(BUILD_SHARED_LIBRARY)
5) Build the code using the following command
/Developer/android-ndk-r9b/ndk-build all
I'm not sure what to do next. What I think is I need to call the function of the library into com_example_usingstaticlibrary_LibraryTest.c. But doing so gives me an error saying
'com_example_fibonaccinative_FibLib' undeclared
EDIT 1:
1) In project 2 (where I want to use the Prebuilt Shared Library (.so)) I copied the library into the 'jni' folder
2) Changed the Android.mk file with the below text.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcom_example_fibonaccinative_FibLib
LOCAL_SRC_FILES := libcom_example_fibonaccinative_FibLib.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_usingstaticlibrary_LibraryTest
LOCAL_SHARED_LIBRARIES := libcom_example_fibonaccinative_FibLib
LOCAL_SRC_FILES := com_example_usingstaticlibrary_LibraryTest.c
include $(BUILD_SHARED_LIBRARY)
3) Added the .h file from the old project (com_example_fibonaccinative_FibLib.h) into the 'jni' folder
4) Changed the source of com_example_usingstaticlibrary_LibraryTest.c to
#include "com_example_usingstaticlibrary_LibraryTest.h"
#include "com_example_fibonaccinative_FibLib.h"
JNIEXPORT jlong JNICALL Java_com_example_usingstaticlibrary_LibraryTest_callLibraryFunction
(JNIEnv *env, jclass class) {
Java_com_example_fibonaccinative_FibLib_fibNR(env, class, 500l);
return -500l;
}
Clean/Build results into the following error
undefined reference to 'Java_com_example_fibonaccinative_FibLib_fibNR'
Edit 2:
With reference to this I've edited the Android.mk as follows
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_fibonaccinative_FibLib
LOCAL_SRC_FILES := libcom_example_fibonaccinative_FibLib.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_usingstaticlibrary_LibraryTest
LOCAL_SRC_FILES := com_example_usingstaticlibrary_LibraryTest.c
LOCAL_SHARED_LIBRARIES := com_example_fibonaccinative_FibLib
include $(BUILD_SHARED_LIBRARY)
The NDK build command compiles the code without error. When trying to load the library it gives the following error
Exception Ljava/lang/UnsatisfiedLinkError; thrown while initializing Lcom/example/usingstaticlibrary/LibraryTest
When I remove LOCAL_SHARED_LIBRARIES := com_example_fibonaccinative_FibLib it works fine but I cannot use any function of the Prebuilt shared library.
Edit 3:
I tried a few more things including the suggestion by #jcm but nothing worked. I'm now attaching the source code. It has both the projects (Cleaned version to reduce size).
FibonacciNative: Contains the first project.
UsingStaticLibrary: Contains the second project. I intend to use the Prebuilt Shared Library of the first project into the second one.
You say you want to include a precompiled static library .a, but the file you reference is a shared library .so. If you still want to include a static library, here comes the solution and explanation:
LOCAL_SHARED_LIBRARIES :=libcom_example_fibonaccinative_FibLib.so
You're trying to include the static library as a shared one, looks like an accidental slip from using the previous shared library entry as reference :).
Upon that, Android makefiles still requires you to actually make a module for the external library.
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_fibonaccinative_FibLib
LOCAL_SRC_FILES := com_example_fibonaccinative_FibLib.a
include $(PREBUILT_STATIC_LIBRARY)
Then you can simply add it as a dependency:
LOCAL_STATIC_LIBRARIES := com_example_fibonaccinative_FibLib
But if you're actually trying to include an external shared library. You would need to change the above to the following:
include $(CLEAR_VARS)
LOCAL_MODULE := com_example_fibonaccinative_FibLib
LOCAL_SRC_FILES := com_example_fibonaccinative_FibLib.so
include $(PREBUILT_SHARED_LIBRARY)
and include it as a shared library.
LOCAL_SHARED_LIBRARIES := com_example_fibonaccinative_FibLib
Hope I helped!
Disclaimer: I have not actually tried to compile an example, simply referencing my knowledge and quick research.
References:
Android NDK: Static library used is different than precompiled library specified
As you're including libcom_example_fibonaccinative_FibLib.so as LibraryTest's shared library, in your java static method, you should load it before loading LibraryTest:
System.loadlibrary(com_example_fibonaccinative_FibLib);
System.loadLibrary(LibraryTest);
Update:
After checking your code, LibraryTest.java should look like:
package com.kochartech.usingstaticlibrary;
public class LibraryTest {
public native static long callLibraryFunction();
static {
System.loadLibrary("com_kochartech_fibonaccinative_FibLib");
System.loadLibrary("com_kochartech_usingstaticlibrary_LibraryTest");
}
}

creating shared library using prebuilt static library using Android.mk

I am trying to build a shared library using prebuilt static library, the respective Android.mk file is below.
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
#Include kerne headers in a proper way later on
#LOCAL_C_INCLUDES:= $(LOCAL_PATH)/../../../../kernel/include
LOCAL_MODULE := sensors.$(TARGET_BOARD_PLATFORM)
LOCAL_MODULE_TAGS := optional
LOCAL_PRELINK_MODULE := false
LOCAL_MODULE_PATH := $(TARGET_OUT_SHARED_LIBRARIES)/hw
LOCAL_STATIC_LIBRARIES := LibFusion_ARM_cpp
#LOCAL_LDFLAGS := $(LOCAL_PATH)/LibFusion_ARM_cpp.a
LOCAL_SHARED_LIBRARIES := liblog libcutils
#include any shared library dependencies
LOCAL_LDFLAGS := $(LOCAL_PATH)/libimu.a
LOCAL_PRELINK_MODULE := false
LOCAL_SRC_FILES := sensors_u8500.cpp
include $(BUILD_SHARED_LIBRARY)
The files present in the folder is
Android.mk , LibFusion_ARM_cpp.a , libimu.a , MEMSAlgLib_eCompass.h , sensors_u8500.cpp
The error what I am getting while building is below,
**make: *** No rule to make target `out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_cpp_intermediates/LibFusion_ARM_cpp.a', needed by `out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/sensors.montblanc.so'. Stop.**
What is the issue here?
Unable to build the shared library. I mean unable to generate libsensor.so file. facing the above mentioned error. ** make: * No rule to make target
It seems that the build system cannot find LibFusion_ARM_cpp.a. AFAIK, you have to first specify a pre-built library module using PREBUILT_STATIC_LIBRARY with LibFusion_ARM_cpp.a set as LOCAL_SRC_FILES and LOCAL_MODULE as LibFusion_ARM_cpp. This will basically copy the specified static library to the default directory the build system searches through for all of the necessary modules.
I did two things to understand the case:
A. In the above shown make file, I added the below so as to ensure that this .a file gets copied into the build.
include $(CLEAR_VARS)
LOCAL_MODULE := LibFusion_ARM
LOCAL_SRC_FILES := LibFusion_ARM_cpp.a
include $(BUILD_STATIC_LIBRARY)
When I build it using mm -n, command I see this strange statement rm -f ... of the LibFusion_ARM.a. Of course the build fails saying it's not able to locate the STATIC LIB.
mkdir -p out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/
rm -f out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a
echo "target StaticLib: LibFusion_ARM (out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a)"
mkdir -p out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/
.
.
Why does this deletion happens? I am not sure. This is causing the problem. Please provide suggestions.
B. I added the below lines in the make file:
include $(CLEAR_VARS)
LOCAL_MODULE := LibFusion_ARM
LOCAL_SRC_FILES := LibFusion_ARM_cpp.a
include $(PREBUILT_STATIC_LIBRARY)
I see the below error:
make: *** No rule to make target `out/target/product/u8500/obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates/LibFusion_ARM.a', needed by `out/target/product/u8500/obj/SHARED_LIBRARIES/sensors.montblanc_intermediates/LINKED/sensors.montblanc.so'.
Stop.
Clearly in the second case, the file LibFusion_ARM is not getting copied (even obj/STATIC_LIBRARIES/LibFusion_ARM_intermediates directory is not created).
Needless to emphasis, if I manually copy the file LibFusion_ARM_cpp.a as LibFusion_ARM.a then the build goes through.

how to include prebuilt shared libraries in apk with eclipse

I have a shared library libfoo.so and need to use it in my android app.
My first try was to have in Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test.cpp
LOCAL_LDLIBS := -L$(PATH_TO_FOO) -lfoo
include $(BUILD_SHARED_LIBRARY)
in my activity, I have:
statis
{
System.loadLibrary("foo");
}
This builds correctly, however I noticed that created apk doesnt include libfoo.so (also I see it is not copied to libs/armeabi). I guess for that reason I have UnsatisfiedLinkError when executing my app.
I saw in some other posts that I need to add $(PREBUILD_SHARED_LIBRARY), so I add the following to my Android.mk:
include $(CLEAR_VARS)
LOCAL_MODULE:= foo
LOCAL_SRC_FILES := $(FOO_PATH)/libfoo.so
include $(PREBUILD_SHARED_LIBRARY)
But now I am getting the build error:
foo: LOCAL_SRC_FILES points to a missing file.
I am sure that the path is correct. Note that the libfoo.so was having origionally the version number at the end, though I had to remove it (and leave only .so) since ndk-build complained.
What am I doing wrong?
The include appears to be misspelt:
include $(PREBUILD_SHARED_LIBRARY)
should be
include $(PREBUILT_SHARED_LIBRARY)
Found the solution!! LOCAL_SRC_FILES can not have absolute or relative paths, just the filename. The path must be set in LOCAL_PATH.
So in my case, instead of:
LOCAL_SRC_FILES := $(FOO_PATH)/libfoo.so
I have now:
LOCAL_PATH := $(FOO_PATH)
LOCAL_SRC_FILES := libfoo.so
And this works ok.
In eclipse, i add a static library by copying the file in the path project/libs/armeabi/ and rebuild the project after cleaning it. This includes the .so in the apk.

Categories

Resources