Android NDK refer to external libraries in JNI - android

I have written C++ file in JNI folder of my application. I am using Windows system with NDK and Cygwin 1.7.I want reffer to CURL library available in Cygwin.How can we refer to external .h(libraries/header) files while creating JNI application in Android?I have created a combined Android and C++ project. But I am referring CURL header file. When I build the project I am getting fatal error: curl/curl.h: No such file or directory issue.

Follow these steps:
Converting from Android project to C/C++ project:
Right click on your project name, go to 'Android Tools' and click 'Add native support'
Adding paths to external .h files:
Right click on your project name, go to 'Properties', under 'C/C++ General', go to 'Paths and Symbols', under 'Includes' tab, add the folder in which your .h file is. Remember to add to all languages and configurations if asked.
Also, since you are in Windows, I think you will need to change your Build command (which is in the 'C/C++ Build' section in project properties) to "bash C:\Development\android-ndk-r8\ndk-build.cmd"

Add the following to your Android.mk:
LOCAL_CFLAGS += -I$/PATH/TO/YOUR/curl.h
LOCAL_LDLIBS += -L$/PATH/TO/YOUR/libcurl.a.for.android -lcurl
The libcurl.a you have installed in cygwin is not usable for android, you need a version targetting android. If you don't have it, build it yourself.

When you get that libcurl.a file, do not forget to copy the headers folder of curl (get into your usr/include/curl from Cygwin) and add this folder to the JNI one in your project, so it knows the headers while compiling.
Which means also referring in your Android.mk :
for the libcurl library
LOCAL_SRC_FILES := libcurl.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/curl
and for your C++ files
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include/curl
LOCAL_WHOLE_STATIC_LIBRARIES := libcurl

Please used this tutorial is nice one.
Don't forgot to change this setting after convert project to C / C ++ native project.
Builder Settings to Build Command
bash C:\tools\android-ndk-r8b-windows\android-ndk-r8b\ndk-build
This is my path of NDK you can change this path accordingly your NDK path.

Related

Call shared library (.so) methods within Android Studios C files

I'm struggling with this for several days now. At the moment i'm just testing it with a simple C++ project (1 .h & 1 .cpp file) and a minimalistic App including the ndk helloJNI sample code (which worked perfect easily):
Target
Import existing C/C++ files (project) to Android Studio
Approach
After trying out some of the (dozens) of different possibilities, i think/thought the following steps would be the best solution for my purpose:
Create the shared library (Calculator.so) from Visual Studios 2015 "Create shared library for Android" (or something) [successful]
Create jniLibs folder in src/main/ with its subfolders (x86 the relevant one in my case)
Add the Android.mk file in src/main/jniLibs which has to be placed there (?)
Include statement: System.loadLibrary("Calculator") without "lib" and ".so" in MainActivity
The library is listed in Android Studio in its folder jniLibs as like the Android.mk. Moreover if i build the apk, the library is successfully packed (verified by unzipping) and i dont get any errors.
BUT: how can i call the methods in the library? I tried the different solutions offered in other threads, but i think i missed something in my .mk or my steps described above.
Tried
Different #include <myLib> statements in native-lib.cpp, like s
Different Android.mk settings (but i'm new to make files so not even tutorials helped me much with my specific problem ::) )
Other locations for the libCalculator.so like in the subfolder x86
and many others - simply not reminding atm (wasntme)
Your help is highly appreciated!
Android.mk
LOCAL_PATH := $(call my-dir)
APP_ABI := x86
# library info
include $(CLEAR_VARS)
LOCAL_MODULE := Calculator
LOCAL_SRC_FILES := $(TARGET_ARCH_ABI)/Calculator.so
LOCAL_EXPORT_C_INCLUDES := ..../Visual Studio 2015/Projects/SO_Library/SO_Library
include $(BUILD_SHARED_LIBRARY)
There are lots of things, you can do in Android NDK. For example, Camera hardware is one of the heaviest hardware in Android OS. Detecting faces, things, giving effects and for thousands of features NDK is the best.
Some helps for your steps:
You can built and prebuilt shared(.so) and static(.a) libraries in Android Studio also. Not need Visual Studio.
Don't create jniLibs folder in main folder. When you build your project via gradle, it already creates this folder and put your target libraries. If you want prebuilt any libraries, put these libraries in main/jni/libs folder and prebuilt then with Android.mk.
Don't add the Android.mk file in jnilibs folder. Create this file in main/jni folder. Also Application.mk file.
Call your libraries, in any activity, where you need, in static method. Like this:
static { System.loadLibrary("my_library") }
Without "lib" and ".so" extensions.
When you want to call your native methods, just use "native" keyword. For example:
private native int nGetNumberFromNativeSide();
Just call this method, where you want, and get result. But for ndk building in gradle side, look at this answer. For building library in Android.mk, these sample lines maybe help you:
include $(CLEAR_VARS)
ifneq (,$(filter $(TARGET_ARCH_ABI), armeabi-v7a x86 arm64-v8a x86_64))
LOCAL_MODULE := my_library
LOCAL_SRC_FILES := $(LOCAL_SRC_LOCATION)/native1.cpp native2.cpp
include $(BUILD_SHARED_LIBRARY)
You can put name anything you want, but dont add lib and .so extensions. Ndk is already doing it.
I have already gave Android.mk example.
When you build Android.mk file, it locates your libraries appropriate folder. Like main/libs/x86/libmy_library.so.
I guess this answer will help you. If you have more questions, add to comment, i'll edit my answer and add answers.

How does one Android NDK library without a Android Project

I would like to be able to build a shared library in using the android ndk-build script, but for some reason I get a bunch of errors:
# I have Application.mk and Android.mk in the current folder
ndk-build -C .
Android NDK: Could not find application project directory !
Android NDK: Please define the NDK_PROJECT_PATH variable to point to it.
Is there a way to build the Android shared library with only the source and header files?$$\int$$
Yes, you can write
ndk-build NDK_PROJECT_PATH=. APP_BUILD_SCRIPT=./Android.mk
But it's much easier to create ./jni directory and put both Application.mk and Android.mk there.

Android NDK Include Path In Eclipse

I have an Android NDK project that builds fine in cygwin using ndk-build.
However, I wanted to have it build in eclipse, so I installed C/C++ Development tools into my ADT version of eclipse and added native support to the project in eclipse. However, after building, I get the following error:
fatal error: timer.h: No such file or directory
In my original Android.mk file, I have the following include that lets it work in cygwin:
LOCAL_C_INCLUDES := /cygdrive/c/ADT/includes/
I tried adding a similar include path (C:\ADT\includes) to Project->Properties->C/C++ General->Paths and Symbols, but still no luck. Any suggestions?
The fix was to use windows paths
LOCAL_C_INCLUDES := C:/ADT/includes/
Note that using the following works as well (per cpu2's answer)
LOCAL_CFLAGS := -IC:/ADT/includes/
Add -I/path/to/includes to your cflags.

How to add dynamic c++ library to Android source?

I need to add my native library to Android source for others applications can use it (call functions from this library in their code). I need to use library like embed without adding it to every project in that I want use it. But i can't find information about this.
Please give me information how to do this. Sorry
As far as I understand, what you are trying to do is to run c/c++ code on Android. Am I right?
To be able to do that, you should use Android NDK to build your native library, and then load it into java code.
You need to follow 4 simple steps to run native code on android, after installing ndk:
1) Create Java "wrapper" for your native code - for. eg. create class named MyNatives which will hold method declared with native keyword. This tells the compiler, that implementation of this method is done in native library. Create static initializer, which will load the library. eg:
public class MyNatives {
static {
System.loadLibrary("hello-jni");
}
public void native nativeMethod(int x);
}
2) Compile the code, and run tool called javah for your native class (there are some plugins for eclipsee which will do that for you, eg. sequoyah)
cd <your project path>
mkdir jni
javah -d jni -classpath bin/classes com.example.MyNatives
This will generate header in jni directory (all native code in android project should be inside this direcotry)
3) Add implementation of your method from generated header
4) Create makefile for android build system and build library. Makefile should be named Android.mk. It makes use of some specific variables and macros, for more info please see NDK documentation, eg:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
# your library name
LOCAL_MODULE := hello-jni
# all source files
LOCAL_SRC_FILES := hello-jni.c
include $(BUILD_SHARED_LIBRARY)
to buld library just call ndk-build from project root directory
For more info, please see Android NDK documentation. Hope it helped a little.

Android NDK Build error in Cygwin

I am configuring NDK for an android app which makes use of the linux/input.h file in its header - #include . The issue is when I call ndk-build, it gives an error which basically implies that it cannot find linux/input.h. Now, cygwin does not contain the linux directory, which is probably why this error is occuring but the ndk does contain it - android-ndk-r8e\platforms\android-14\arch-x86\usr\include\linux\input.h - shouldn't ndk-build search here as well? To give some more context, I am trying to compile the EventInjector library described here and others seem to have it working, which means it must be possible somehow.
Please follow these instructions:
in Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
TARGET_PLATFORM := android-8
LOCAL_MODULE := your_lib
LOCAL_SRC_FILES := my_file.c
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)</li>
your_lib will be the name of your library, you may write any name
my_file.c will be your source file name that is present in JNI folder
Right click on project
Choose Properties>Builders
Click on New...
Select Program, then Ok
In main tab:
Give your Builder a suitable name, eg. "NDK"
For Location, click on Browse file system>select your ndk-build.cmd,
eg. C:\android-ndk-r8-windows\android-ndk-r8\ndk-build.cmd (My NDK path)
Below there will be Working Directory:
Click on: Browse workspace - choose your project,
eg. ${workspace_loc:/Test} (My project 'Test')
Click on **Refresh Tab**
Select **Specific Resources** radio button
Click on **Specify Resources..**
Select libs folder from your project
Now click on **Build Options** Tab
Check:
After a Clean
During manual builds
During auto builds
Specify working set of relevant resources
Click on **Specify Resources...**
Select JNI folder of your project
Click Apply...Ok
Now clean it and build...then refresh
You will get your_lib.so in libs>armeabi
libs>armeabi>your_lib.so this structure will create automatically ....

Categories

Resources