How to include static library in another ndk project? - android

I have created static library of one of my project say libABC.a. Now i want to use this static library in another android project at jni layer. The second project would also have its own .cpp files inside jni folder, which would be using the functions of libABC.a static library. Now my question is what are the steps through which i can include static library into another project at jni layer ?

You can use PREBUILT_STATIC_LIBRARY in your Android.mk file.
They have a very detailed explanation in android-ndk/docs/PREBUILT.xml
In short you add to your Android.mk something like
include $(CLEAR_VARS)
LOCAL_MODULE := anynamehere
LOCAL_SRC_FILES := yourlib.a
include $(PREBUILT_STATIC_LIBRARY)
before your module

Related

using android ndk to call native function from .so file

I found instructions for how to link and use c/c++ code in Android by utilizing the NDK. But I'm searching how call function from third party .so .
For example your prebuilt library is called "libmy.so"
In the project folder of the project you want to use it:
1) create libmy folder in jni folder (jni/libmy)
2) copy your libmy.so here
Then, just create a jni/libmy/Android.mk file:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := libmy
LOCAL_SRC_FILES := libmy.so
include $(PREBUILT_SHARED_LIBRARY)
Now in your jni/Android.mk you can write:
LOCAL_SHARED_LIBRARIES := libmy
Then when you do ndk-build, it will copy this library in to libs/armeabi/
After that you can use this library in your C++ code.
You just have to put the .so in your libs/armeabi-v7a folder (or whatever other architecture you have compiled for, like armeabi, x86 etc.) and Eclipse will automatically see it and integrate it into the APK.
Then to access any native functions from the .so in your Java code, you just have to declare it as a native function at the top of your class. For example
protected static native void AKUAppInitialize ();
which can then be called anywhere later in the code like
AKUAppInitialize();
You have to configure your NDK part if you have the source code: look at this tutorial

Where does the NDK look for libraries?

Suppose I want to compile with the NDK a c++ function that in its body calls a function in a library (like STL, etc). How to tell the NDK where in my pc is the library so that when compiling my c++ function the NDK will make the jump when the function in the library is called ?
You have to specify your libraries in your Android.mk file.
For the standard library, you only have to specify in your Application.mk which one you want to use, i.e.:
APP_STL=gnustl_shared
for the GCC standard library.
For your other libraries, you have to put in your Android.mk file which library you want to use, and tell the ndk to build them if necessary.
For an already build library, put
include $(CLEAR_VARS)
LOCAL_MODULE=<give a name to the lib you want to link>
LOCAL_EXPORT_C_INCLUDES=<path to your lib include directory>
LOCAL_SRC_FILE=<path to your library binary file>
include $(PREBUILT_SHARED_LIBRARY) #or STATIC if your lib is static
For a library that must be build, put
include $(CLEAR_VARS)
LOCAL_MODULE=<give a name to the lib you want to link>
LOCAL_SRC_FILE=<list all the files necessary to build your lib>
LOCAL_EXPORT_C_INCLUDES=<path to your lib include directory>
include $(BUILD_SHARED_LIBRARY) #or STATIC if you want to build it static
Then, after doing that, just add the following
LOCAL_STATIC_LIBRARIES=<list your static libs **using their LOCAL_MODULE names**>
LOCAL_SHARED_LIBRARIES=<list your shared libs **using their LOCAL_MODULE names**>
and voila!

android ndk communicate different c++ project

I want to create a c++ project for android ndk.And I want to use it every project like dynamic library.I dont want to change /transport source code every time.I import *.so file and include it and use its class or whatever.
This is possible.If it possible how could import and use it.
Or i create java project and i use it to communicate c++ project with using jni and i compile it.After that i have a *.jar file and i use it instead of android ndk.
Which one of them possible or effective.
I'm not entirely sure if I understood the question correctly, but I assume you prefer to write your Android applications using solely/mostly C++ and have a core library/module that you want to re-use for every consecutive project WITHOUT including that libraries SOURCE files in each consecutive project.
You can omit including the source files and include the final built .so file in your new project by adding the required libraries into your makefile. Like so:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_C_INCLUDES := (includes for libraryname)
LOCAL_MODULE := libraryname
LOCAL_SRC_FILES := libraryname.so
include $(PREBUILT_SHARED_LIBRARY)
include $(CLEAR_VARS)
--- instructions for custom application code here ---
LOCAL_SHARED_LIBRARIES := libraryname
Where "libraryname" is the name of the library module and "libraryname.so" is the name of the library file. Note the path should be relative to the make file. Below the second "include $(CLEAR_VARS)" and above the final "LOCAL_SHARED_LIBRARIES" you add the instructions for building the source code of the application which is to use the shared library.
Don't forget to load all libraries in order on the Java side, i.e.:
System.loadLibrary( "libraryname" );
System.loadLibrary( "customlib" );

How to use .so and .h files in Android?

I am using C++ to code in Android (I am using cocos2d-x, specifically).
Now, say, I have a class, MyClass, with .h and .cpp files. I would like to create a .so out of these files. Then, I would like to include that .so in my project and access it via #include "MyClass.h". Is it possible? If so, how? Thanks!
To build your static library for ARM you can use the NDK standalone toolchain.
Once you've compiled your .so, you can include the library in your project using the Android NDK make file (Android.mk) which will look like the following:
LOCAL_PATH := $(call my-dir)
LOCAL_SRC_FILES := my_module.so
LOCAL_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)
You'll want to make sure you include the headers (/include) in the right path.
Not sure if your intent is to ge back into Android-land with your code, but if you're looking to create a bridge between your .so/C/C++ code and Java, you'll need to describe the appropriate JNI methods in C, build out the necessary Java classes with the static System.loadLibrary('my_module') imports and native method declarations mapped to your JNI methods.

Re-use code Android NDK

I found a lot of tutorials showing how to start developing Android Applications using NDK.
But I have a rather "easy/stupid" question:
Please consider the following two tutorials:
http://mobile.tutsplus.com/tutorials/android/ndk-tutorial/
http://www.indiedb.com/tutorials/creating-compiling-and-deploying-native-projects-from-the-android-ndk
http://mindtherobot.com/blog/452/android-beginners-ndk-setup-step-by-step/
http://marakana.com/forums/android/examples/49.html
Now, in the second tutorial they are building the hello-jni example.
My question is:
After using the ndk-build
and producting the:
is it possible to use the resulted libhello-jni.so and distribute this to others instead of the actual C code?
For example modifying the Android.mk and replacing com_myproject_MyActivity.c to something.so in order to include the shared library?:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := com_myproject_MyActivity.c
include $(BUILD_SHARED_LIBRARY)
Any suggestions or tutorials welcome. Thanks in advance.
You can use a prebuilt NDK library by copying it into libs/armeabi (or whatever architecture is it for), then loading in run-time. From the standpoint of the Android build system, it's just another file to include in the APK.
The problem, however, is that the JNI function names include, by convention, the name of the package and class they will belong to; so from the standpoint of a SO consumer project, its use will look rather unnatural, as none of the JNI functions would fit into its classes. You'll probably have to ship a companion JAR where the respective Java classes are declared.
Via VitamioBundle,
You no need to do re-using code:
is it possible to use the resulted libhello-jni.so and distribute this to others instead of the actual C code? For example modifying the Android.mk and replacing com_myproject_MyActivity.c to something.so in order to include the shared library?
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mylib
LOCAL_SRC_FILES := com_myproject_MyActivity.c
include $(BUILD_SHARED_LIBRARY)
VitamioBundle can be considered as shared library,
Therefore, you can use it as your Android Library.
Let's fun.

Categories

Resources