Using C/C++ Library on Android


I have coded a library that provides some function add, sub, divide, multi by c/c++ programming language. It's built to library.so by using Android NDK. So now, I want to call these function of library by using Android. I want to as How I can do ?



NDK Package

Android Studio only includes the default tools and SDK, so download and install the NDK package separately. There are two ways to do this. The first and easiest is the automatic installation option in the SDK Tools tab. This is a large download (~1GB) so be sure to have a good internet connection and/or patience.

The second way is to download the NDK manually from the NDK Downloads page. This saves some time because of the smaller download size (<.5 GB) but will need some setting up. Download the appropriate NDK package for your platform and follow the installation instructions. You can put the extracted package anywhere you want, but remember this location as you will need it later.

Hello World

Let’s run a test project to see if the NDK installation works. The NDK package folder contains samples but they don’t seem to work out of the box in Android Studio without extra configuration. Instead we’ll import samples that work from the welcome screen. These NDK samples from GitHub can also be downloaded or cloned directly. This collection of samples has better compatibility with the latest version of Android Studio.

Select Import an Android code sample and type hello in the search box to filter the list. Choose Hello JNI from the filtered list under the Ndk category. Click Next to edit the app name and project location, then click Finish.


After the code has loaded and Gradle has synced, let’s take a look at the resulting project structure. You might have noticed what appears to be a discrepancy in the project’s name. Why is it called HelloJNI and not HelloNDK? What is ‘JNI’ and how does it differ from the ‘NDK’? The ‘Java Native Interface’ is a framework that enables Java applications to interact with native code. The JNI and the NDK work together to bring native support to Android apps. The NDK is part of the Android framework while the JNI is available to any Java application, not just Android apps.

Inside the project is a folder named jni which will hold all the native C or C++ source code of the app. The JNI provides a two-way interface. The C/C++ code is able to call Java code, including the standard Android libraries, and the Java code is able to call native functions defined in C/C++ code. Native code is governed by the same sandbox and security rules as Java code so the app will not have full unrestricted access to hardware. For a more detailed discussion on the JNI, read the JNI articles from the Android Developer website.


For a working example project, you can find one from here: https://github.com/russell-shizhen/JniExample



  • Using static function variable vs class variable to store some state
  • Logging simple text in Apache Camel logs
  • How to play an AMR audio file?
  • Setting up Akka with Eclipse
  • How to manually build Expression which will return always true?
  • Cannot put DrawerLayout under StatusBar
  • Draw multiple rectangles android canvas
  • robocopy invalid parameter “-”
  • How to do multi-class image classification in keras?
  • How to set the GOOGLE_APPLICATION_CREDENTIALS from Google Cloud Vision in C#?
  • Laravel: Google contacts API gives empty results
  • Python Django ImportError: No module named website
  • Plotting a continuous stream of data with MatPlotLib
  • Hadoop MapReduce sort reduce output using the key
  • Combine rows with same id and delete duplicated rows
  • Fork and Join with Amazon Lambda
  • Old data appears in SQLite database when reinstalling app
  • Eclipse ADT Plugin crashed after updating to version 22.0
  • Mock request/post with mockito
  • Call a specific instance of a service in Azure Service Fabric
  • Microsoft bot framework webchat C#
  • What is the correct way to combine two UserControls into one with two states?
  • no endpoints available for service \\“kubernetes-dashboard\\”
  • Octave code for gradient descent using vectorization not updating cost function correctly
  • How to create wsdl from xsd
  • Calculate savings percentage for house down payment in 36 months
  • Combine two jagged lists into one
  • VB.net Service Programming and using TCP Sockets
  • How to search for a method or variable name within android project?
  • Auto send email based on the time and email address in database
  • Defer unused CSS
  • Boolean filter using a timestamp value on a dataframe in Python
  • AWS RDS Parameter Group not changing MySQL encoding
  • .Net core Hosted Services guaranteed to complete
  • Add checkbox dynamically using angular 2
  • ASP.NET MVC 2 actions for the same route?
  • Ajax call on Multiple selection in Select box
  • trigger ontouch event programmatically
  • How to handle div that is created dynamically in a table
  • Google App Engine backend servlet not responding