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!
Related
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
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" );
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.
This question seems to have been asked a lot but all were trying to use eclipse to package the library inside the APK.
However, my requirement is to package the library inside the APK (which will later be loaded using System.loadLibrary() through Java) using the Android build system, i.e. i want to write an Android.mk file that does this job.
Requirement:
1. Prebuilt shared library: libTest.so
2. Write an Android.mk file that will package this to libs/armeabi-7 inside the apk.
I don't know much about the build system I am using but the compilation is done using "mm" command after exporting the required environment variables.
When I provide libTest for LOCAL_JNI_SHARED_LIBRARIES, it tries to find it inside its exported paths and fails to find it there and hence build fails.
Can anyone please give any pointers on writing an Android.mk file that will package my prebuild shared library into the APK?
In order to prebuild your native library you have to
Create jni folder in your project folder
Create libs folder in your project folder
Add Adnroid.mk make file to the jni folder, it should looks like
this:
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_LDLIBS := -llog
LOCAL_MODULE := Test
LOCAL_SRC_FILES := Test.cpp
include $(BUILD_SHARED_LIBRARY)
Note 1: Test.cpp is the main library source file containing implementation of native methods. You can add more sources as a space separated list.
Note 2: Do not include headers, they are included automatically.
Note 3: If you need to enable C++ STL, then create another make file - Application.mk, add it to the jni folder and set APP_STL := stlport_static flag in it.
Then you will have to set up a builder. Refer to this article for how to do that:
After these steps, the builder will create your library in the libs folder which will be automatically packed into the apk when building the whole application.
Note: Your library name should be lowercase, it is a Linux convention. The "lib" prefix will be automatically added, so the final library name will be libtest.so.
Let's say there is one libxxx.so shared library under libs/armeabi/ of project which you want to pack into apk. There are 2 things you need to do in the Android.mk as below:
# 1. Copy .so to out/target/product/***/obj/lib
$(shell cp $(wildcard $(LOCAL_PATH)/libs/armeabi/*.so $(TARGET_OUT_INTERMEDIATE_LIBRARIES))
# 2. Make libxxx to be packed into apk
LOCAL_JNI_SHARED_LIBRARIES := libs/libxxxx
Then you can use apktool to unpack the built apk, you will find the libxxx.so will be located in libs/armeabi*/. It is indeed packed into the apk.
I had two LOCAL_JNI_SHARED_LIBRARIES that I wanted to build into my APK. I manage to do it by setting the following in my Android.mk:
LOCAL_MODULE_TAGS := samples
After re-compiling the module I was able to find both libraries inside the .apk under /lib/.
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