Calling android C/C++ code with Java Native Access (JNA)

in #utopian-io7 years ago (edited)

Overview

Java Native Access (JNA) hides all the complexities of Java Native Interface (JNI) and allows java developers to call native code using 100% pure Java without having to write C/C++ code. We can directly call native functions using plain java methods, no boilerplate or generated glue code is required.

This post covers

  • Creating basic Android Studio project
  • Downloading JNA AAR library
  • Importing and configuring JNA AAR library
  • Calling C/C++ code from Java

Requirements

  • Android Studio 3.0 or higher

Difficulty

  • Intermediate

Guide

1. Creating basic Android Studio project

Create new android project and change Application name you can also change Company domain and Package name according to requirements and click next

p1.png

Select minimum SDK version and click next

p2.png

Select Empty Activity and click next

p3.png

Change Activity Name and Layout Name according to requirements and click finish to create project

p4.png

Project has been created after successful gradle builld

p5.png

2. Downloading JNA AAR library

Download jna.aar from here

3. Importing and configuring AAR library

Create New Module and select Import JAR/AAR Package and click next

p6.png

Select jna.aar file from file system and click Finish

p7.png

Now jna module added to project, open build.gradle file and add jna module under dependencies

dependencies {
    compile project(':jna')
    ....
    ....
}

p8.png

4. Calling C/C++ code from Java

Native method we will call is available in android C shared library (libc)

pid_t getpid(void);

getpid() returns the process ID (PID) of current running process. Return type is int and accepts no arguments.

Mapping this C function to Java is simple, Java method name will be same as C method name, return type is int with no arguments and we have to add native keyword which tells compiler that method is implemented in native code.

public static native int getpid();

Last step is to load native shared library in static block which will load shared library during class loading time.

static {
    Native.register(MainActivity.class, Platform.C_LIBRARY_NAME);
}

First argument to Native.register() method is the class where we defined our native methods and 2nd argument is the name of native library in our case is C library which has getpid() method.

p9.png

Calling native method is same as calling other java methods

System.out.println("process id = " + getpid());

Output

Process id of our running app is printed in logcat window, highlighted in screenshot

p10.png

Conclusion

JNA hides all the complexities of Java Native Interface (JNI) and allows developers to write pure java code without writing C/C++ code. It removes the overhead of writing C/C++ code, headers files, C to Java type conversions and vice versa, memory management etc.
JNA is available on all major desktop platforms and also on android platform.

Code

Project is available on github



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @kabooom I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • This is your first accepted contribution here in Utopian. Welcome!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

thanks and i voted for you