karthik kolanji karthik kolanji - 1 year ago 103
Android Question

Import existing c++ librray (.a or .so file) ndk android

I just gone through native development in android. I am successful in getting my AndroidStudio 2.2.2 ready for native debelopment

I also build sample hello-jni project

What I am trying to achieve

I am trying to use an existing library designed in c++ (I will be provided with static library .a extension or .so file)

Few confusions regarding native developement

1) Shall I use the .cpp & .h files of the exixting c++ library instead of .a or .so file ?

2) Do I need to make CMakeLists.text : So far as I googled my .a files is not generated using ndk-build , so I need to make it.

If I use .cpp & .h files , should I make Android.mk & Application.mk

Does CMakeLists.text compile my newly developed android project as library or my existing .a file ?

3) Where do I put the .a file in my project . Is it under jni folder?

4) Should my java class files should define methods with keyword native same like as implemented in c++ file (Example : In c++ file method name getData() , should java class contain public native getData() )

Answer Source

Ok so you have bunch of questions. Some of these questions are personal preference type but I will provide them as my personal choice.


This is your choice. I, personally, would use the compiled .so file. This way I never have to worry about NDK and CMake and .mk files. If you have the file, all you have to do is add the file to the libs folder (not lib folder) and make a minor change to your build.gradle file. That's it.

Change to build.gradle:

sourceSets {
    main {
        manifest.srcFile 'AndroidManifest.xml'
        java.srcDirs = ['src']
        res.srcDirs = ['res']
        assets.srcDirs = ['assets']
        jniLibs.srcDirs = ['libs']

2 & 3

These would be irrelevant with this option.


You would have to do something like this no matter if you use the files or the compiled libraries:

public class MyNativeMethods {
    static {

    public native int native_method_1(int fd);
    public native int native_method_2(int fd);
    public native void native_method_3(int fd, int arr[]);
    public native int[] native_method_4(int fd);

And then, you can call those methods from your Activity/Fragment.

Hope this is clear enough.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download