Adding libpng in android ndk project - android-ndk

I've searched a lot of topics about linking libpng to my android ndk project but I've found right answer for my problem and I hope somebody will help me.
This is hierarchy of my project:
jni
different_cpp_files
different_hpp_files
Android.mk
libpng
different_cpp_files
different_hpp_files
Android.mk
Android.mk in libpng folder:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LS_C=$(subst $(1)/,,$(wildcard $(1)/*.c))
LOCAL_MODULE := png
LOCAL_SRC_FILES := \
$(filter-out example.c pngtest.c,$(call LS_C,$(LOCAL_PATH)))
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
LOCAL_EXPORT_LDLIBS := -lz
include $(BUILD_STATIC_LIBRARY)
I suppose that everything is right here..
Android.mk in jni folder:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LS_CPP=$(subst $(1)/,,$(wildcard $(1)/*.cpp))
LOCAL_MODULE := pacman
LOCAL_CFLAGS := -Wno-psabi
LOCAL_SRC_FILES := $(call LS_CPP,$(LOCAL_PATH))
LOCAL_LDLIBS := -landroid -llog -lEGL -lGLESv1_CM -lOpenSLES
LOCAL_STATIC_LIBRARIES := android_native_app_glue png
include $(BUILD_SHARED_LIBRARY)
$(call import-module,android/native_app_glue)
$(call import-module,libpng)
The last line shows that I got libpng like native_app_glue lib(in the directory of android-ndk sources) Now I want to compile libpng from my project. What I need to change in Android.mk file?

i've got another way for you:
Download all files from here and paste it into a new folder anywhere on your system:
https://github.com/julienr/libpng-android
go into the folder and run:
./build.sh
You will get an libpng.a file in [YOUR_FOLDER]/obj/local/armeabi/libpng.a
Copy this file into:
[YOUR_ANDROID_NDK_FOLDER]/platforms/[ALL_FOLDERS_IN_HERE]/arch-arm/usr/lib/
now you can use libpng in all your projects with the simple line:
LOCAL_LDLIBS += -lpng
you only have to include this in your cpp's:
#include <png.h>
Have fun!

Related

Adding two prebuilt static and shared libraries using Android.mk

I am trying to add both Dlib and superpowers libraries to my android project using Android.mk. I included successfully prebuilt shared Dlib library (.so files) using Android.mk, but when I am trying to add the superpower as a static library (the package has .a files and CMakeLists.txt), I am facing issue regarding liking JNI functions with java. I cannot use the JNI functions in java. I would appreciate it if someone can help me. Here is my Android.mk file:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libs
LOCAL_SRC_FILES := /[MyProjectAddress]/app/libs/$(TARGET_ARCH_ABI)/libdlib.so
LOCAL_EXPORT_C_INCLUDES := /[MyDLIBFileAddress]/dlib_v19.7/dlib
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := superpoweredLib
LOCAL_SRC_FILES := /[MyProjectAddress]/app/libs/$(TARGET_ARCH_ABI)/libSuperpoweredAndroid$(TARGET_ARCH_ABI).a
LOCAL_EXPORT_C_INCLUDES := /[SuperpoweredFileAddress]/SuperpoweredSDK/Superpowered/AndroidIO/SuperpoweredAndroidAudioIO.cpp \ /[SuperpoweredFileAddress]/SuperpoweredSDK/Superpowered \
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := FrequencyDomain
LOCAL_LDFLAGS := -Wl,--build-id
LOCAL_LDLIBS += -llog -ldl
LOCAL_SRC_FILES := \ /[MyProjectAddress]/app/src/main/jni/FrequencyDomain.cpp \
LOCAL_C_INCLUDES += /[MyProjectAddress]/app/src/debug/jni
LOCAL_C_INCLUDES += /[MyProjectAddress]/app/src/main/jni
LOCAL_SHARED_LIBRARIES := libs
include $(BUILD_SHARED_LIBRARY)
LOCAL_SHARED_LIBRARIES := superpoweredLib
include $(BUILD_SHARED_LIBRARY)

ndk-build for jsoncpp always giving one error

Pre-requisites : I am using Android Studio 2.1.2
I have downloaded source of jsoncpp from following link
https://github.com/open-source-parsers/jsoncpp
I have already checked following SO thread , not getting proper solutions :
LOCAL_MODULE_FILENAME should not include file extensions i get this error each time i run ndk-build in terminal
Using JsonCpp on X-Cross platform library
My common Android.mk is as follows:
LOCAL_PATH := $(call my-dir)
#JsonCpp lib
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
include $(LOCAL_PATH)/jsnlib/Android.mk
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
With code for building some other libraries too , which is working fine.
My jsoncpp's Android.mk is as follows
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := $(LOCAL_PATH)/jsnlib/include/json/*.h
FILE_LIST += $(wildcard $(LOCAL_PATH)/jsnlib/src/lib_json/*.cpp)
LOCAL_SRC_FILES := $(FILE_LIST:$(LOCAL_PATH)/%=%)
LOCAL_MODULE := jsnlib
LOCAL_MODULE_FILENAME:= libjsnlib
include $(BUILD_STATIC_LIBRARY)
When I do ndk-build always getting following error
Android NDK: jni/jsnlib/Android.mk:jsnlib: LOCAL_MODULE_FILENAME should not include file extensions
Android NDK: jni/jsnlib/Android.mk:jsnlib: LOCAL_MODULE_FILENAME must not contain a file extension
What happens here is that while you're in the middle of defining your jsoncpplib module you include another makefile, which contains its own module definition:
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
include $(LOCAL_PATH)/jsnlib/Android.mk
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
You should move the inclusion of the other makefile to above the where you're doing CLEAR_VARS:
include $(LOCAL_PATH)/jsnlib/Android.mk
include $(CLEAR_VARS)
LOCAL_MODULE := jsoncpplib
LOCAL_STATIC_LIBRARIES := jsnlib
LOCAL_LDLIBS += -llog -ldl
include $(BUILD_SHARED_LIBRARY)
Also, LOCAL_MODULE_FILENAME:= libjsnlib seems redundant, since LOCAL_MODULE := jsnlib should result in the same library name.

How to include third-party *.a into Android NDK using android studio.?

I am having .a file from third-party. How can use it in android studio?
Please help me.
You can not use directly a .a library in your app. This libraries are static and Android app only allow you to load dynamic libraries.
But you can build a dynamic library linking with your static library. Si you just have to add a few line to you Android.mk to link with this static library. Put your prebuilt .a and his header in a prebuild jni alongside your jni folder. Then your Android.mk should look like something like this :
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := YourStaticLib
LOCAL_SRC_FILES := ../prebuilt/your_static_lib_prebuild.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/../prebuilt
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := DynamicLib
LOCAL_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/../prebuilt
LOCAL_SRC_FILES := your_src_file.cpp
LOCAL_LDLIBS := -llog
LOCAL_ARM_NEON := true
LOCAL_STATIC_LIBRARIES := YourStaticLib
include $(BUILD_SHARED_LIBRARY)

Problems loading dependent .SO in Android NDK

I have the following Android.mk...
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := Box2D-local
LOCAL_SRC_FILES := $(LOCAL_PATH)/../Box2D/libs/$(TARGET_ARCH_ABI)/libbox2D.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := openbox
LOCAL_C_INCLUDES := $(LOCAL_PATH)/.. $(LOCAL_PATH)
NDK_OUT :=../../
LOCAL_SRC_FILES := \
$(subst $(LOCAL_PATH)/,, \
$(wildcard $(LOCAL_PATH)/*.cpp) \
$(wildcard $(LOCAL_PATH)/Collision/Shapes/*.cpp))
LOCAL_LDLIBS := -lm -llog -ldl -lGLESv1_CM
LOCAL_SHARED_LIBRARIES := Box2D-local
include $(BUILD_SHARED_LIBRARY)
Everything compiles fine but when I run the application I see....
D/dalvikvm(14851): Trying to load lib /data/app-lib/com.lmdig.android.tutorial.oglbox2dbasics-1/libopenbox.so 0x40ce7138
E/dalvikvm(14851): dlopen("/data/app-lib/com.lmdig.android.tutorial.oglbox2dbasics-1/libopenbox.so") failed: Cannot load library: soinfo_link_image(linker.cpp:1635): could not load library "libbox2D.so" needed by "libopenbox.so"; caused by load_library(linker.cpp:745): library "libbox2D.so" not found
But when I ls the /data/app-lib/com.lmdig.android.tutorial.oglbox2dbasics-1/ folder on the device I see...
root#android:/ # ls /data/app-lib/com.lmdig.android.tutorial.oglbox2dbasics-1/
libbox2D.so
libopenbox.so
Is my LOCAL_SRC_FILES wrong or something?
Looks like I thought when I loaded it in another class it would be used for this one. That assumption was incorrect and I had to load both libraries in the class.

Prevent manually added libraries from being deleted by ndk-build

I have a project which is reusing a native library (libocr.so) pre-compiled and for which I don't have source files.
I manually put the library on libs/armeabi of my project and everything works perfectly.
Then I needed to create a new native library to the same project. I put my source code as weel as the Android.mk file in my jni folder and I build it with ndk-buld command.
The library is build and placed in libs/armeabi folder, but libocr.so (the one manually added) is automatically deleted from there...
How can I prevent libocr.so from being deleted?
Here is my Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libyuv
LOCAL_SRC_FILES := ycrcbutils.c
include $(BUILD_SHARED_LIBRARY)
Thanks in advance for any help, Luca.
...ok I found the answer by myself...
according to ndk/docs/PREBUILTS.HTML I changed my Android.mk like this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := libyuv
LOCAL_SRC_FILES := ycrcbutils.c
include $(BUILD_SHARED_LIBRARY)
# Add prebuilt libocr
include $(CLEAR_VARS)
LOCAL_MODULE := libocr
LOCAL_SRC_FILES := libocr.so
include $(PREBUILT_SHARED_LIBRARY)
and placed a copy of my libocr.so under jni folder of my project.

Resources