How can i build shared and static library with the same name? - android

I want to build shared and static library with the same name but i have an error:
Android NDK: But this module was already defined by path/to/Android.mk
Android.mk:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := j
LOCAL_SRC_FILES := j.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := j
LOCAL_SRC_FILES := j.c
include $(BUILD_STATIC_LIBRARY)
I want to publish shared and static library.

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := j_shared
LOCAL_MODULE_FILENAME := libj
LOCAL_SRC_FILES := j.c
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := j
LOCAL_SRC_FILES := j.c
include $(BUILD_STATIC_LIBRARY)
Note that you should explicitly set the lib prefix, but should not provide the .so suffix.

Related

Android JNI native library not packaged to apk

While transforming my project from eclipse to AS.
I got a fatal error which caused by "the lib not found " problem.
My project contains jni codes,compiled by ndk build system and managed by the "Android.mk" file.
After transformed I found that two of the libraries(.so) was not packaged into the apk file while other libraries are packaged correctly.
Here is part of the content of "Android.mk" file,all libraries are packaged but "curl" and "jsqlite".The two files do exist in the right path.Is there any suggestion,thks!!
LOCAL_PATH := $(call my-dir)
MY_LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := skia
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libskia.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := iconv
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libiconv.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_LDLIBS :=
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := jsqlite
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libjsqlite.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_LDLIBS := -lz
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := curl
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libcurl.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_LDLIBS :=
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := gdal
LOCAL_SRC_FILES := libs/$(TARGET_ARCH_ABI)/libgdal.so
LOCAL_SHARED_LIBRARIES := jsqlite
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
LOCAL_EXPORT_LDLIBS := -lz
include $(PREBUILT_SHARED_LIBRARY)

How to use shared library without header files in Android?

I have a shared lib (ex. libcrypto.so), but don't have header files.
Can I use this lib in other module?
I tried to use LOCAL_EXPORT_C_INCLUDES to export header files, but It doesn't work.
This is prebuilt module:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libcrypto
LOCAL_SRC_FILES := prebuilt/$(TARGET_ARCH_ABI)/libcrypto.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
And, this is module used libcrypto:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES += test.c # include "openssl/crypto.h"
LOCAL_C_INCLUDES += $(crypto_PATH)/include
LOCAL_SHARED_LIBRARIES += libcrypto
LOCAL_MODULE := libtest
LOCAL_MODULE_TAGS := optional
LOCAL_ARM_MODE := arm
LOCAL_PRELINK_MODULE := false
include $(BUILD_SHARED_LIBRARY)
This is libcrypto module (boringssl):
LOCAL_PATH := $(call my-dir)
# Target shared library
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE := libcrypto
LOCAL_EXPORT_C_INCLUDE_DIRS := $(LOCAL_PATH)/src/include
LOCAL_ADDITIONAL_DEPENDENCIES := $(LOCAL_PATH)/Android.mk $(LOCAL_PATH)/crypto-sources.mk
LOCAL_SDK_VERSION := 9
LOCAL_CFLAGS += -fvisibility=hidden -DBORINGSSL_SHARED_LIBRARY -DBORINGSSL_IMPLEMENTATION -DOPENSSL_SMALL -Wno-unused-parameter
# sha256-armv4.S does not compile with clang.
LOCAL_CLANG_ASFLAGS_arm += -no-integrated-as
LOCAL_CLANG_ASFLAGS_arm64 += -march=armv8-a+crypto
include $(LOCAL_PATH)/crypto-sources.mk
include $(BUILD_SHARED_LIBRARY)
EDIT: Added boringssl module. I actually used #include "openssl/crypto.h" in test.c
Usually, you find crypto.h under include/openssl. So, in your case, either change the #include statement in test.c
#include "openssl/crypto.h"
Or (less recommended), edit
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include/openssl

Adding a compiler flag to only one file in Android.mk

I have an Android.mk file that has a number of files for which LOCAL_CFLAGS get applied to them. I would like to apply a different flag to only one of the files out of the many. How can this be accomplished?
I searched the internet from the Android perspective, but didn't find a whole lot. Considering the following example I would like to apply flag TEST3 to file test3.c only. I looked at Per-file CPPFLAGS in Android.mk, but I couldn't find anything as far as how to use PRIVATE_CPPFLAGS to one file. Any ideas?
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
include $(BUILD_SHARED_LIBRARY)
The supported way to achieve your goal is to use a separate static library for C/CPP files that need different parameters. In this particular case, the fix would be as easy as
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := test3
LOCAL_SRC_FILES := test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3 -DTEST3
include $(BUILD_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
LOCAL_WHOLE_STATIC_LIBRARIES := test3
include $(BUILD_SHARED_LIBRARY)
There is another approach, similar to one I forged a while ago
LOCAL_PATH := $(call my-dir)
TARGET-process-src-files-tags += $(call add-src-files-target-cflags, $(LOCAL_TEST3_SRC_FILES), $(LOCAL_TEST3_CFLAGS))
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
LOCAL_TEST3_SRC_FILES := test3.c
LOCAL_TEST3_CFLAGS := -DTEST3
include $(BUILD_SHARED_LIBRARY)
If -Dtest3 would do, you can use another hack:
LOCAL_PATH := $(call my-dir)
get-src-file-target-cflags = $(LOCAL_SRC_FILES_TARGET_CFLAGS.$1) -D$(basename $1)_DEFINE
include $(CLEAR_VARS)
LOCAL_MODULE := test
LOCAL_SRC_FILES := test1.c test2.c test3.c
LOCAL_CFLAGS := -DTEST1_2_AND_3
include $(BUILD_SHARED_LIBRARY)
See more in How to dynamically get the current compiler target file name in Android.mk's LOCAL_CFLAGS?.

including header files android jni

I am trying to use pjsip on android.. after ./configure-android and make dep and make commands, static libraries were generated 13 of them.
I used the include folder which is copied inside jni folder. The content of android.mk in jni folder is as follows:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libpj-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpj-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjlib-util-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjlib-util-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjmedia-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../voiper/pjsip_lib/libpjmedia-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjmedia-audiodev-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjmedia-audiodev-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjmedia-codec-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjmedia-codec-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjmedia-videodev-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjmedia-videodev-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjnath-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjnath-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsdp-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsdp-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsip-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsip-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsip-simple-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsip-simple-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsip-ua-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsip-ua-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsua2-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsua2-arm-unknown-linux-androideabi.a
LOCAL_MODULE := libpjsua-arm-unknown-linux-androideabi
LOCAL_SRC_FILES := ../pjsip_lib/libpjsua-arm-unknown-linux-androideabi.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := dummy_pjsip_jni_module
LOCAL_SRC_FILES := dummy_pjsua.c
LOCAL_EXPORT_C_INCLUDES += ./include
LOCAL_C_INCLUDES += ./include
LOCAL_STATIC_LIBRARIES := libpj-arm-unknown-linux-androideabi libpjlib-util-arm-unknown-linux-androideabi libpjmedia-arm-unknown-linux-androideabi libpjmedia-audiodev-arm-unknown-linux-androideabi libpjmedia-codec-arm-unknown-linux-androideabi libpjmedia-videodev-arm-unknown-linux-androideabi libpjnath-arm-unknown-linux-androideabi libpjsdp-arm-unknown-linux-androideabi libpjsip-arm-unknown-linux-androideabi libpjsip-simple-arm-unknown-linux-androideabi libpjsip-ua-arm-unknown-linux-androideabi libpjsua2-arm-unknown-linux-androideabi libpjsua-arm-unknown-linux-androideabi
include $(BUILD_SHARED_LIBRARY)
I wrote a simple one line c file that only includes a header file inside include folder. Then i run ndk-build from the project directory and get no such file for the header file which is present inside the include folder. There is no application.mk.. What am i missing?
EDIT
the '.' represents the current directory.. "include" folder is inside jni folder
There are several issues here:
1) Each prebuilt static library should have its own module definition, i.e. your Android.mk should look like:
include $(CLEAR_VARS)
LOCAL_MODULE := libpj
LOCAL_SRC_FILES := libpj-arm-unknown-linux-androideabi.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := libpjlib-util
LOCAL_SRC_FILES := libpjlib-util-arm-unknown-linux-androideabi.a
include $(PREBUILT_STATIC_LIBRARY)
... etc
Note that the module name doesn't have to be exactly the same as the file path, which makes things a little clearer for the rest.
2) That '.' will refer, at build time, to the current directory where you invoke ndk-build, you need to use $(LOCAL_PATH)/include instead to refer to the right include path (assuming your Android.mk is under jni/ too). I.e.:
include $(CLEAR_VARS)
LOCAL_MODULE := dummy_pjsip_jni_module
LOCAL_SRC_FILES := dummy_pjsua.c
LOCAL_EXPORT_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_C_INCLUDES += $(LOCAL_PATH)/include
LOCAL_STATIC_LIBRARIES := libpj libpjlib-util libpjmedia libpjmedia-audiodev \
libpjmedia-codec libpjmedia-videodev libpjnath \
libpjsdp libpjsip libpjsip-simple libpjsip-ua \
libpjsua2 libpjsua
include $(BUILD_SHARED_LIBRARY)

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

Categories

Resources