Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I compile my own C++ library for Android?

I have written and tested a library in C++. The code even works in my Android application if I add the source files directly. While I do have experience compiling static and dynamic libraries for common operating systems, I have zero experience compiling for a mobile system like this. I've done some research, and I'm still a bit lost as to exactly how to approach this. For example, I am unsure of whether to build a makefile for use with ndk-build or to just invoke one of the Android's compilers directly.

I did see this question, but it does not quite match my situation. I just want to run build and have it spit out libfoo.a (I'd like to produce libfoo.so as well, but libfoo.a is of greater interest to me right now.) The example in that question's winning answer implied that it would build the library as one step/module for building the final application. I tried doing it that way just to see, but I had no luck.

Can anyone please guide me in this endeavor?

CLARIFICATION -- I do not want to build the library and immediately pipe it into an application. I want a .a or .so file that I can link against in multiple future Android applications.

like image 208
TheBuzzSaw Avatar asked Oct 11 '22 16:10

TheBuzzSaw


2 Answers

Create a dummy java file with empty code and make sure there is a android_main function in your C++ code. Build using ndk-build. the resulting apk will make your library an application.

See the samples from the android-ndk-r5/samples directory, see the sample native-bitmap to get some idea.

like image 195
Anil Arrabole Avatar answered Nov 10 '22 00:11

Anil Arrabole


If I understand it correctly that a shared library is not acceptable and you want to be static (but why is that so important?), probably the easiest way to do so is to simply supply source code that can be added to a project.

Ultimately there is nothing special about the ndk build system other than it knowing the right commands to issue to build the necessary files for the assortment of curent android architectures. If you really want to do something different, you can log the operation of the android build system in creating a shared library, and then write your own Makefile that performs the analogous steps to create a static library. HOWEVER, you will have to update your Makefile any time the underlying assumptions or target collections change in a new android version.

like image 21
Chris Stratton Avatar answered Nov 10 '22 00:11

Chris Stratton