Call C++ library from Java in Android

14,815

Solution 1

Yes, it's possible. You need to build your C++ library using the Android NDK. The new link https://developer.android.com/ndk/index.html

Solution 2

You need to write an Java Native Interface(JNI) to be able to call the native(c/c++) libraries from Android Java code.
The hierarchy is like:

+++++++++++++++++++++++++++++++++++        
|     Android Application         |        
+++++++++++++++++++++++++++++++++++        


+++++++++++++++++++++++++++++++++++  
|  Android Application Framework  |
+++++++++++++++++++++++++++++++++++  


+++++++++++++++++++++++++++++++++++    
|     Java Native Interface       |
+++++++++++++++++++++++++++++++++++


+++++++++++++++++++++++++++++++++++
|      C/C++ Native Libraries     |
+++++++++++++++++++++++++++++++++++

Solution 3

Yes you can. As previous posters mentioned you build your C++ library using the NDK and use JNI to call it from Java

If you're planning to make several C++ classes accessible you can use SWIG to automatically generate the JNI layer for you

http://www.swig.org/index.php

Share:
14,815

Related videos on Youtube

Agnel Kurian
Author by

Agnel Kurian

Software Engineer with 18+ years of experience developing software in a wide range of areas: desktop, web, user interfaces, 2D/3D graphics, geometry, encryption and even structural analysis! I have a degree in Civil Engineering and a diploma from NIIT. I'm very comfortable on Linux. I like solving interesting problems.

Updated on June 04, 2022

Comments

  • Agnel Kurian
    Agnel Kurian almost 2 years

    Is it possible to call a C++ library from an Android app? If yes, how?

    I have zero experience in Android programming (some Java programming experience though) but I would still like to know if this is possible. Given the number of great C++ libraries out there, I would be surprised if it were not possible to call a C++ library from an Android app.

  • Ela782
    Ela782 over 8 years
    What if my C++ library is a header-only library? (i.e. there's nothing to build)?
  • Mitch
    Mitch about 5 years
    Can the JNI call C++ directly or does it call C that calls C++?
  • somebody4
    somebody4 about 3 years
    for reference....just create an empty cpp and include the header