C++ Library on Android with Android Studio
最后更新于
最后更新于
2013-08-07
This post falls into the category of “write it down before I forget it”. I know next to nothing about Android/Java development (approx 12 hours worth) but I knew I needed a certain C++ library for an upcoming app. I managed to get the C++ library working from java after 20+ attempts, 4 coffees and the better part of an evening.
Most of the code here is cobbled together from these sources:
, and included documentation.
Android Studio v0.23, NDK release 9, target SDK version of 8. Mac OS.
These are the steps:
Compile your library for Android
Write the C/C++ wrapper for your library
Configure gradle to package up your library
Test from java
The key documentation file to read is called STANDALONE-TOOLCHAIN.HTML
as we will be using a standalone toolchain to build the third party library. Install the standard toolchain. The commands below will install it to /tmp/my-android-toolchain
.
Set some environment variables so that the configuration and build process will use the right compiler.
Extract your library tarball and start the configuration and building process. It is important to tell your configure script which toolchain to use, as well as specifying a folder (prefix) for the output. Since we are building a static library we will also instruct it to build one.
You should now have a yourLibrary.a
file in build/lib
and a whole pile of headers in build/include
. Create a folder called prebuild
in your Android project root folder. (The root folder is one level down from the YourAppNameProject
folder and is usually named after your app) Copy the yourLibrary.a
file to the prebuild
folder and also copy the include
folder.
This will depend on which library you are wrapping. Modify one of the following to carry out some simple task using the library you are wrapping. These are derived from the hello-jni
sample app in the NDK - check there for more info on how they work. Your wrapper files and the .mk
files should be placed in the project_root/jni
folder.
Next, set up the Android.mk
file for your wrapper. This is like a makefile
for the ndk-build command that will build your wrapper.
I also needed the following in my Application.mk
file:
At this point, you should be able to build your library from the jni
folder.
You can check the project_root/libs/armeabi
folder for your new library.
Android Studio doesn’t currently support NDK development so some gradle hacks are required. In a nutshell, the modifications copy and package up the .so file so that it is copied and installed with your app. Check the references for more detail. In build.gradle add the following:
(Update August 2015 - I’ve been informed that tasks.withType(Compile)
should now be tasks.withType(JavaCompile)
.)
Also add the following to the dependencies{...} section
:
In the activity you are calling your wrapper from, add the following, modifying names as appropriate:
If it doesn’t crash, you have probably done it. Time to celebrate!
First, grab the . This includes a toolchain for cross-compiling C/C++ to Android. Extract the NDK somewhere sane, and add the tools to your path.